標籤:set row bitmap start throws blog over main tor
別忘記在ApplicationManifest.xml 中加上許可權的設定。
<uses-permission Android:name = "android.permission.SET_WALLPAPER"/>
壁紙設定方法有三種:
第一 通過WallpaperManager方法中的 setBitmap()
第二 通過WallpaperManager方法中的 setResource()
第三 通過ContextWrapper 類中提供的setWallpaper()方法
由於 Activity 繼承ContextThemeWrapper ,ContextThemeWrapper繼承 ContextWrapper.
<1>通過執行個體化WallpaperManager 類調用單例類中setBitmap()方法。
[java] view plain copy
- package xiaosi.Wallpaper;
-
- import java.io.IOException;
-
- import android.app.Activity;
- import android.app.WallpaperManager;
- import android.content.res.Resources;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.widget.Toast;
-
- public class WallpaperActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
- Resources res = getResources();
- Bitmap bitmap=BitmapFactory.decodeResource(res,R.drawable.h);
- try
- {
- wallpaperManager.setBitmap(bitmap);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
第二種方法:通過WallpaperManager方法中的 setResource()
[java] view plain copy
- WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
- try {
- wallpaperManager.setResource(getResources().getIdentifier("wallpaper" + imagePosition, "drawable", "com.ch"));
- Toast.makeText(this, "設定成功", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
第三種方法:通過ContextWrapper 類中提供的setWallpaper()方法
[java] view plain copy
- //重寫ContextWrapper中setWallpaper()方法
- public void setWallpaper(InputStream paramInputStream) throws IOException {
- super.setWallpaper(paramInputStream);
- Toast.makeText(this, "設定成功", 1).show();
- }
-
- //設定壁紙代碼
- Resources localResources = getBaseContext().getResources();
- InputStream localInputStream2 = localResources
- .openRawResource(getResources().getIdentifier(
- "wallpaper" + imagePosition, "drawable", "com.ch"));
- try {
- setWallpaper(localInputStream2);
- } catch (IOException e) {
- e.printStackTrace();
- }
Android學習筆記進階21之設定壁紙