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

发布于 2024-12-27 09:18:06 字数 55 浏览 4 评论 0原文

我想获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2025-01-03 09:18:06

您可以使用 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();      

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();

or do like here

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();

the above code will give you current imageview sized bitmap like screen shot of device

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();      
不念旧人 2025-01-03 09:18:06

由于某种原因,接受的答案对我不起作用,而是我按照目标屏幕 dpi 实现了图像尺寸,如下所示。

方法1

Context context = this; //If you are using a view, you'd have to use getContext();
Resources resources = this.getResources();
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, R.drawable.cake, bounds); //use your resource file name here.
Log.d("MainActivity", "Image Width: " + bounds.outWidth);

这是原始链接

http://upshots.org/android /android-get-dimensions-of-image-resource

方法2

BitmapDrawable b = (BitmapDrawable)this.getResources().getDrawable(R.drawable.cake);
Log.d("MainActivity", "Image Width: " + b.getBitmap().getWidth());

它不显示图像资源中像素的确切数量,但显示一个有意义的数字,也许有人可以进一步解释。

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

Context context = this; //If you are using a view, you'd have to use getContext();
Resources resources = this.getResources();
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, R.drawable.cake, bounds); //use your resource file name here.
Log.d("MainActivity", "Image Width: " + bounds.outWidth);

Here is the original link

http://upshots.org/android/android-get-dimensions-of-image-resource

Method 2

BitmapDrawable b = (BitmapDrawable)this.getResources().getDrawable(R.drawable.cake);
Log.d("MainActivity", "Image Width: " + b.getBitmap().getWidth());

It doesn't show the exact number of pixels in the image resource but a meaningful number perhaps someone can explain further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文