高解像度の画像に問題があります。
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 のデバイスで試したことはありませんが、クラッシュするようです。
コードを見たときに何か悪いことがわかる人はいますか?