十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“Android中如何使用WallPaper设置壁纸”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中如何使用WallPaper设置壁纸”吧!

成都创新互联自2013年起,公司以网站设计、网站制作、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户超过千家,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。
具体代码如下:
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设置壁纸”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!