2012-12-19 1 views
5

私は、ギャラリーアプリを作成し、私の最初のアプリと、これは私のコードアンドロイドでクロップしたりズームしたりせずに壁紙を設定するコードはありますか?

Bitmap bmd = BitmapFactory.decodeStream(is); 

    try{ 
     getApplicationContext().setWallpaper(bmd); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

でいます上記のコードは、壁紙を設定します。しかし、それが設定されてしまった後に壁紙は、トリミングやズームます! 上記のコードで私ができる変更がありますので、設定したときにズームやトリミングをしなくても壁紙を設定できます。

Plzzzzが手伝ってくれます!ありがとうございます:-)

+0

ビットマップサイズをデバイスのディスプレイのサイズと同じに設定するか、視差効果を実現しますか? – forcelain

答えて

3

私はこれに遅れています。小さなを行い、上記のコードが機能しない場合

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int height = displayMetrics.heightPixels; 
int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width 

// First decode with inJustDecodeBounds=true to check dimensions 
final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

// Calculate inSampleSize 
options.inSampleSize = calculateInSampleSize(options, width, height); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options); 

WallpaperManager wm = WallpaperManager.getInstance(this); 
try { 
    wm.setBitmap(decodedSampleBitmap); 
} catch (IOException e) { 
    Log.e(TAG, "Cannot set image as wallpaper", e); 
} 

:あなたのケースでは

、多少このようなことにより、デバイスのサイズにあなたの写真を調整してみてください:それはあなたとあなたの質問を訪れる人たちがお役に立てば幸いです修正:

... 
WallpaperManager wm = WallpaperManager.getInstance(this); 
try { 
    wm.setBitmap(decodedSampleBitmap); 
    wm.suggestDesiredDimensions(width, height); 
} catch (IOException e) { 
    Log.e(TAG, "Cannot set image as wallpaper", e); 
} 

そして方法calculateInSampleSize

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

と再メンバ:アクセス許可を追加する:

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

calculateInSampleSizeメソッドとは何ですか?説明してください。 –

+0

@RobinRoyal私は答えを更新しました:-) –

+0

高さで切り取っても機能しません。しかし、ビットマップの完全なwidhtによって壁紙を設定する方法は? – NickUnuchek

関連する問題