2013-05-21 1 views
2

나는 wallpapermanager에 간단한 문제가 있으며이 사이트에서 답변을 찾을 수 없습니다. 나는 1280x800 (내 타블렛의 디스플레이) 인 그림과 함께이 간단한 코드를 가지고있다. 코드를 실행하면 마치 전체 그림이 확대 된 것처럼 이미지 중심을 배경 무늬로 사용합니다. 왜 그럴까요? 감사!리소스에서 배경 화면 설정

package com.daniel.wallpaperPorsche; 

import java.io.IOException; 
import com.daniel.wallpaper.R; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.WallpaperManager; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(this); 

try { 
    myWallpaperManager.setResource(R.drawable.porsche_911_1280x800_72dpi); 

    Toast.makeText(getBaseContext(), "Success set as wallpaper", Toast.LENGTH_SHORT).show(); 

} catch (IOException e) { 
    Toast.makeText(getBaseContext(), "Error set as wallpaper", Toast.LENGTH_SHORT).show(); 

    } 
    super.onDestroy(); 
    } 
} 

답변

1

나는 BitmapFactory 함께 (해상도 크기가 동일 할 때 당신이보고있는 종종 완전히 임의의 것) 어떤 줌 문제없이 배경 화면을 설정하기 위해 찾은 가장 좋은 방법. WallpaperManager.setResource 대신 WallpaperManager.setBitmap을 사용합니다.

String path = "path/to/desired/wallpaper/file"; 
final BitmapFactory.Options options = new BitmapFactory.Options(); 

//The inJustDecodeBounds option tells the decoder to return null (no bitmap), but it does 
//include the size of the image, letting us query for the size. 

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

//wm = WallpaperManager 
wm.suggestDesiredDimensions(decodedSampleBitmap.getWidth(), decodedSampleBitmap.getHeight()); 
wm.setBitmap(decodedSampleBitmap); 

calculateInSampleSize는 실제로 Google의 안드로이드 설명서를 구현하는 방법입니다. 그들의 목표는 큰 비트 맵을 효율적으로로드하는 것입니다. 여기에 대해 약간의 독서를 할 수 있습니다. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

불행히도, 그 구현은 쓰레기이며 자주 잘못되었습니다. 나는 이것을 권하고 싶다.

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

    int multiplyHeight = 1; 
    int multiplyWidth = 1; 

    while (imageHeight >= reqHeight) 
    { 
     multiplyHeight++; 
     imageHeight = imageHeight/2; 
    } 
    while (imageWidth >= reqWidth) 
    { 
     multiplyWidth++; 
     imageWidth = imageWidth/2; 
    } 

    if (multiplyHeight > multiplyWidth) 
     return multiplyHeight; 
    else 
     return multiplyWidth; 

} 

이 방법은 큰 파일을로드하기위한 효율적 인에서 OutOfMemoryException을 방지하고, 또한 당신이보고있는 줌 이상한 피해야한다. 그렇지 않다면 알려주세요. 내가 한 일을 다시 살펴 보겠습니다.