2

我用来将图像作为画布背景的所有方法

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

我得到一张大图。任何人都可以请。为我提供根据设备大小获取图像集的解决方案。由于我是编码新手,因此将不胜感激:)

谢谢你

4

3 回答 3

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));
于 2013-07-15T15:24:59.167 回答
0

这个过程看起来像这样:

  1. 获取屏幕尺寸
  2. 计算新的图像大小(保持纵横比)
  3. 将位图解码为新大小。

获取屏幕尺寸

要获得设备的确切屏幕尺寸,请使用DisplayMetrics...

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

metrics变量现在将包含以像素为单位的屏幕尺寸。您可以使用metrics.widthPixels和获取值metrics.heightPixels

计算新的图像大小(保持纵横比)

要计算新的图像大小,我们可以使用BitmapFactory.Options并告诉它将图像缩小 1 倍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

于 2013-07-15T15:39:54.287 回答
0

Determine the device width and height and scale it:

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

So in complete you can use the following ready-to-use snippet taken from 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;
}

So finaly use:

 myCanvas.drawBitmap(scaleToActualAspectRatio(myBitmap), X, Y, null)
于 2013-07-15T15:26:53.060 回答