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

查看:1233
本文介绍了如何在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的高度和宽度,但这并不能为您提供图像的确切宽度和高度,要获取图像的宽度高度,首先需要获取drawable作为背景,然后将drawable转换为BitmapDrawable,以将图像获取为Bitmap,就可以像这里一样获得宽度和高度

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天全站免登陆