How to get the height and width of an ImageView/Bitmap in Android(如何在 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.
推荐答案
你可以通过getWidth()和getHeight()来获取ImageView的高度和宽度,但这不会给你图像的确切宽度和高度,要首先获取图像宽度高度,您需要将可绘制对象作为背景,然后将可绘制对象转换为 BitmapDrawable 以将图像作为位图获取,您可以像这里一样获取宽度和高度
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
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 大小的位图,比如设备的屏幕截图
the above code will give you current imageview sized bitmap like screen shot of device
仅适用于 ImageView 大小
for only ImageView size
imageView.getWidth();
imageView.getHeight();
如果你有可绘制的图像并且你想要那个尺寸,你可以这样得到
If you have drawable image and you want that size you can get like this way
Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
这篇关于如何在 Android 中获取 ImageView/Bitmap 的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Android 中获取 ImageView/Bitmap 的高度和宽度


- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01