0

如何设置图像壁纸全图像背景屏幕。我的代码

Bitmap bmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.splash);

            DisplayMetrics outMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
            int w = outMetrics.widthPixels;
            int h = outMetrics.heightPixels;
            Bitmap wallpaper=Bitmap.createScaledBitmap(bmap, w, h, true);

                            WallpaperManager m = WallpaperManager.getInstance(getApplicationContext());
            try {
                m.setBitmap(wallpaper);
            } catch (IOException e) {
                e.printStackTrace();
            }

图片

图片

我需要图片全套壁纸

4

2 回答 2

1
public void changeWallpaper(String path) {
   FileInputStream is;
         BufferedInputStream bis;
                WallpaperManager wallpaperManager;
                Drawable wallpaperDrawable;
                File sdcard = Environment.getExternalStorageDirectory();
                try {
                        is = new FileInputStream(new File(path));
                        bis = new BufferedInputStream(is);
                        Bitmap bitmap = BitmapFactory.decodeStream(bis);
                        Bitmap useThisBitmap = Bitmap.createBitmap(bitmap);
                        wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
                        wallpaperDrawable = wallpaperManager.getDrawable();
                        wallpaperManager.setBitmap(useThisBitmap);
                } catch (Exception e) {
                        e.printStackTrace();
                }
    }

上面的代码对我有用并且不要忘记添加

<uses-permission android:name="android.permission.SET_WALLPAPER" />

在 AndroidManifest.xml 文件中

您也可以将您的可绘制对象转换为位图

BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_name);
于 2013-10-31T10:57:02.373 回答
0

您可以通过启动结果活动来启动裁剪意图并在结果中检索它,然后使用壁纸管理器类。像这样 :

Uri imgUri=Uri.parse("android.resource://your.package.name/"+R.drawable.image); 
Intent intent = new Intent("com.android.camera.action.CROP");  
intent.setDataAndType(imgUri, "image/*");  
intent.putExtra("crop", "true");  
intent.putExtra("aspectX", 1);  
intent.putExtra("aspectY", 1);  
intent.putExtra("outputX", 80);  
intent.putExtra("outputY", 80);  
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO);

并在 onResult 函数中使用壁纸管理器

于 2013-10-31T10:59:08.120 回答