最新Android 天气APP(二十九)壁纸设置、图片查看、图片保存,2024年最新flutter弹窗页面缩小

最后

针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

  • Android前沿技术大纲

  • 全套体系化高级架构视频

Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、混合式开发(ReactNative+Weex)全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

/**

  • 壁纸数据列表

*/

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;

break;

case ExifInterface.ORIENTATION_ROTATE_180:

digree = 180;

break;

case ExifInterface.ORIENTATION_ROTATE_270:

digree = 270;

break;

default:

digree = 0;

break;

}

}

//如果图片不为0

if (digree != 0) {

// 旋转图片

Matrix m = new Matrix();

m.postRotate(digree);

orc_bitmap = Bitmap.createBitmap(orc_bitmap, 0, 0, orc_bitmap.getWidth(),

orc_bitmap.getHeight(), m, true);

}

if (orc_bitmap != null) {

iv.setImageBitmap(orc_bitmap);

}

} catch (IOException e) {

e.printStackTrace();

exif = null;

}

}

/**

  • 4.4以下系统处理图片的方法

*/

public static String getImageBeforeKitKatPath(Intent data, Context context) {

Uri uri = data.getData();

String imagePath = getImagePath(uri, null, context);

return imagePath;

}

//比例压缩

public static Bitmap comp(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG, 100, baos);

if (baos.toByteArray().length / 1024 > 5120) {//判断如果图片大于5M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中

}

ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());

BitmapFactory.Options newOpts = new BitmapFactory.Options();

//开始读入图片,此时把options.inJustDecodeBounds 设回true了

newOpts.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

newOpts.inJustDecodeBounds = false;

int w = newOpts.outWidth;

int h = newOpts.outHeight;

//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

float hh = 800f;//这里设置高度为800f

float ww = 480f;//这里设置宽度为480f

//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

int be = 1;//be=1表示不缩放

if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放

be = (int) (newOpts.outWidth / ww);

} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放

be = (int) (newOpts.outHeight / hh);

}

if (be <= 0)

be = 1;

newOpts.inSampleSize = be;//设置缩放比例

newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565

//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

isBm = new ByteArrayInputStream(baos.toByteArray());

bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

return bitmap;//压缩好比例大小后再进行质量压缩

}

}

因为第三个是手动上传,所以会打开你的本地相册,当你选择一个图片之后,需要拿到返回的数据。所以要重写onActivityResult,在这个方法里面获取到图片的路径,然后放到缓存里,这时候你的壁纸类型就是手动上传的壁纸了。

/**

  • Activity返回结果

  • @param requestCode

  • @param resultCode

  • @param data

*/

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

//打开相册后返回

case SELECT_PHOTO:

if (resultCode == RESULT_OK) {

String imagePath = null;

//判断手机系统版本号

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

//4.4及以上系统使用这个方法处理图片

imagePath = CameraUtils.getImgeOnKitKatPath(data, this);

} else {

imagePath = CameraUtils.getImageBeforeKitKatPath(data, this);

}

displayImage(imagePath);

}

Log.d(“result–>”, requestCode + " " + resultCode + " " + data.getData().toString());

break;

default:

Log.d(“result–>”, requestCode + " " + resultCode + " " + data.getData().toString());

break;

}

}

/**

  • 从相册获取完图片(根据图片路径显示图片)

*/

private void displayImage(String imagePath) {

if (!TextUtils.isEmpty(imagePath)) {

//将本地上传选中的图片地址放入缓存,当手动定义开关打开时,取出缓存中的图片地址,显示为背景

SPUtils.putInt(Constant.WALLPAPER_TYPE, 3, context);

SPUtils.putString(Constant.WALLPAPER_URL, imagePath, context);

ToastUtils.showShortToast(context, “已更换为你选择的图片”);

} else {

SPUtils.putInt(Constant.WALLPAPER_TYPE, 0, context);

ToastUtils.showShortToast(context, “图片获取失败”);

}

}

