网站建设资讯

NEWS

网站建设资讯

Android中如何使用WallPaper设置壁纸-创新互联

本篇内容主要讲解“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);
  }
}

布局文件:



  
  
    

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


网页题目:Android中如何使用WallPaper设置壁纸-创新互联
网页网址:http://cdweb.net/article/ddpchd.html