2012-12-19 16 views
5

tôi đang tạo ra một ứng dụng ảnh, ứng dụng đầu tiên của tôi và đây là mã của tôiCó mã nào để đặt hình nền mà không cắt xén và phóng to trong android không?

Bitmap bmd = BitmapFactory.decodeStream(is); 

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

Đoạn mã trên bộ hình nền Nhưng hình nền bị cắt hoặc thu nhỏ sau khi nó đã thiết lập !! Có bất kỳ sửa đổi nào tôi có thể thực hiện trong mã trên để tôi có thể đặt hình nền mà không cần phóng to hoặc cắt xén khi được đặt !!!!

Plzzzz giúp tôi !! Cảm ơn trước :-)

+0

Bạn muốn đặt kích thước bitmap bằng với kích thước màn hình của thiết bị hoặc bạn muốn đạt được hiệu ứng thị sai? – forcelain

Trả lời

3

Tôi trễ để trả lời câu hỏi này. Hy vọng nó sẽ giúp bạn và những người đến thăm hỏi của bạn:

Trong trường hợp của bạn, cố gắng điều chỉnh hình ảnh của bạn để kích thước thiết bị bằng hơi như thế này:

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

Nếu mã ở trên không hoạt động, làm một nhỏ sửa đổi:

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

và phương pháp 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; 
} 

và tái thành viên để thêm quyền:

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

Phương pháp calculateInSampleSize là gì.Vui lòng giải thích. –

+0

@RobinRoyal Tôi đã cập nhật câu trả lời ... :-) –

+0

Không hoạt động, nó cắt theo Chiều cao. Nhưng làm thế nào để thiết lập hình nền bằng widht đầy đủ của bitmap? – NickUnuchek

Các vấn đề liên quan