至于默认壁纸,只要壁纸类型改为4,然后清空缓存中壁纸地址就可以了。因为这个地址是MainActivity中用来显示背景的依据,没有了就会显示默认背景。

至于第二个每日一图,就是在点击的时候把通过结果返回的地址拼接之后,再放入缓存中。同样指定类型。当然最头痛的是这个壁纸列表,首先在当前页面我们已经可以看到这个壁纸列表数据了。那么我们可以通过点击item的时候跳转到查看该壁纸完整的页面。所以需要创建一个ImageActivity,在app的UI包下创建。

布局的代码如下:

<?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”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“@color/white”

tools:context=“.ui.ImageActivity”>

<androidx.viewpager2.widget.ViewPager2

android:id=“@+id/vp”

android:layout_width=“match_parent”

android:layout_height=“match_parent”/>

<ImageView

android:id=“@+id/iv_back”

android:layout_width=“@dimen/dp_50”

android:layout_height=“@dimen/dp_50”

android:layout_marginLeft=“@dimen/dp_12”

android:layout_marginTop=“@dimen/dp_30”

android:padding=“@dimen/dp_8”

android:background=“@drawable/selector_bg_img”

android:src=“@mipmap/icon_image_return_white” />

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:layout_centerHorizontal=“true”

android:layout_marginBottom=“@dimen/dp_12”

android:gravity=“center”

android:orientation=“horizontal”>

<com.google.android.material.button.MaterialButton

android:id=“@+id/btn_setting_wallpaper”

style=“@style/Widget.MaterialComponents.Button.UnelevatedButton”

android:layout_width=“@dimen/dp_100”

android:layout_height=“@dimen/dp_32”

android:layout_marginRight=“@dimen/dp_6”

android:insetTop=“@dimen/dp_0”

android:insetBottom=“@dimen/dp_0”

android:text=“设为壁纸”

android:theme=“@style/Theme.MaterialComponents.Light.NoActionBar”

app:backgroundTint=“@color/white_2”

app:cornerRadius=“@dimen/dp_16”

app:strokeColor=“@color/white”

app:strokeWidth=“@dimen/dp_1” />

<com.google.android.material.button.MaterialButton

android:id=“@+id/btn_download”

style=“@style/Widget.MaterialComponents.Button.UnelevatedButton”

android:layout_width=“@dimen/dp_100”

android:layout_height=“@dimen/dp_32”

android:layout_marginLeft=“@dimen/dp_6”

android:insetTop=“@dimen/dp_0”

android:insetBottom=“@dimen/dp_0”

android:text=“下载壁纸”

android:theme=“@style/Theme.MaterialComponents.Light.NoActionBar”

app:backgroundTint=“@color/about_bg_color”

app:cornerRadius=“@dimen/dp_16” />

因为布局用的是ViewPager2。它可以直接传RecyclerView.Adapter进去,让我们希望的是,进入之后可以左右滑动查看壁纸,所以用到这个,那么怎么实现呢?首先需要展示的item布局。在app下的layout中创建一个item_image_list.xml文件,里面的布局代码如下:

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.material.imageview.ShapeableImageView xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/wallpaper”

android:scaleType=“centerCrop”

android:layout_width=“match_parent”

android:layout_height=“match_parent”/>

然后在ImageActivity中,需要继承BaseActivity。里面的完整代码如下:

package com.llw.goodweather.ui;

import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.util.Log;

import android.view.View;

import android.widget.ImageView;

import androidx.annotation.Nullable;

import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;

import com.chad.library.adapter.base.BaseQuickAdapter;

import com.chad.library.adapter.base.BaseViewHolder;

import com.google.android.material.button.MaterialButton;

import com.google.gson.Gson;

import com.llw.goodweather.R;

import com.llw.goodweather.utils.Constant;

import com.llw.goodweather.utils.SPUtils;

import com.llw.goodweather.utils.StatusBarUtil;

import com.llw.goodweather.utils.ToastUtils;

