如何在 Android 中获取 ImageView/Bitmap 的高度和宽度 [英] How to get the height and width of an ImageView/Bitmap in Android

查看:27
本文介绍了如何在 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 的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