试图在 ImageView 中获取图像的显示大小

Trying to get the display size of an image in an ImageView(试图在 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() and getIntrinsicHeight() will both return the original dimensions.
  • Getting the Drawable through ImageView.getDrawable() and casting it to a BitmapDrawable, then using BitmapDrawable.getBitmap().getWidth() and getHeight() 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 中获取图像的显示大小

基础教程推荐