ImageView
中または背景画像の画像ビットマップの高さと幅を取得したい。私を助けてください、どんな助けでもありがたいです。
71653 次
2 に答える
94
getWidth()とgetHeight()を使用してImageViewの高さと幅を取得できますが、画像の正確な幅と高さはわかりません。最初に画像の幅の高さを取得するには、背景としてドローアブルを取得してから変換する必要があります。 BitmapDrawableにドローアブルして、画像をビットマップとして取得します。そこから、ここのように幅と高さを取得できます。
Bitmap b = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();
またはここのように
imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();
上記のコードは、デバイスのスクリーンショットのような現在のimageviewサイズのビットマップを提供します
ImageViewサイズのみ
imageView.getWidth();
imageView.getHeight();
ドローアブル画像があり、そのサイズが必要な場合は、このように取得できます
Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
于 2012-01-16T13:15:32.820 に答える
3
どういうわけか、受け入れられた答えは私にはうまくいきませんでした、代わりに私はこのようにターゲット画面のdpiに従って画像の寸法を達成しました。
方法1
Context context = this; //If you are using a view, you'd have to use getContext();
Resources resources = this.getResources();
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, R.drawable.cake, bounds); //use your resource file name here.
Log.d("MainActivity", "Image Width: " + bounds.outWidth);
これが元のリンクです
http://upshots.org/android/android-get-dimensions-of-image-resource
方法2
BitmapDrawable b = (BitmapDrawable)this.getResources().getDrawable(R.drawable.cake);
Log.d("MainActivity", "Image Width: " + b.getBitmap().getWidth());
画像リソースの正確なピクセル数は表示されませんが、おそらく誰かがさらに説明できる意味のある数です。
于 2018-01-05T11:10:11.567 に答える