2

この関数で画像のサイズを縮小しています:

Drawable reduceImageSize (String path) 
{
    BitmapDrawable bit1 = (BitmapDrawable) Drawable.createFromPath(path);
    Bitmap bit2 = Bitmap.createScaledBitmap(bit1.getBitmap(), 640, 360, true);
    BitmapDrawable bit3 = new BitmapDrawable(getResources(),bit2);
    return bit3;
}

そして、それは正常に動作します。唯一の問題は、この関数を複数回呼び出すとアプリが遅くなることです。この関数を最適化する方法はありますか? たぶん、マトリックスを介してサイズを縮小しますか?また、SD カードから画像を読み込んでおり、アニメーション用に Drawable としてバックが必要であり、この関数はこれを提供します。

4

1 に答える 1

4

と を使用BitmapFactory.OptionsinJustDecodeBoundsて縮小します。

Bitmap bitmap = getBitmapFromFile(path, width, height);

public static Bitmap getBitmapFromFile(String path, int width, int height) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return bitmap;
}

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) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

詳しくはこちらをご覧ください: Loading Large Bitmaps Efficiently

また、このメソッドをどこで呼び出しているのかわかりませんが、それらのメソッドが多数ある場合は、次の方法でビットマップをキャッシュしていることを確認してくださいLruCache: Caching Bitmaps

于 2013-06-04T10:40:25.227 に答える