如何在 Android 中获取 ImageView/Bitmap 的高度和宽度
我想获取 ImageView 或背景图像中的图像位图的高度和宽度。请帮助我,任何帮助将不胜感激。
I want to get the height and width of an image bitmap that is either in ImageView
or a background image. Please help me, any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 getWidth() 和 getHeight() 获取 ImageView 的高度和宽度,但这不会为您提供图像的确切宽度和高度,要首先获取图像宽度高度,您需要获取可绘制对象作为背景,然后进行转换Drawable 到 BitmapDrawable 以获取位图形式的图像,您可以像这里一样获取宽度和高度,
或者像这里一样,
上面的代码将为您提供当前 imageview 大小的位图,例如
仅适用于 ImageView 大小
的设备屏幕截图如果您有可绘制图像并且您想你可以这样得到那个尺寸
You can get height and width of ImageView by using getWidth() and getHeight() through while this will not give you the exact width and height of the image, for getting the Image width height first you need to get the drawable as background then convert drawable to BitmapDrawable to get the image as Bitmap from that you can get the width and height like here
or do like here
the above code will give you current imageview sized bitmap like screen shot of device
for only ImageView size
If you have drawable image and you want that size you can get like this way
由于某种原因,接受的答案对我不起作用,而是我按照目标屏幕 dpi 实现了图像尺寸,如下所示。
方法1
这是原始链接
http://upshots.org/android /android-get-dimensions-of-image-resource
方法2
它不显示图像资源中像素的确切数量,但显示一个有意义的数字,也许有人可以进一步解释。
For some reason accepted answer didn't work for me, instead I achieved the image dimension as per the target screen dpi like this.
Method 1
Here is the original link
http://upshots.org/android/android-get-dimensions-of-image-resource
Method 2
It doesn't show the exact number of pixels in the image resource but a meaningful number perhaps someone can explain further.