2011-10-12 99 views
1

我想缩放壁纸设置的位图但没有影响:我有这样的代码,并在原文件夹和阵列中的所有.jpg文件,代码:位图到壁纸调整大小缩放设置壁纸

baton3.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 

     InputStream tapeta = getResources().openRawResource(textureArrayWin[n]); 

     Bitmap bitmap = BitmapFactory.decodeStream(tapeta); 
     Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth(); 
     int height = display.getHeight(); 

     int oldwidth= bitmap.getWidth(); 
     int oldheight= bitmap.getHeight(); 
     float skalaszerokosci = ((float) oldwidth)/width; 
     float skalawysokosci = ((float)oldheight)/height; 

     Matrix macierz = new Matrix(); 
     macierz.postScale(skalaszerokosci, skalawysokosci); 

     Bitmap zmieniona = Bitmap.createBitmap(bitmap, 0, 0, 
       width, height, macierz, true); 


     try 
     { 

      getApplicationContext().setWallpaper(zmieniona); 


     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 


    } 

}); 
+0

顺便说一句。壁纸太大,缩放:| – ramzixp

+0

您是否尝试过'createScaledBitmap()'而不是? – DeeV

+0

是的,尝试过......同样的效果,看起来setWallpaper是seting自己的决议:| – ramzixp

回答

1

附加体现 <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/> <uses-permission android:name="android.permission.SET_WALLPAPER" />

1

下面的代码:

// Get display dimensions 
    final DisplayMetrics metrics = getResources().getDisplayMetrics(); 
    final int displayWidth = metrics.widthPixels; 
    final int displayHeight = metrics.heightPixels; 

    // Here I'm decoding a resource, just for the example sake 
    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture); 

    // obtain the original Bitmap's dimensions 
    final int originalWidth = bitmap.getWidth(); 
    final int originalHeight = bitmap.getHeight(); 

    // Obtain the horizontal and vertical scale factors 
    final float horizontalScaleFactor = (float) originalWidth/(float) displayWidth; 
    final float verticalScaleFactor = (float) originalHeight/(float) displayHeight; 

    // Get the biggest scale factor to use in order to maintain original image's aspect ratio 
    final float scaleFactor = Math.max(verticalScaleFactor, horizontalScaleFactor); 
    final int finalWidth = (int) (originalWidth/scaleFactor); 
    final int finalHeight = (int) (originalHeight/scaleFactor); 

    // Create the final bitmap 
    final Bitmap wallpaperBmp = Bitmap.createScaledBitmap(
      bitmap, finalWidth, finalHeight, true); 

    // Recycle the original bitmap 
    bitmap.recycle(); 

    // Finally set it as wallpaper 
    try { 
     final WallpaperManager wallMan = WallpaperManager.getInstance(this); 
     wallMan.setBitmap(wallpaperBmp); 
    } catch (IOException e) { 
     Log.e("Wallpaper", "Something went wrong: " + e.toString()); 
     wallpaperBmp.recycle(); 
    } 

另外,不要忘了将SET_WALLPAPER添加到您的manifest.xml中:

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