public List getVertical() {
return vertical;
}
public void setVertical(List vertical) {
this.vertical = vertical;
}
public static class VerticalBean {
/**
-
preview : http://img5.adesk.com/5e26fed9e7bce75ebd2c449d
-
thumb : http://img5.adesk.com/5e26fed9e7bce75ebd2c449d?imageMogr2/thumbnail/!350x540r/gravity/Center/crop/350x540
-
img : http://img5.adesk.com/5e26fed9e7bce75ebd2c449d?imageMogr2/thumbnail/!720x1280r/gravity/Center/crop/720x1280
-
views : 0
-
cid : [“4ef0a35c0569795756000000”]
-
rule : ?imageMogr2/thumbnail/! < W i d t h > x <Width>x <Width>xr/gravity/Center/crop/ < W i d t h > x <Width>x <Width>x
-
ncos : 6
-
rank : 51169
-
source_type : vertical
-
tag : []
-
url : []
-
wp : http://img5.adesk.com/5e26fed9e7bce75ebd2c449d
-
xr : false
-
cr : false
-
favs : 830
-
atime : 1.580187618E9
-
id : 5e26fed9e7bce75ebd2c449d
-
store : qiniu
-
desc :
*/
private String preview;
private String thumb;
private String img;
private int views;
private String rule;
private int ncos;
private int rank;
private String source_type;
private String wp;
private boolean xr;
private boolean cr;
private int favs;
private double atime;
private String id;
private String store;
private String desc;
private List cid;
private List<?> tag;
private List<?> url;
public String getPreview() {
return preview;
}
public void setPreview(String preview) {
this.preview = preview;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
public int getNcos() {
return ncos;
}
public void setNcos(int ncos) {
this.ncos = ncos;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSource_type() {
return source_type;
}
public void setSource_type(String source_type) {
this.source_type = source_type;
}
public String getWp() {
return wp;
}
public void setWp(String wp) {
this.wp = wp;
}
public boolean isXr() {
return xr;
}
public void setXr(boolean xr) {
this.xr = xr;
}
public boolean isCr() {
return cr;
}
public void setCr(boolean cr) {
this.cr = cr;
}
public int getFavs() {
return favs;
}
public void setFavs(int favs) {
this.favs = favs;
}
public double getAtime() {
return atime;
}
public void setAtime(double atime) {
this.atime = atime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStore() {
return store;
}
public void setStore(String store) {
this.store = store;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public List getCid() {
return cid;
}
public void setCid(List cid) {
this.cid = cid;
}
public List<?> getTag() {
return tag;
}
public void setTag(List<?> tag) {
this.tag = tag;
}
public List<?> getUrl() {
return url;
}
public void setUrl(List<?> url) {
this.url = url;
}
}
}
}
实体bean有了,下面就是拆分这个请求地址了。找到在mvplibrary下找到ServiceGenerator.java。
新增如下
然后再打开ApiService.java,里面新增一个接口
/**
-
手机壁纸API
-
@return WallPaperResponse 网络壁纸数据返回
*/
@GET(“/v1/vertical/vertical?limit=30&skip=180&adult=false&first=0&order=hot”)
Call getWallPaper();
下面就是订阅器了,在app中的contract包下新建一个WallPaperContract.java,里面的代码如下:
package com.llw.goodweather.contract;
import com.llw.goodweather.api.ApiService;
import com.llw.goodweather.bean.BiYingImgResponse;
import com.llw.goodweather.bean.WallPaperResponse;
import com.llw.mvplibrary.base.BasePresenter;
import com.llw.mvplibrary.base.BaseView;
import com.llw.mvplibrary.bean.AppVersion;
import com.llw.mvplibrary.bean.WallPaper;
import com.llw.mvplibrary.net.NetCallBack;
import com.llw.mvplibrary.net.ServiceGenerator;
import retrofit2.Call;
import retrofit2.Response;
/**
-
壁纸订阅器
-
@author llw
*/
public class WallPaperContract {
public static class WallPaperPresenter extends BasePresenter {
/**
- 获取必应 每日一图
*/
public void biying() {
ApiService service = ServiceGenerator.createService(ApiService.class, 1);
service.biying().enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if (getView() != null) {
getView().getBiYingResult(response);
}
}
@Override
public void onFailed() {
if (getView() != null) {
getView().getDataFailed();
}
}
});
}
/**
- 获取壁纸数据
*/
public void getWallPaper() {
// 6 表示访问网络壁纸接口
ApiService service = ServiceGenerator.createService(ApiService.class, 6);
service.getWallPaper().enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if (getView() != null) {
getView().getWallPaperResult(response);
}
}
@Override
public void onFailed() {
if (getView() != null) {
getView().getDataFailed();
}
}
});
}
}
public interface IWallPaperView extends BaseView {
/**
-
获取必应每日一图返回
-
@param response BiYingImgResponse
*/
void getBiYingResult(Response response);
/**
-
壁纸数据返回
-
@param response WallPaperResponse
*/
void getWallPaperResult(Response response);
/**
- 错误返回
*/
void getDataFailed();
}
}
订阅器也有了,现在回到WallPaperActivity ,继承MvpActivity,传入订阅器,然后实现里面的接口,代码如下:
package com.llw.goodweather.ui;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import com.llw.goodweather.R;
import com.llw.goodweather.contract.WallPaperContract;
import com.llw.mvplibrary.bean.WallPaper;
import com.llw.mvplibrary.mvp.MvpActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Response;
/**
-
壁纸管理
-
@author llw
*/
public class WallPaperActivity extends MvpActivity<WallPaperContract.WallPaperPresenter> implements WallPaperContract.IWallPaperView {
@Override
public void initData(Bundle savedInstanceState) {
}
@Override
public int getLayoutId() {
return R.layout.activity_wall_paper;
}
@Override
protected WallPaperContract.WallPaperPresenter createPresent() {
return new WallPaperContract.WallPaperPresenter();
}
@Override
public void getBiYingResult(Response response) {
}
@Override
public void getWallPaperResult(Response response) {
}
@Override
public void getDataFailed() {
}
}
现在可以先不管这个Activity了,列表既然是显示壁纸,那么就需要一个item的布局,下面在app下的layout下面创建一个item_wallpaper_list.xml,布局代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:id=“@+id/item_wallpaper”
android:layout_height=“wrap_content”
android:layout_margin=“@dimen/dp_5”
android:orientation=“vertical”>
<com.google.android.material.imageview.ShapeableImageView
android:id=“@+id/iv_wallpaper”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:scaleType=“centerCrop”
app:shapeAppearanceOverlay=“@style/roundedCornerStyle” />
你可能没有用过ShapeableImageView,没关系,首先在mvplibrary中的build.gradle中的dependencies闭包下新增一个依赖库
api ‘com.google.android.material:material:1.2.0’//更强
我之前的是1.1.0,那么你可以改成1.2.0。然后同步到你的项目中。你就可以是material专属的UI控件了,你可能会问为什么要用这个控件,普通的ImageView不行吗?因为普通的ImageView没有圆角啊,说道圆角图片我相信你不会陌生,你可能想到自定义ImageView来实现、或者使用第三方库来实现,但是ShapeableImageView里面就自带了圆角的样式给你,惊不惊喜意不意外?好了,废话不多少了,你的布局中应该还有报错的地方才对。因为你少了一个roundedCornerStyle的样式。在mvplibrary下的styles.xml中,新增一个样式就可以了。
可能你还注意到我这个item的高度是wrap_content,所以你看不到高度,那么为什么这样做呢?因为我要使用瀑布流,哪种错落感,会给用户不一样的体验,因为不设置高度,是因为需要动态设置ImageView的高度,来实现这个错落感。OK,下面该写这个Adapter了。在app的adapter包下新建一个WallPaperAdapter.java,相信这个代码你能看得懂。
package com.llw.goodweather.adapter;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
import com.baidu.panosdk.plugin.indoor.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.material.imageview.ShapeableImageView;
import com.llw.goodweather.R;
import com.llw.goodweather.bean.WallPaperResponse;
import java.util.List;
/**
-
壁纸列表适配器
-
@author llw
*/
public class WallPaperAdapter extends BaseQuickAdapter<WallPaperResponse.ResBean.VerticalBean, BaseViewHolder> {
//定义一个item的高度列表
List mHeightList;
/**
- 头部广告
*/
private String Top = “top”;
/**
- 底部广告
*/
private String Bottom = “bottom”;
public WallPaperAdapter(int layoutResId, @Nullable List<WallPaperResponse.ResBean.VerticalBean> data, List heightList) {
super(layoutResId, data);
this.mHeightList = heightList;
}
@Override
protected void convert(BaseViewHolder helper, WallPaperResponse.ResBean.VerticalBean item) {
ShapeableImageView imageView = helper.getView(R.id.iv_wallpaper);
//获取imageView的LayoutParams
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.height = dip2px(mHeightList.get(helper.getAdapterPosition()));
//重新设置imageView的高度
imageView.setLayoutParams(layoutParams);
if (Top.equals(item.getDesc()) || Bottom.equals(item.getDesc())) {
imageView.setImageResource(R.mipmap.icon_logo);
} else {
Glide.with(mContext).load(item.getImg()).into(imageView);
}
helper.addOnClickListener(R.id.item_wallpaper);
}
// dp 转成 px
private int dip2px(float dpVale) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpVale * scale + 0.5f);
}
}
适配器也写完了,下面改渲染页面了,对不对。回到WallPaperActivity
/**
- 标题
*/
@BindView(R.id.toolbar)
Toolbar toolbar;
/**
- 数据列表
*/
@BindView(R.id.rv)
RecyclerView rv;
/**
- AppBarLayout布局
*/
@BindView(R.id.appbar)
AppBarLayout appbar;
先绑定页面的控件,然后创建列表和适配器的对象
/**
- 壁纸数据列表
*/
private List<WallPaperResponse.ResBean.VerticalBean> mList = new ArrayList<>();
/**
- 壁纸数据适配器
*/
private WallPaperAdapter mAdapter;
/**
- item高度列表
*/
private List heightList = new ArrayList<>();
/**
- 壁纸数量
*/
private static final int WALLPAPER_NUM = 30;
/**
- 头部和底部的item数据
*/
private WallPaperResponse.ResBean.VerticalBean topBean, bottomBean;
/**
- 必应的每日壁纸
*/
private String biyingUrl = null;
新增一个初始化列表数据的方法。
/**
- 初始化列表数据
*/
private void initWallPaperList() {
heightList.add(100);
for (int i = 0; i < WALLPAPER_NUM; i++) {
heightList.add(300);
}
heightList.add(100);
mAdapter = new WallPaperAdapter(R.layout.item_wallpaper_list, mList, heightList);
//瀑布流
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//设置布局管理
rv.setLayoutManager(manager);
//设置数据适配器
rv.setAdapter(mAdapter);
//请求数据
mPresent.getWallPaper();
//获取必应壁纸
mPresent.biying();
mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
}
});
}
下面就该来处理返回的数据了。
/**
-
必应壁纸数据返回
-
@param response BiYingImgResponse
*/
@Override
public void getBiYingResult(Response response) {
if (response.body().getImages() != null) {
//得到的图片地址是没有前缀的,所以加上前缀否则显示不出来
biyingUrl = “http://cn.bing.com” + response.body().getImages().get(0).getUrl();
Log.d(“type–>”, biyingUrl);
} else {
ToastUtils.showShortToast(context, “未获取到必应的图片”);
}
}
/**
-
网络壁纸数据返回
-
@param response WallPaperResponse
*/
@Override
public void getWallPaperResult(Response response) {
if (response.body().getMsg().equals(Constant.SUCCESS)) {
List<WallPaperResponse.ResBean.VerticalBean> data = response.body().getRes().getVertical();
//创建头部和底部的两个广告item的假数据
topBean = new WallPaperResponse.ResBean.VerticalBean();
topBean.setDesc(“top”);
topBean.setImg(“”);
bottomBean = new WallPaperResponse.ResBean.VerticalBean();
bottomBean.setDesc(“bottom”);
bottomBean.setImg(“”);
//数据填充
if (data != null && data.size() > 0) {
mList.clear();
//添加头部
mList.add(topBean);
//添加主要数据
for (int i = 0; i < data.size(); i++) {
mList.add(data.get(i));
}
//添加尾部
mList.add(bottomBean);
Log.d(“list–>”, new Gson().toJson(mList));
//根据数据数量来刷新列表
mAdapter.notifyItemInserted(mList.size());
//删除数据库中的数据
LitePal.deleteAll(WallPaper.class);
for (int i = 0; i < mList.size(); i++) {
WallPaper wallPaper = new WallPaper();
wallPaper.setImgUrl(mList.get(i).getImg());
wallPaper.save();
}
dismissLoadingDialog();
} else {
ToastUtils.showShortToast(context, “壁纸数据为空”);
dismissLoadingDialog();
}
} else {
dismissLoadingDialog();
ToastUtils.showShortToast(context, “未获取到壁纸数据”);
}
}
@Override
public void getDataFailed() {
dismissLoadingDialog();
ToastUtils.showShortToast(context, “请求超时”);
}
Constant中增加一个
/**
- 成功
*/
public static final String SUCCESS = “success”;
你会发现网络壁纸的返回处理有些麻烦。不过注释都有了,应该看得懂。下面在mvplibrary中创建WallPaper.java
里面的代码很简单:
package com.llw.mvplibrary.bean;
import org.litepal.crud.LitePalSupport;
import java.util.List;
/**
-
壁纸表
-
@author llw
*/
public class WallPaper extends LitePalSupport {
private String ImgUrl;
public String getImgUrl() {
return ImgUrl;
}
public void setImgUrl(String imgUrl) {
ImgUrl = imgUrl;
}
}
然后改动assets下面的litepal.xml文件。
然后在WallPaperActivity的initData调用相关的方法。
@Override
public void initData(Bundle savedInstanceState) {
//加载弹窗
showLoadingDialog();
//高亮状态栏
StatusBarUtil.StatusBarLightMode(this);
//左上角的返回
Back(toolbar);
initWallPaperList();
}
下面请求就会有这个数据了,而且你上滑动就会有顶部的标题上隐藏的效果。
2. 浮动按钮的交互
下面加一个浮动按钮。在activity_wall_paper.xml中新增加一个
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=“@+id/fab_setting”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“end|bottom”
android:layout_margin=“@dimen/dp_20”
android:clickable=“true”
android:src=“@mipmap/icon_setting”
app:backgroundTint=“@color/white”
app:backgroundTintMode=“screen”
app:borderWidth=“@dimen/dp_0”
app:hoveredFocusedTranslationZ=“@dimen/dp_18”
app:pressedTranslationZ=“@dimen/dp_18”
app:rippleColor=“@color/blue_one” />
icon_setting的图标
然后在WallPaperActivity中新增
/**
- 底部浮动按钮
*/
@BindView(R.id.fab_setting)
FloatingActionButton fabSetting;
然后在initWallPaperList方法中新增如下代码:
//滑动监听
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy <= 0) {
fabSetting.show();
} else {//上滑
fabSetting.hide();
}
}
});
通过滑动RecyclerView对浮动按钮进行控制。当然浮动按钮要是光是显示和隐藏自然远远不行,浮动按钮点击之后要怎么样呢?
要出现一个底部弹窗,供你选择哪种方式的壁纸。
下面在app的layout下新建一个dialog_bottom_wallpaper_setting.xml,布局代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“@color/white”
android:orientation=“vertical”>
<LinearLayout
android:id=“@+id/lay_wallpaper_list”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:foreground=“?android:attr/selectableItemBackground”
android:gravity=“center”
android:orientation=“horizontal”
android:padding=“@dimen/dp_16”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“壁纸列表”
android:textColor=“@color/black_4”
android:textSize=“@dimen/sp_14” />
<ImageView
android:id=“@+id/iv_wallpaper_list”
android:layout_width=“@dimen/dp_24”
android:layout_height=“@dimen/dp_24”
android:src=“@mipmap/icon_selected”
android:visibility=“invisible” />
<View
android:layout_width=“match_parent”
android:layout_height=“1dp”
android:layout_marginLeft=“@dimen/dp_16”
android:layout_marginRight=“@dimen/dp_16”
android:background=“@color/gray_white_2” />
<LinearLayout
android:id=“@+id/lay_everyday_wallpaper”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:foreground=“?android:attr/selectableItemBackground”
android:gravity=“center”
android:orientation=“horizontal”
android:padding=“@dimen/dp_16”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“每日一图”
android:textColor=“@color/black_4”
android:textSize=“@dimen/sp_14” />
<ImageView
android:id=“@+id/iv_everyday_wallpaper”
android:layout_width=“@dimen/dp_24”
android:layout_height=“@dimen/dp_24”
android:src=“@mipmap/icon_selected”
android:visibility=“invisible” />
<View
android:layout_width=“match_parent”
android:layout_height=“1dp”
android:layout_marginLeft=“@dimen/dp_16”
android:layout_marginRight=“@dimen/dp_16”
android:background=“@color/gray_white_2” />
<LinearLayout
android:id=“@+id/lay_upload_wallpaper”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:foreground=“?android:attr/selectableItemBackground”
android:gravity=“center”
android:orientation=“horizontal”
android:padding=“@dimen/dp_16”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“手动上传”
android:textColor=“@color/black_4”
android:textSize=“@dimen/sp_14” />
<ImageView
android:id=“@+id/iv_upload_wallpaper”
android:layout_width=“@dimen/dp_24”
android:layout_height=“@dimen/dp_24”
android:src=“@mipmap/icon_selected”
android:visibility=“invisible” />
<View
android:layout_width=“match_parent”
android:layout_height=“1dp”
android:layout_marginLeft=“@dimen/dp_16”
android:layout_marginRight=“@dimen/dp_16”
android:background=“@color/gray_white_2” />
<LinearLayout
android:id=“@+id/lay_default_wallpaper”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:foreground=“?android:attr/selectableItemBackground”
android:gravity=“center”
android:orientation=“horizontal”
android:padding=“@dimen/dp_16”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“默认壁纸”
android:textColor=“@color/black_4”
android:textSize=“@dimen/sp_14” />
<ImageView
android:id=“@+id/iv_default_wallpaper”
android:layout_width=“@dimen/dp_24”
android:layout_height=“@dimen/dp_24”
android:src=“@mipmap/icon_selected”
android:visibility=“invisible” />
预览如下:
icon_selected图标:
下面来写这个弹窗。
回到WallPaperActivity。
初始化这个弹窗,注意这个导包是我自定义的,不是系统自带的。
import com.llw.mvplibrary.view.dialog.AlertDialog;
/**
- 底部弹窗
*/
AlertDialog bottomSettingDialog = null;
然后写一个方法用来显示弹窗以及里面的一些业务逻辑的处理。
/**
- 壁纸底部弹窗弹窗
*/
private void showSettingDialog(int type) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.addDefaultAnimation()//默认弹窗动画
.setCancelable(true)
.fromBottom(true)
//载入布局文件
.setContentView(R.layout.dialog_bottom_wallpaper_setting)
//设置弹窗宽高
.setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//壁纸列表
.setOnClickListener(R.id.lay_wallpaper_list, v -> {
Intent intent = new Intent(context, ImageActivity.class);
intent.putExtra(“position”, 0);
startActivity(intent);
bottomSettingDialog.dismiss();
//每日一图
}).setOnClickListener(R.id.lay_everyday_wallpaper, v -> {
ToastUtils.showShortToast(context, “使用每日一图”);
SPUtils.putString(Constant.WALLPAPER_URL, biyingUrl, context);
//壁纸列表
SPUtils.putInt(Constant.WALLPAPER_TYPE, 2, context);
bottomSettingDialog.dismiss();
//手动上传
}).setOnClickListener(R.id.lay_upload_wallpaper, v -> {
startActivityForResult(CameraUtils.getSelectPhotoIntent(), SELECT_PHOTO);
ToastUtils.showShortToast(context, “请选择图片”);
bottomSettingDialog.dismiss();
//默认壁纸
}).setOnClickListener(R.id.lay_default_wallpaper, v -> {
ToastUtils.showShortToast(context, “使用默认壁纸”);
SPUtils.putInt(Constant.WALLPAPER_TYPE, 4, context);//使用默认壁纸
SPUtils.putString(Constant.WALLPAPER_URL, null, context);
bottomSettingDialog.dismiss();
});
bottomSettingDialog = builder.create();
ImageView iv_wallpaper_list = (ImageView) bottomSettingDialog.getView(R.id.iv_wallpaper_list);
ImageView iv_everyday_wallpaper = (ImageView) bottomSettingDialog.getView(R.id.iv_everyday_wallpaper);
ImageView iv_upload_wallpaper = (ImageView) bottomSettingDialog.getView(R.id.iv_upload_wallpaper);
ImageView iv_default_wallpaper = (ImageView) bottomSettingDialog.getView(R.id.iv_default_wallpaper);
switch (type) {
//壁纸列表
case 1:
iv_wallpaper_list.setVisibility(View.VISIBLE);
break;
//每日一图
case 2:
iv_everyday_wallpaper.setVisibility(View.VISIBLE);
break;
//手动上传
case 3:
iv_upload_wallpaper.setVisibility(View.VISIBLE);
break;
//默认壁纸
case 4:
iv_default_wallpaper.setVisibility(View.VISIBLE);
break;
default:
iv_default_wallpaper.setVisibility(View.GONE);
break;
}
bottomSettingDialog.show();
//弹窗关闭监听
bottomSettingDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
fabSetting.show();
}
});
}
可以看到通过一个type来控制当前的壁纸属于那种模式,然后在弹窗关闭的时候显示浮动按钮,我在Constant中定义了两个变量,一个用于保存壁纸的状态,一个用于保存壁纸的地址值。
/**
- 壁纸地址
*/
public static final String WALLPAPER_URL = “wallpaperUrl”;
/**
- 壁纸类型 1 壁纸列表 2 每日一图 3 手动上传 4 默认壁纸
*/
public static final String WALLPAPER_TYPE = “wallpaperType”;
里面用到过一个工具类CameraUtils,代码如下:
package com.llw.goodweather.utils;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
/**
-
相机、相册工具类
-
@author llw
*/
public class CameraUtils {
public static Intent getTakePhotoIntent(Context context, File outputImagepath) {
//获取系統版本
int currentapiVersion = Build.VERSION.SDK_INT;
// 激活相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
if (currentapiVersion < 24) {
// 从文件中创建uri
Uri uri = Uri.fromFile(outputImagepath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} else {
//兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, outputImagepath.getAbsolutePath());
Uri uri = context.getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
}
return intent;
}
public static Intent getSelectPhotoIntent() {
Intent intent = new Intent(“android.intent.action.GET_CONTENT”);
intent.setType(“image/*”);
return intent;
}
/*
- 判断sdcard是否被挂载
*/
public static boolean hasSdcard() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
- 4.4及以上系统处理图片的方法
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getImgeOnKitKatPath(Intent data, Context context) {
String imagePath = null;
Uri uri = data.getData();
Log.d(“uri=intent.getData :”, “” + uri);
if (DocumentsContract.isDocumentUri(context, uri)) {
String docId = DocumentsContract.getDocumentId(uri); //数据表里指定的行
Log.d(“getDocumentId(uri) :”, “” + docId);
Log.d(“uri.getAuthority() :”, “” + uri.getAuthority());
if (“com.android.providers.media.documents”.equals(uri.getAuthority())) {
String id = docId.split(“:”)[1];
String selection = MediaStore.Images.Media._ID + “=” + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, context);
} else if (“com.android.providers.downloads.documents”.equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(“content://downloads/public_downloads”), Long.valueOf(docId));
imagePath = getImagePath(contentUri, null, context);
}
} else if (“content”.equalsIgnoreCase(uri.getScheme())) {
imagePath = getImagePath(uri, null, context);
}
return imagePath;
}
/**
- 通过uri和selection来获取真实的图片路径,从相册获取图片时要用
*/
public static String getImagePath(Uri uri, String selection, Context context) {
String path = null;
Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
//改变拍完照后图片方向不正的问题
public static void ImgUpdateDirection(String filepath, Bitmap orc_bitmap, ImageView iv) {
int digree = 0;//图片旋转的角度
//根据图片的URI获取图片的绝对路径
Log.i(“tag”, “>>>>>>>>>>>>>开始”);
//String filepath = ImgUriDoString.getRealFilePath(getApplicationContext(), uri);
Log.i(“tag”, “》》》》》》》》》》》》》》》” + filepath);
//根据图片的filepath获取到一个ExifInterface的对象
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
Log.i(“tag”, “exif》》》》》》》》》》》》》》》” + exif);
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021最新面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
//数据表里指定的行
Log.d(“getDocumentId(uri) :”, “” + docId);
Log.d(“uri.getAuthority() :”, “” + uri.getAuthority());
if (“com.android.providers.media.documents”.equals(uri.getAuthority())) {
String id = docId.split(“:”)[1];
String selection = MediaStore.Images.Media._ID + “=” + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, context);
} else if (“com.android.providers.downloads.documents”.equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(“content://downloads/public_downloads”), Long.valueOf(docId));
imagePath = getImagePath(contentUri, null, context);
}
} else if (“content”.equalsIgnoreCase(uri.getScheme())) {
imagePath = getImagePath(uri, null, context);
}
return imagePath;
}
/**
- 通过uri和selection来获取真实的图片路径,从相册获取图片时要用
*/
public static String getImagePath(Uri uri, String selection, Context context) {
String path = null;
Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
//改变拍完照后图片方向不正的问题
public static void ImgUpdateDirection(String filepath, Bitmap orc_bitmap, ImageView iv) {
int digree = 0;//图片旋转的角度
//根据图片的URI获取图片的绝对路径
Log.i(“tag”, “>>>>>>>>>>>>>开始”);
//String filepath = ImgUriDoString.getRealFilePath(getApplicationContext(), uri);
Log.i(“tag”, “》》》》》》》》》》》》》》》” + filepath);
//根据图片的filepath获取到一个ExifInterface的对象
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
Log.i(“tag”, “exif》》》》》》》》》》》》》》》” + exif);
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021最新面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
[外链图片转存中…(img-rjYcFOSr-1725996555590)]
[外链图片转存中…(img-O3Gk80pS-1725996555591)]
[外链图片转存中…(img-cvu1ks73-1725996555591)]
[外链图片转存中…(img-jXWou2K8-1725996555592)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。