5

プライベートファイルに画像があります。ファイルを読み取り、ドローアブルを作成して、ImageView に割り当てます。ImageView には WRAP_CONTENT があるため、サイズは自動です。

320x480 の画面では、画像はきれいに見えますが、より解像度が高く高密度の 480x800 または 480x854 (N1、ドロイド) の画面では、画像がたとえば 150x150 の場合、画像は 100x100 として表示されます。

もちろん、密度と関係がありますが、これをどのように解決すればよいかわかりません。

これは私のコードです:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

fis.close();

imageView.setImageDrawable(icon);

ありがとう

================================================== ==============

アップデート:

次のコードを使用します。

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "アイコン");

次にアイコンのサイズを調べると、Android はサイズが 100x100 であると認識しますが、実際には 150x150 です。

濃度によって画像を縮小しているように見えます。誰でもこれとこれを回避する方法を説明できますか?

ありがとう

4

4 に答える 4

2

画像を希望のサイズに再拡大縮小します。

            d = Drawable.createFromPath(filePath);
            if (d != null) {
                Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap();
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
                int newWidth = 170;
                int newHeight = 170;
                // calculate the scale
                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);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                width, height, matrix, true);
                // make a Drawable from Bitmap to allow to set the BitMap
                d = new BitmapDrawable(resizedBitmap);
            }
于 2010-12-17T06:45:03.300 に答える
2

私は同様の問題に取り組んでいます。これを試してください:

TypedValue typedValue = new TypedValue();
//density none divides by the density, density default keeps the original size
typedValue.density = TypedValue.DENSITY_DEFAULT;
Drawable d = Drawable.createFromResourceStream(null, typedValue, fis, "icon");

drawable-* ディレクトリの代わりに、一般的なストリームから解像度/サイズ変更画像を選択する一般的なソリューションを開発する方法については、まだ取り組んでいます。

于 2011-08-10T13:05:31.940 に答える
2

BitmapDrawable#setTargetDensity を試してください:

Bitmap b = BitmapFactory.decodeFile(path)
BitmapDrawable bmd = new BitmapDrawable(b);
bmd.setTargetDensity(getResources().getDisplayMetrics());
于 2013-05-14T07:11:42.063 に答える
1

デバイスに依存しないピクセル (または DIP) でいくつかの寸法を設定します。

于 2010-04-15T22:55:03.533 に答える