2013-07-15 40 views
1

設備大小在畫布上規模的背景圖片所有我曾經用來獲取在畫布上如何根據機器人

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.help_4, 0, 0, null); 

圖像爲背景,我得到一個大的圖像。所以任何人都可以請。爲我提供一個根據設備大小獲取圖像集的解決方案。由於我是新來的編碼的詳細解釋,將不勝感激:)

謝謝

+0

位圖類有一個createScaledBitmap方法 – njzk2

回答

0

你幾乎沒有。你只需要通過設置輸出的寬度和高度(根據屏幕大小)來配置BitmapFactory.Options變量:

BitmapFactory.Options options = new BitmapFactory.Options(); 
Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
options.outHeight = size.x; 
options.outWidth = size.y; 
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.help_4, options)); 
0

確定設備的寬度和高度和規模:在完成這麼

int deviceWidth = getWindowManager().getDefaultDisplay() 
.getWidth(); 
int deviceHeight = getWindowManager().getDefaultDisplay() 
.getHeight(); 

您可以使用從here採取了以下準備使用的片段:

public Bitmap scaleToActualAspectRatio(Bitmap bitmap) { 
if (bitmap != null) { 
boolean flag = true; 

int deviceWidth = getWindowManager().getDefaultDisplay() 
.getWidth(); 
int deviceHeight = getWindowManager().getDefaultDisplay() 
.getHeight(); 

int bitmapHeight = bitmap.getHeight(); 
int bitmapWidth = bitmap.getWidth(); 

if (bitmapWidth > deviceWidth) { 
flag = false; 

// scale According to WIDTH 
int scaledWidth = deviceWidth; 
int scaledHeight = (scaledWidth * bitmapHeight)/bitmapWidth; 

try { 
if (scaledHeight > deviceHeight) 
    scaledHeight = deviceHeight; 
    bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, 
    scaledHeight, true); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
} 

if (flag) { 
    if (bitmapHeight > deviceHeight) { 
    // scale According to HEIGHT 
    int scaledHeight = deviceHeight; 
    int scaledWidth = (scaledHeight * bitmapWidth)/bitmapHeight; 

    try { 
     if (scaledWidth > deviceWidth) 
     scaledWidth = deviceWidth; 
     bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, 
     scaledHeight, true); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
    } 
    } 
return bitmap; 
} 

所以finaly使用:

myCanvas.drawBitmap(scaleToActualAspectRatio(myBitmap), X, Y, null) 
0

這一過程將是這個樣子:

  1. 獲取屏幕尺寸
  2. 計算新的圖像尺寸(保持縱橫比)
  3. 解碼位圖到新的大小。

獲取屏幕尺寸

要獲取設備的確切屏幕大小,使用DisplayMetrics ...

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 

metrics變量現在將包含像素的屏幕尺寸。您可以使用metrics.widthPixelsmetrics.heightPixels來獲取這些值。

計算新的圖像尺寸(保持縱橫比)

要計算新的圖像的大小,我們可以使用BitmapFactory.Options並告訴它通過的x一個因子來縮放圖像下移。爲了計算這個因素(也稱爲inSampleSize),用下面的方法...

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

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

解碼位圖,以新的大小

// First decode with inJustDecodeBounds=true to check dimensions 
final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.drawable.help_4, options); 

// Calculate inSampleSize 
options.inSampleSize = calculateInSampleSize(options, metrics.widthPixels, metrics.heightPixels); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
return BitmapFactory.decodeResource(getResources(), R.drawable.help_4, options); 

inJustDecodeBounds選項告訴框架,以空出位圖以防止OOM異常,因爲我們只對維度感興趣,而不是實際的圖像。

該方法將確保位圖的有效縮放。在這裏閱讀更多:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html