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

Gui*_*ant 45 android imageview ondraw

我试图获得图像视图中显示的图像的实际大小.实际上我的图像比屏幕大,图像视图正在调整图像大小以显示它.我正在寻找这个新尺寸.

我试图在自定义视图中覆盖ImageView的onDraw方法,但我没有得到正确的高度和宽度...

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

你有线索吗?

B T*_*B T 100

这里的答案都没有实际回答这个问题:

从a Bitmap显示的任何尺寸中ImageView,找到所显示图像实际尺寸,而不是所提供的尺寸Bitmap.

即:

  • 使用ImageView.getDrawable().getInstrinsicWidth()getIntrinsicHeight()将返回原始尺寸.
  • 获得Drawable通过ImageView.getDrawable()并强制转换为BitmapDrawable,然后用BitmapDrawable.getBitmap().getWidth()getHeight()也返回原来的图像和它的尺寸.

获得所显示图像的实际尺寸的唯一方法是提取并使用Matrix用于显示图像的变换.这必须在测量阶段之后进行,这里的例子显示了它在叫OverrideonMeasure()自定义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);
    }

}  
Run Code Online (Sandbox Code Playgroud)

注意:为了Matrix从代码中获得图像转换(例如在一个中Activity),函数是ImageView.getImageMatrix()- 例如myImageView.getImageMatrix()


Que*_* S. 27

我扩展了BT的答案,从中生成静态方法,并将图像左侧和顶部位置包含在ImageView中:

/**
 * Returns the bitmap position inside an imageView.
 * @param imageView source ImageView
 * @return 0: left, 1: top, 2: width, 3: height
 */
public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
    int[] ret = new int[4];

    if (imageView == null || imageView.getDrawable() == null)
        return ret;

    // Get image dimensions
    // Get image matrix values and place them in an array
    float[] f = new float[9];
    imageView.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 = imageView.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);

    ret[2] = actW;
    ret[3] = actH;

    // Get image position
    // We assume that the image is centered into ImageView
    int imgViewW = imageView.getWidth();
    int imgViewH = imageView.getHeight();

    int top = (int) (imgViewH - actH)/2;
    int left = (int) (imgViewW - actW)/2;

    ret[0] = left;
    ret[1] = top;

    return ret;
}
Run Code Online (Sandbox Code Playgroud)


Squ*_*ear 10

我发现WarrenFaith建议使用setAdjustViewBounds,但我不得不将ImageView的layout_width/layout_height更改为'wrap_content'(使用'match_parent',setAdjustViewBounds什么也没做).为了获得我想要的高度/宽度/重力行为,我必须将ImageView包装在FrameLayout中:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

执行此操作后,ImageView的尺寸(由getWidth()和getHeight()返回)与图像的显示大小相匹配.


Pet*_*ego 6

尝试

ImageView.getDrawable().getIntrinsicHeight()
ImageView.getDrawable().getIntrinsicWidth()
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,此答​​案将返回绘制的位图的尺寸,而不是实际显示的尺寸或"显示尺寸". (2认同)

bhu*_*ups 0

尝试覆盖onMeasure(int widthMeasureSpec, int heightMeasureSpec)而不是 onSizeChanged。