Trying to get the display size of an image in an ImageView(试图在 ImageView 中获取图像的显示大小)
问题描述
我正在尝试获取图像视图中显示的图像的实际大小.实际上我的图像比屏幕大,并且图像视图正在调整图像大小以显示它.我正在寻找这种新尺寸.
I'm trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I'm looking for this new size.
我尝试在自定义视图中覆盖 ImageView 的 onDraw 方法,但没有得到正确的高度和宽度...
I've tried to override the onDraw method of the ImageView in a custom view but I'm not getting the correct height and width...
public class LandImageView extends ImageView
{
public LandImageView( Context context )
{
super( context );
}
public LandImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public LandImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onDraw( Canvas canvas )
{
super.onDraw( canvas );
int test = this.getWidth();
int test2 = this.getHeight();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
}
你有什么线索吗?
推荐答案
这里的答案都没有真正回答问题:
None of the answers here actually answer the question:
从 ImageView
显示的任意大小的 Bitmap
,找到 显示图像的实际尺寸,而不是提供的 Bitmap
的尺寸.
From a Bitmap
of any size displayed by an ImageView
, find the actual dimensions of the displayed image as opposed to the dimensions of the supplied Bitmap
.
即:
- 使用
ImageView.getDrawable().getInstrinsicWidth()
和getIntrinsicHeight()
都会返回原始尺寸. - 通过
ImageView.getDrawable()
获取Drawable
并将其转换为BitmapDrawable
,然后使用BitmapDrawable.getBitmap().getWidth()
和getHeight()
还返回原始图像及其尺寸.
- Using
ImageView.getDrawable().getInstrinsicWidth()
andgetIntrinsicHeight()
will both return the original dimensions. - Getting the
Drawable
throughImageView.getDrawable()
and casting it to aBitmapDrawable
, then usingBitmapDrawable.getBitmap().getWidth()
andgetHeight()
also returns the original image and its dimensions.
获得显示图像的实际尺寸的唯一方法是提取并使用用于显示图像的变换Matrix
.这必须在测量阶段之后完成,这里的示例显示它在 onMeasure()
的 Override
中为自定义 ImageView
调用:
The only way to get the actual dimensions of the displayed image is by extracting and using the transformation Matrix
used to display the image as it is shown. This must be done after the measuring stage and the example here shows it called in an Override
of onMeasure()
for a custom ImageView
:
public class SizeAwareImageView extends ImageView {
public SizeAwareImageView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Get image matrix values and place them in an array
float[] f = new float[9];
getImageMatrix().getValues(f);
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
final float scaleX = f[Matrix.MSCALE_X];
final float scaleY = f[Matrix.MSCALE_Y];
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
final Drawable d = getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();
// Calculate the actual dimensions
final int actW = Math.round(origW * scaleX);
final int actH = Math.round(origH * scaleY);
Log.e("DBG", "["+origW+","+origH+"] -> ["+actW+","+actH+"] & scales: x="+scaleX+" y="+scaleY);
}
}
注意:要从一般代码中获取图像变换Matrix
(如在Activity
中),函数是ImageView.getImageMatrix()
- 例如myImageView.getImageMatrix()
Note: To get the image transformation Matrix
from code in general (like in an Activity
), the function is ImageView.getImageMatrix()
- e.g. myImageView.getImageMatrix()
这篇关于试图在 ImageView 中获取图像的显示大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:试图在 ImageView 中获取图像的显示大小
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01