Android編程之手機壁紙WallPaper設(shè)置方法示例
本文實例講述了Android編程之手機壁紙WallPaper設(shè)置方法。分享給大家供大家參考,具體如下:
/**
* Andorid設(shè)置手機屏幕的壁紙
*
* @description:
* @author ldm
* @date 2016-5-4 下午3:08:56
*/
public class SetWallpaperActivity extends Activity {
// WallpaperManager類:系統(tǒng)壁紙管理。通過它可以獲得當前壁紙以及設(shè)置指定圖片作為系統(tǒng)壁紙。
private WallpaperManager wallpaperManager;
// 壁紙對應的Drawable
private Drawable wallpaperDrawable;
// 展示樣式的ImageView
private ImageView imageView;
// 隨機生成圖片的顏色 Button
private Button randomize;
// 設(shè)置壁紙
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)壁紙設(shè)置顏色
wallpaperDrawable.setColorFilter(mColors[mColor],
PorterDuff.Mode.MULTIPLY);// 取兩層繪制交集
imageView.setImageDrawable(wallpaperDrawable);
// imageView.invalidate();
}
});
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {
// 設(shè)置壁紙
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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android實現(xiàn)編程修改手機靜態(tài)IP的方法
這篇文章主要介紹了Android實現(xiàn)編程修改手機靜態(tài)IP的方法,涉及Android編程實現(xiàn)對系統(tǒng)底層信息修改的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
實時獲取股票數(shù)據(jù)的android app應用程序源碼分享
本文我們分享一個實時獲取股票數(shù)據(jù)的android app應用程序源碼分享,可以作為學習使用,本文貼出部分重要代碼,需要的朋友可以參考下本文2015-09-09
ubuntu用wifi連接android調(diào)試程序的步驟
這篇文章主要介紹了ubuntu用wifi連接android調(diào)試程序的步驟,需要的朋友可以參考下2014-02-02
Android handle-message的發(fā)送與處理案例詳解
這篇文章主要介紹了Android handle-message的發(fā)送與處理案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
Android之rk3588?開發(fā)環(huán)境準備及問題解決方法
這篇文章主要介紹了Android中的rk3588?開發(fā)環(huán)境準備,本文給大家分享遇到的問題及解決方法,本文給大家講解的非常詳細對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
Android回調(diào)與觀察者模式的實現(xiàn)原理
這篇文章主要為大家詳細介紹了Android回調(diào)與觀察者模式的實現(xiàn)原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android?MaterialAlertDialogBuilder修改按鈕屬性
這篇文章主要介紹了Android?MaterialAlertDialogBuilder修改按鈕屬性實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

