[英]Set background wallpaper from a drawable
在我的應用中,我需要獲取設備的當前牆紙:
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
不,問題在於此操作將導致UI在獲取背景時稍有滯后,此外,我需要將其設置為應用程序的背景,我已經嘗試過:
//Drawable Wallpaper defined already...
new Thread(new Runnable() {
public void run() {
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
}
}).start();
if (Wallpaper == null)
{
//Resources res = getResources();
//Drawable drawable1 = res.getDrawable(R.drawable.bg1);
//getWindow().setBackgroundDrawable(drawable1);
}
else
{
Wallpaper.setAlpha(50);
getWindow().setBackgroundDrawable(Wallpaper);
}
//........
但這沒用,有什么主意嗎? 如果可能的話,請提供代碼,我還是android的新手。此外,還有更好的方法嗎?
請嘗試以下操作,看看是否有幫助。 我已注釋了代碼以供您擴展(如果需要)。
private class SetWallpaperTask extends AsyncTask<Void, Void, Void> {
Drawable wallpaperDrawable;
@Override
protected void onPreExecute() {
// Runs on the UI thread
// Do any pre-executing tasks here, for example display a progress bar
Log.d(TAG, "About to set wallpaper...");
}
@Override
protected Void doInBackground(Void... params) {
// Runs on the background thread
WallpaperManager wallpaperManager = WallpaperManager.getInstance
(getApplicationContext());
wallpaperDrawable = wallpaperManager.getDrawable();
}
@Override
protected void onPostExecute(Void res) {
// Runs on the UI thread
// Here you can perform any post-execute tasks, for example remove the
// progress bar (if you set one).
if (wallpaperDrawable != null) {
wallpaperDrawable.setAlpha(50);
getWindow().setBackgroundDrawable(wallpaperDrawable);
Log.d(TAG, "New wallpaper set");
} else {
Log.d(TAG, "Wallpaper was null");
}
}
}
並執行此(后台)任務:
SetWallpaperTask t = new SetWallpaperTask();
t.execute();
如果仍然遇到問題,建議您遍歷SetWallpaperActivity.java示例並嘗試復制該示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.