2013-07-15 27 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