2011-10-12 10 views
1

壁紙のビットマップを設定しますが効果はありません。私は、生のフォルダと、アレイ内のコードをこのコードとすべての.jpgファイルを持っています。またビットマップから壁紙へのスケールのサイズ変更setWallpaper

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

、忘れないでください:

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

BTW。壁紙が大きすぎる、ズームされた:| – ramzixp

+0

代わりに 'createScaledBitmap()'を試しましたか? – DeeV

+0

はい、試してみました...同じ効果、setWallpaperが独自の解像度を設定しているようです: – ramzixp

答えて

1

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

1

をマニフェストに追加ここでは、コードですSET_WALLPAPERをマニフェストファイルに追加します。