今天就跟大家聊聊有關Android中怎么使用WallPaper設置壁紙,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

具體如下:
public class SetWallpaperActivity extends Activity {
// WallpaperManager類:系統(tǒng)壁紙管理。通過它可以獲得當前壁紙以及設置指定圖片作為系統(tǒng)壁紙。
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();// 獲得當前系統(tǒng)的壁紙
initViews();
initListeners();
}
private void initListeners() {
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
// 給當前系統(tǒng)壁紙設置顏色
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設置壁紙有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝大家的支持。
網頁名稱:Android中怎么使用WallPaper設置壁紙-創(chuàng)新互聯
分享地址:http://chinadenli.net/article32/djoosc.html
成都網站建設公司_創(chuàng)新互聯,為您提供全網營銷推廣、微信公眾號、網站設計、域名注冊、外貿建站、ChatGPT
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