Android性能位图

Android性能位图,android,bitmap,Android,Bitmap,我有这个 Bitmap appWidgetBitmap = Bitmap.createBitmap(availableWidth, availableHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(appWidgetBitmap); 在日志控制台中我看到了这个 "dalvikvm-heap Grow heap (frag case) to......byte allocation 如何使用尽可能少的资源

我有这个

    Bitmap appWidgetBitmap = Bitmap.createBitmap(availableWidth, availableHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(appWidgetBitmap);
在日志控制台中我看到了这个

"dalvikvm-heap Grow heap (frag case) to......byte allocation
如何使用尽可能少的资源制作位图?

我在
BitmapFactory
上看到了一些东西,但是如何做到这一点,因为上面的代码是
createBitmap
,而不是从资源中获得的。

使用以下方法将图像的大小调整为纵横比:

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

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    // BitmapFactory.decodeResource(res, resId, options);

     return Bitmap.createScaledBitmap(
            BitmapFactory.decodeResource(res, resId, options), reqWidth,
            reqHeight, true);
}
然后将图像解码为位图格式并设置到画布中:

 Bitmap src = decodeSampledBitmapFromResource(getResources(),
            R.drawable.ic_bg_box, imgWidth, imgHeight);
    Canvas canvas = new Canvas(src);

我希望这会对你有所帮助。

这就是你需要的。谢谢应该是这边。嗨,反正我没有资源。你们看,我的代码只是创建一个位图,然后把它放到画布上。但您的代码是从资源中解码位图。