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 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01