如何在Android中获取ImageView / Bitmap的高度和宽度

我想获得ImageView或背景图像中的图像位图的高度和宽度.请帮助我,任何帮助将不胜感激.

解决方法:

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

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();      
上一篇:JAVA截取全屏


下一篇:如何在Pygame中获得监视器的分辨率?