如何获取android中使用的图像的高度和宽度

abh*_*eta 25 android dimensions imageview android-bitmap

我想获得ImageView中的图像的高度和宽度,或者在android模拟器中获取背景图片.请帮助我,任何帮助都会被appriciated.

Pra*_*tik 83

您可以通过使用getWidth()和getHeight()来获取ImageView的高度和宽度,而这不会为您提供图像的精确宽度和高度,为了获得图像宽度高度,首先需要将drawable作为背景然后转换可绘制到BitmapDrawable以将图像作为Bitmap从中获取宽度和高度,就像这里一样

Bitmap b = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();
Run Code Online (Sandbox Code Playgroud)

还是喜欢这里

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();
Run Code Online (Sandbox Code Playgroud)

上面的代码将为您提供当前imageview大小的位图,如设备的屏幕截图

仅适用于ImageView大小

imageView.getWidth(); 
imageView.getHeight(); 
Run Code Online (Sandbox Code Playgroud)

如果你有可绘制的图像,并且你想要这个尺寸,你就可以这样做

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight(); 
int w = d.getIntrinsicWidth();      
Run Code Online (Sandbox Code Playgroud)