5

我正在开发一个应用程序,它从图库中选择一张图片,然后将该图片设置为壁纸。但这里的问题是只有部分图像设置为墙纸而不是整个图像,但我想将整个图像设置为墙纸。你能告诉我怎么做吗???

这是我的代码...

public class Scaleimage extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String FileName;
        File file = new File("/sdcard/pictures");   
        File[] imageFiles = file.listFiles( );
        if(imageFiles.length > 0 ) {
            FileName = imageFiles[0].getName();
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());   
        Bitmap myBitmap =  BitmapFactory.decodeFile("/sdcard/pictures" + "/" + FileName); 

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels << 1;
        myBitmap = Bitmap.createScaledBitmap(myBitmap,width, height, true);
        try {
            wallpaperManager.setBitmap( myBitmap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}
4

2 回答 2

7

将壁纸尺寸设置为您的图像尺寸:

WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
wm.setBitmap(bitmap);
wm.suggestDesiredDimensions(w, h);

并记得添加权限:

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
于 2012-07-10T10:26:36.260 回答
1

像这样将宽度高度传递给壁纸管理器:

final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(
                Context.WALLPAPER_SERVICE);    

Bitmap myBitmap = Bitmap.createScaledBitmap(Const.cropped_bitmap, wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), true);
wallpaperManager.suggestDesiredDimensions(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());

try {

    wallpaperManager.setBitmap(myBitmap);
    Toast.makeText(CropImageActivity.this, CropImageActivity.this.getString(R.string.wallpaper_has_been_set), 0).show();
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(CropImageActivity.this, "Wallpaper not set", 0).show();
}

不要忘记添加权限:

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
于 2016-06-05T09:57:12.143 回答