2

Androidで画像のサイズを変更するには、次の方法を使用します。

public Bitmap resize(Bitmap img, int Width, int Height) {

    int width = img.getWidth();     
    int height = img.getHeight();
    int newWidth = (int) Width;
    int newHeight = (int) Height;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();

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

    // rotate the Bitmap
    //matrix.postRotate(45);

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

    return resizedBitmap;
} 

ほとんどの Android デバイスで正常に動作します。ただし、一部のデバイスでは、サイズ変更された画像が表示されません。どうすればこれを解決できますか?

4

2 に答える 2

1

でスケーリングされたビットマップを作成できます

Bitmap bitmap = Bitmap.createScaledBitmap(b, width, height, true);

ここでは、プログラムで画面サイズを読み取ることにより、さまざまなデバイスのデバイスの画面サイズに応じて幅と高さを指定できます。デバイスの画面サイズをプログラムで読み取る方法は次のとおりです

于 2013-09-02T15:49:42.660 に答える