import com.llw.mvplibrary.base.BaseActivity;

import com.llw.mvplibrary.bean.WallPaper;

import org.litepal.LitePal;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import butterknife.BindView;

import butterknife.OnClick;

/**

  • 查看图片

  • @author llw

*/

public class ImageActivity extends BaseActivity {

@BindView(R.id.iv_back)

ImageView ivBack;

@BindView(R.id.btn_setting_wallpaper)

MaterialButton btnSettingWallpaper;

@BindView(R.id.btn_download)

MaterialButton btnDownload;

@BindView(R.id.vp)

ViewPager2 vp;

List mList = new ArrayList<>();

WallPaperAdapter mAdapter;

String wallpaperUrl = null;

private int position;

private Bitmap bitmap;

@Override

public void initData(Bundle savedInstanceState) {

showLoadingDialog();

//透明状态栏

StatusBarUtil.transparencyBar(context);

//获取位置

position = getIntent().getIntExtra(“position”, 0);

//获取数据

mList = LitePal.findAll(WallPaper.class);

Log.d(“list–>”, “” + mList.size());

if (mList != null && mList.size() > 0) {

for (int i = 0; i < mList.size(); i++) {

if (mList.get(i).getImgUrl().equals(“”)) {

mList.remove(i);

}

}

}

Log.d(“list–>”, “” + mList.size());

//RecyclerView实现方式

mAdapter = new WallPaperAdapter(R.layout.item_image_list, mList);

Log.d(“wallPaper”, new Gson().toJson(mList));

//ViewPager2实现方式

vp.setAdapter(mAdapter);

vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

@Override

public void onPageSelected(int position) {

Log.d(“position–>”, “” + position);

wallpaperUrl = mList.get(position).getImgUrl();

bitmap = getBitMap(wallpaperUrl);

}

});

mAdapter.notifyDataSetChanged();

vp.setCurrentItem(position, false);

dismissLoadingDialog();

}

@Override

public int getLayoutId() {

return R.layout.activity_image;

}

@OnClick({R.id.iv_back, R.id.btn_setting_wallpaper, R.id.btn_download})

public void onViewClicked(View view) {

switch (view.getId()) {

case R.id.iv_back:

finish();

break;

//设置壁纸

case R.id.btn_setting_wallpaper:

//放入缓存

SPUtils.putString(Constant.WALLPAPER_URL, wallpaperUrl, context);

//壁纸列表

SPUtils.putInt(Constant.WALLPAPER_TYPE, 1, context);

ToastUtils.showShortToast(context, “已设置”);

break;

//下载壁纸

case R.id.btn_download:

saveImageToGallery(context, bitmap);

break;

default:

break;

}

}

/**

  • 壁纸适配器

*/

public class WallPaperAdapter extends BaseQuickAdapter<WallPaper, BaseViewHolder> {

public WallPaperAdapter(int layoutResId, @Nullable List data) {

super(layoutResId, data);

}

@Override

protected void convert(BaseViewHolder helper, WallPaper item) {

ImageView imageView = helper.getView(R.id.wallpaper);

Glide.with(mContext).load(item.getImgUrl()).into(imageView);

}

}

/**

  • 保存图片到本地相册

  • @param context 上下文

  • @param bitmap bitmap

  • @return

*/

public boolean saveImageToGallery(Context context, Bitmap bitmap) {

// 首先保存图片

String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + “good_weather”;

File appDir = new File(filePath);

if (!appDir.exists()) {

appDir.mkdir();

}

String fileName = “wallpaper” + 1024 + “.jpg”;

File file = new File(appDir, fileName);

try {

FileOutputStream fos = new FileOutputStream(file);

//通过io流的方式来压缩保存图片

boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 60, fos);

fos.flush();

fos.close();

//把文件插入到系统图库

MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

//保存图片后发送广播通知更新数据库

Uri uri = Uri.fromFile(file);

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

if (isSuccess) {

ToastUtils.showShortToast(context, “图片保存成功”);

return true;

} else {

ToastUtils.showShortToast(context, “图片保存失败”);

return false;

}

} catch (IOException e) {

e.printStackTrace();

}

