2011-10-17 129 views
0

我有圖像在SD卡640 * 480,我想將其設置爲主屏幕壁紙。我如何以小質量損失來做到這一點?隨着我的代碼壁紙有一個不好的清晰度。這是我的代碼:設置在Android壁紙

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); 
    } 
    } 

回答

0

任何時候你擴大規模你會遭受質量損失。你想要顯示這個圖像的尺寸是多少?請記住,Android壁紙尺寸是屏幕高度的兩倍(例如800x480屏幕需要800x960圖像)。

我的建議是將圖像放大並向下縮放。下采樣造成的質量損失遠不如從上採樣那麼明顯。如果空間不是問題,您可以將多種尺寸的圖像包含在不同的屏幕分辨率中。

+0

com.coolaris.media當我將此圖片設置爲牆紙時,以高質量和清晰度將圖片放大。但我不知道如何在我的程序中使用這個包。 –