2012-12-19 1 views
5

난 갤러리 응용 프로그램을 만드는 오전, 내 첫 번째 응용 프로그램 및이 내 코드안드로이드에서 자르기 및 확대/축소하지 않고 배경 무늬를 설정할 수있는 코드가 있습니까?

Bitmap bmd = BitmapFactory.decodeStream(is); 

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

위의 코드는 배경 화면으로 설정하지만 배경 화면이 잘 리거나가 설정되었다 후 확대됩니다입니다! 내가 위의 코드에서 할 수있는 변경 사항이있어서 확대/축소 또는 자르기 설정없이 배경 화면을 설정할 수 있습니다 !!!!

Plzzzz help me out !! 미리 감사드립니다 :-)

+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