0

ImageView があり、ユーザーの GPS 位置に基づいて getImageResource() が必要です。6 つの画像があり、2 点間の距離が短くなると、画像を新しいリソースに置き換えます。

私はGalaxy S4でアプリをテストしていますが、問題は、OutOfMemoryが原因で、非常に小さな乱数のロード後にアプリがクラッシュすることです。

画像をキャッシュする良い方法はありますか? (おそらく、AsyncTask を使用してそれらをロードする必要があります)

画像は 400x400px の png-24 ビットで透明です。

ありがとうございました

4

2 に答える 2

1

これを使用してみてください:

public static Bitmap decodeSampledBitmapFromResource(String uri,
        int reqWidth, int reqHeight, int orientation) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap decodeFile = BitmapFactory.decodeFile(uri, options);
    int rotate = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate = 90;
        break;
    }
    Matrix matrix = new Matrix();

    // matrix.postScale(scaleWidth, scaleHeight);
    matrix.postRotate(rotate);

    Bitmap rotatedBitmap = Bitmap.createBitmap(decodeFile, 0, 0,
            decodeFile.getWidth(), decodeFile.getHeight(), matrix, true);

    return rotatedBitmap;

}

private 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;
}
于 2013-07-11T18:37:33.990 に答える
0

Galaxy S4はドローアブルで動作する可能性が最も高いxxhdpiため、すべてを入れるmdpiと、システムがS4のdpiレベルに合わせて画像を拡大するため、OutOfMemoryエラーが発生します。xhdpidpi (およびを含む) に応じて、それぞれのフォルダーにドローアブルをスケーリングして配置してみてくださいxxhdpi。その後、コードを最適化してください。

于 2013-07-11T19:05:29.067 に答える