ToastUtils.showShortToast(context, “图片保存失败”);

return false;

}

/**

  • Url转Bitmap

  • @param url

  • @return

*/

public Bitmap getBitMap(final String url) {

//新启一个线程进行转换

new Thread(new Runnable() {

@Override

public void run() {

URL imageurl = null;

try {

imageurl = new URL(url);

} catch (MalformedURLException e) {

e.printStackTrace();

}

try {

HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();

conn.setDoInput(true);

conn.connect();

InputStream inputStream = conn.getInputStream();

bitmap = BitmapFactory.decodeStream(inputStream);

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

return bitmap;

}

}

下面讲一下这个页面的业务逻辑。从WallPaperActivity中点击item活着是点击底部弹窗的时候传递点击的position过来。然后查询数据库中的壁纸表,移除掉ImgUrl为 “”的数据,因为这个数据是我手动加上去的,然后就是设置适配器给ViewPager2,滑动的时候获取选中页的图片地址,并将地址转成bitmap,然后当你点击底部的下载壁纸的时候,通过保存当前的bitmap到手机本地,页面的逻辑就讲完了。下面回到WallPaperActivity,在里面添加item的点击后的业务处理。

在这里插入图片描述

还差一步,那就是浮动按钮的点击事件没有写。

@OnClick(R.id.fab_setting)

public void onViewClicked() {

fabSetting.hide();

int type = SPUtils.getInt(Constant.WALLPAPER_TYPE, 4, context);

showSettingDialog(type);

}

那么到这里页面的代码就差不多写完了。累死我了,写完代码写博客,说真的写博客比写代码更累。下面就是回到MainActivity中去做壁纸的显示处理。在onResume方法中

在这里插入图片描述

方法代码如下:

/**

  • 更换壁纸

*/

private void updateWallpaper() {

String imgUrl = SPUtils.getString(Constant.WALLPAPER_URL, null, context);

if (imgUrl != null) {

Glide.with(context).load(imgUrl).into(bg);

} else {

Glide.with(context).load(R.drawable.img_5).into(bg);

}

}

OK,代码就写完了。

3. 其他优化

我记得我之前说过有空就要优化那个搜索城市页面的弹窗,之前用的原生的比较的丑。所以现在自己创建了一个布局,在app中的layout下创建一个dialog_tip.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:background=“@drawable/shape_white_5”

android:layout_width=“@dimen/dp_270”

android:layout_height=“wrap_content”>

<TextView

android:id=“@+id/tv_title”

android:text=“提示”

android:padding=“@dimen/dp_8”

android:gravity=“center”

android:textSize=“@dimen/sp_16”

android:textColor=“@color/black_4”

android:layout_width=“match_parent”

android:layout_height=“@dimen/dp_48”/>

<TextView

android:id=“@+id/tv_content”

android:text=“内容”

android:padding=“@dimen/dp_20”

android:gravity=“center”

android:textSize=“@dimen/sp_14”

android:textColor=“@color/black_4”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”/>

<View

android:background=“@color/gray”

android:layout_width=“match_parent”

android:layout_height=“0.3dp”/>

<LinearLayout

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<TextView

android:id=“@+id/tv_cancel”

android:text=“取消”

写在最后

在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。

如果你觉得自己学习效率低,缺乏正确的指导,可以一起学习交流!

加入我们吧!群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

nimg.cn/20200922150245427.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4NDM2MjE0,size_16,color_FFFFFF,t_70#pic_center)

方法代码如下:

/**

  • 更换壁纸

*/

private void updateWallpaper() {

String imgUrl = SPUtils.getString(Constant.WALLPAPER_URL, null, context);

if (imgUrl != null) {

Glide.with(context).load(imgUrl).into(bg);

} else {

Glide.with(context).load(R.drawable.img_5).into(bg);

}

}

OK,代码就写完了。

3. 其他优化

我记得我之前说过有空就要优化那个搜索城市页面的弹窗,之前用的原生的比较的丑。所以现在自己创建了一个布局,在app中的layout下创建一个dialog_tip.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:background=“@drawable/shape_white_5”

android:layout_width=“@dimen/dp_270”

android:layout_height=“wrap_content”>

<TextView

android:id=“@+id/tv_title”

android:text=“提示”

android:padding=“@dimen/dp_8”

android:gravity=“center”

android:textSize=“@dimen/sp_16”

android:textColor=“@color/black_4”

android:layout_width=“match_parent”

android:layout_height=“@dimen/dp_48”/>

<TextView

android:id=“@+id/tv_content”

android:text=“内容”

android:padding=“@dimen/dp_20”

android:gravity=“center”

android:textSize=“@dimen/sp_14”

android:textColor=“@color/black_4”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”/>

<View

android:background=“@color/gray”

android:layout_width=“match_parent”

android:layout_height=“0.3dp”/>

<LinearLayout

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<TextView

android:id=“@+id/tv_cancel”

android:text=“取消”

写在最后

在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。

如果你觉得自己学习效率低,缺乏正确的指导,可以一起学习交流!

加入我们吧!群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android设置桌面壁纸及恢复默认墙纸,程序中将创建一个存储壁纸图片资源的id数组,定义被选中的图片在id数组中的索引,需要自定义一个BaseAdapter,然后:   ImageView iv = new ImageView(Sample_12_2.this);//新建一个ImageView   iv.setBackgroundResource(imgIds[position]);//设置ImageView的背景图片   iv.setScaleType(ImageView.ScaleType.CENTER_CROP);   iv.setLayoutParams(new Gallery.LayoutParams(120, 120));//设置相框中元素的大小   将设置壁纸和恢复壁纸的功能写入按钮监听事件中,通过单击按钮来激活这两个功能:   设置ImageView为当前墙纸:   Button btnGetWall = (Button)findViewById(R.id.getWall);//获得Button对象   btnGetWall.setOnClickListener(new View.OnClickListener() {//为Button添加OnClickListener监听器   @Override   public void onClick(View v) {    ImageView iv = (ImageView)findViewById(R.id.currWall);    iv.setBackgroundDrawable(getWallpaper()); //设置ImageView显示的内容为当前墙纸   }   恢复默认的壁纸:   setContentView(R.layout.main);//设置当前屏幕   Button btnClearWall = (Button)findViewById(R.id.clearWall);//获得Button对象   btnClearWall.setOnClickListener(new View.OnClickListener() {//添加OnClickListener监听器   @Override   public void onClick(View v) {//重写onClick方法   try {    Sample_12_2.this.clearWallpaper();//还原手机壁纸   } catch (IOException e) {//捕获并打印异常    e.printStackTrace();   }
Flutter 弹窗是一种用于在应用程序中显示提示、确认或自定义内容的界面元素。常见的 Flutter 弹窗包括 AlertDialog、CupertinoAlertDialog、SimpleDialog 等。 其中,AlertDialog 提示框是最常用的一种,可以通过 showDialog 方法来显示。可以根据需要设置参数,如 barrierDismissible、barrierColor、barrierLabel 和 anchorPoint 等,来控制弹窗的行为和样式。 除了内置的弹窗类型,我们还可以自定义一个 Dialog,并根据需要添加动画效果,如 GIF 图片等。这样可以实现更加个性化的弹窗效果。 总结来说,Flutter 弹窗是应用程序中常用的界面元素,可以通过内置的弹窗类型或自定义的方式来实现,可以根据需要设置参数来控制弹窗的行为和样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Flutter之自定义Dialog实现版本更新弹窗功能的实现](https://download.csdn.net/download/weixin_38746926/12722459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【Flutter】【弹窗弹窗的快速上手使用和自定义Dialog](https://blog.csdn.net/weixin_43444734/article/details/127481395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值