Android中如何使用WallPaper设置壁纸

发布时间:2022-04-08 16:09:10 作者:iii
来源:亿速云 阅读:383

本篇内容主要讲解“Android中如何使用WallPaper设置壁纸”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中如何使用WallPaper设置壁纸”吧!

具体代码如下:

public class SetWallpaperActivity extends Activity {
  // WallpaperManager类:系统壁纸管理。通过它可以获得当前壁纸以及设置指定图片作为系统壁纸。
  private WallpaperManager wallpaperManager;
  // 壁纸对应的Drawable
  private Drawable wallpaperDrawable;
  // 展示样式的ImageView
  private ImageView imageView;
  // 随机生成图片的颜色 Button
  private Button randomize;
  // 设置壁纸
  private Button setWallpaper;
  // 暂定的一些颜色值
  final static private int[] mColors = { Color.BLUE, Color.GREEN, Color.RED,
      Color.LTGRAY, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set_wallpaper);
    // 初始化WallpaperManager
    wallpaperManager = WallpaperManager.getInstance(this);
    wallpaperDrawable = wallpaperManager.getDrawable();// 获得当前系统的壁纸
    initViews();
    initListeners();
  }
  private void initListeners() {
    randomize.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        int mColor = (int) Math.floor(Math.random() * mColors.length);
        // 给当前系统壁纸设置颜色
        wallpaperDrawable.setColorFilter(mColors[mColor],
            PorterDuff.Mode.MULTIPLY);// 取两层绘制交集
        imageView.setImageDrawable(wallpaperDrawable);
        // imageView.invalidate();
      }
    });
    setWallpaper.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        try {
          // 设置壁纸
          wallpaperManager.setBitmap(imageView.getDrawingCache());
          finish();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
  }
  private void initViews() {
    imageView = (ImageView) findViewById(R.id.imageview);
    imageView.setDrawingCacheEnabled(true);
    imageView.setImageDrawable(wallpaperDrawable);
    randomize = (Button) findViewById(R.id.randomize);
    setWallpaper = (Button) findViewById(R.id.setwallpaper);
  }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview" />
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <Button
      android:id="@+id/randomize"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/randomize"
      android:layout_gravity="bottom" />
    <Button
      android:id="@+id/setwallpaper"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/set_wallpaper"
      android:layout_gravity="bottom" />
  </LinearLayout>
</FrameLayout>

到此,相信大家对“Android中如何使用WallPaper设置壁纸”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Android仿主流壁纸App设置界面
  2. Android怎么使用线程更换壁纸

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android wallpaper

上一篇:Android中如何实现底图局部加载移动

下一篇:Android中怎么使用照相机

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》