2

高解像度の画像に問題があります。

1280x720 の画像に nodpi-drawable フォルダーを使用し、このコードを使用してスケーリングします。

public static Drawable scaleDrawable(Drawable d, int width, Activity cxt)
    {
        BitmapDrawable bd = (BitmapDrawable)d;

        double oldWidth = bd.getBitmap().getWidth();
        double scaleFactor = width / oldWidth;

        int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor);
        int newWidth = (int) (oldWidth * scaleFactor);

        Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth));

        BitmapDrawable bd2 = (BitmapDrawable)drawable;

        return  drawable;
    }

    public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

        int width = bm.getWidth(); 
        int height = bm.getHeight(); 

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();

        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

        return resizedBitmap; 
        }

そのコードを使用して画像を画面幅に合わせて拡大縮小するため、画面が 320x480 の場合、画像は 320 に拡大縮小され、縦横比が維持されます。画像が下から画面の外に出ても気にしません。

すべて正常に動作しますが、正確に 720x1280 の画面を持つ xhdpi デバイス、特に Samsung Galaxy Note 2 で試してみると.

次の行で Out Of Memory Exception でクラッシュします。

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

画像を 720 から 720 にスケーリングする必要がある理由を理解できませんが、コードの最適化が非常に悪いか何かである必要があります。

1080x1920 のデバイスで試したことはありませんが、クラッシュするようです。

コードを見たときに何か悪いことがわかる人はいますか?

4

2 に答える 2

7

このメソッドを使用してビットマップのサイズを変更します-

 Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);

この定義を使用してください-

 public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
    int reqHeight) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

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

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
}
  public int calculateInSampleSize(BitmapFactory.Options options,
    int reqWidth, int reqHeight) {

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

リソースを使用している場合は、メソッドを BitmapFactory の decodeResource メソッドに置き換えます。

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

....
.....
return BitmapFactory.decodeResource(res, resId, options);
}
于 2013-08-01T09:42:08.480 に答える
0

画像処理操作には、ユーティリティ クラス BitmapFactory を使用する必要があります。また、BitmapFactory.Options を使用して、ビットマップの入出力サイズを調整します。ビットマップが不要になったら、関連するメモリを解放する必要があります。

于 2013-08-01T09:40:15.130 に答える