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

标签 android imageview android-bitmap dimensions

我想获取 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/

相关文章:

java - 使用自定义颜色在 ImageView 上添加图像叠加

android - 在 GridView 中同步加载图像 - Android

android - 删除未反射(reflect)在图库中的图像

android - 将我存储的 JPG 文件加载到 ImageView 的最有效方法是什么?

php - 从 mysql-php(android) 中检索图像

android - 在 Android 中使用 Canvas 和位图,如何获取此图像?

java - 我的 Android 库中的包结构更好

Java/Android 什么比 ArrayList 更快?

android - Camera intent.getData()在部分设备返回null,主要在android 7.0

android - 由 : android. database.sqlite.SQLiteException 引起:没有这样的表:BOOK(代码 1 SQLITE_ERROR)