5

Android设置手机壁纸(WallPaper)

 3 years ago
source link: http://www.androidchina.net/4844.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Android设置手机壁纸(WallPaper) – Android开发中文站

Android设置手机壁纸

/**
* Andorid设置手机屏幕的壁纸
*
* @description:
* @author ldm
* @date 2016-5-4 下午3:08:56
*/
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);
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK