问题描述
我想要得到的图像视图中显示图像的实际大小。其实我的形象是大于屏幕和ImageView的是在调整图像以diplay它。我在寻找这个新的大小。
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);
}
}
你有没有什么线索?
Do you have any clues ?
推荐答案
没有问题的答案在这里居然回答这个问题:
None of the answers here actually answer the question:
从位图
由的ImageView
显示的任何尺寸,找到实际尺寸所显示的图像的作为反对的尺寸所提供的位图
。的
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()
,并将其转换成一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.
获得的唯一方法在实际显示的图像尺寸是通过提取和利用,改造矩阵
用于显示图像,因为它被示出。这必须在测量阶段之后完成这里的例子显示了它在 onMeasure()
的覆盖
呼吁自的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);
}
}
注意::要获取图像变换矩阵
从code一般(如在活动
),功能 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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!