在Android中设置壁纸

时间:2011-10-17 20:38:53

标签: android android-image

我在SD卡中有图像640 * 480,我想将其设置为主屏幕壁纸。如何以低质量损失实现这一目标?用我的代码壁纸有一个不太清晰。那是我的代码:

private void setWallpaper(String filePath) throws IOException{
    if(filePath!=null){
    // set options for decoding
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = false;   
    options.inJustDecodeBounds = true;
    //get bitmap from filepath
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    //get sizes of bitmap
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    //get sizes of screen and scale level
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int newWidth = display.getWidth()*2;
    int newHeight = display.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    //set matrix of the scaling
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    //get new bitmap for wallpaper
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            width, height, matrix, true);
    //set bitmap as wallpaper
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    wallpaperManager.clear();
    wallpaperManager.setBitmap(resizedBitmap);
    }
    }

1 个答案:

答案 0 :(得分:0)

任何时候你向上扩展你都会遭受质量损失。你想用什么尺寸的屏幕显示这个图像?请记住,Android壁纸尺寸是屏幕高度的两倍(例如800x480屏幕需要800x960图像)。

我的建议是拥有更大的图像并向下缩放。下采样的质量损失远不如升级。如果空间不是问题,您可以为不同的屏幕分辨率包含多种尺寸的图像。