我想获取 ImageView
或背景图像中的图像位图的高度和宽度。请帮助我,任何帮助将不胜感激。
最佳答案
您可以通过使用 getWidth() 和 getHeight() 来获取 ImageView 的高度和宽度,但这不会为您提供图像的确切宽度和高度,为了获得图像宽度高度首先您需要获取可绘制对象背景然后将drawable转换为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();
关于android - 如何在 Android 中获取 ImageView/Bitmap 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8880376/