获取ImageView中图像的显示大小

时间:2012-09-17 16:09:05

标签: android image drawable image-size

代码很简单:

<ImageView android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:src="@drawable/cat"/>

请注意,ImageView使用fill_parent表示宽度和高度。

图像cat是一个小图像,它将被放大以适合ImageView,同时保持宽高比。

我的问题是如何获得图像的显示尺寸?我试过了:

imageView.getDrawable().getIntrinsicHeight()

但它是图像的原始高度cat

我试过了:

imageView.getDrawable().getBounds()

但是返回Rect(0,0,0,0)

4 个答案:

答案 0 :(得分:42)

以下内容将起作用:

ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image

if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView

(iw x ih)现在表示视图中图像的实际重新缩放(宽度x高度)(换句话说,显示图像的大小)


编辑:我认为编写上述答案的更好方法(以及使用整数的答案):

            final int actualHeight, actualWidth;
            final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
            final int bitmapHeight = ..., bitmapWidth = ...;
            if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
                actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
                actualHeight = imageViewHeight;
            } else {
                actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
                actualWidth = imageViewWidth;
            }

            return new Point(actualWidth,actualHeight);

答案 1 :(得分:3)

这是一个帮助函数,用于获取imageView中的图像边界。

/**
 * Helper method to get the bounds of image inside the imageView.
 *
 * @param imageView the imageView.
 * @return bounding rectangle of the image.
 */
public static RectF getImageBounds(ImageView imageView) {
    RectF bounds = new RectF();
    Drawable drawable = imageView.getDrawable();
    if (drawable != null) {
        imageView.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds()));
    }
    return bounds;
}

答案 2 :(得分:0)

我想很多人都来自这个示例https://developer.android.com/training/animation/zoom.html,并且不想使用android:scaleType="centerCrop"(也许因为ImageView是布局中的约束),您不用担心,我回来了!

只需替换以

开头的整个块
// Adjust the start bounds to be the same aspect ratio as the final
// bounds using the "center crop" technique.

带有以下内容

//adjust for scaled image to constraint
    int realheight = ResourcesCompat.getDrawable(getResources(),imageResId,null).getIntrinsicHeight();
    int realwidth = ResourcesCompat.getDrawable(getResources(),imageResId,null).getIntrinsicWidth();

    // Adjust the start bounds to be the same aspect ratio as the final
    // bounds using ueen's adjusteddimensions technique. This prevents undesirable
    // stretching during the animation. Also calculate the start scaling
    // factor (the end scaling factor is always 1.0).
    float startScale;
    if ((float) finalBounds.width() / finalBounds.height()
            > (float) startBounds.width() / startBounds.height()) {
        // Extend start bounds horizontally
        int adjustedheight = realheight*startBounds.width()/realwidth;
        int adjustedoffset = (startBounds.height()-adjustedheight) / 2;
        startScale = (float) adjustedheight / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - startBounds.width()) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
        startBounds.offset(+0, +adjustedoffset);
    } else {
        // Extend start bounds vertically
        int adjustedwidth = realwidth*startBounds.height()/realheight;
        int adjustedoffset = (startBounds.width()-adjustedwidth) / 2;
        startScale = (float) adjustedwidth / finalBounds.width();
        Log.d("dfs", startBounds.width()+" - "+realwidth+" - " + adjustedwidth);
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - startBounds.height()) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
        startBounds.offset(+adjustedoffset, +0);
    }

像一个烟囱一样工作, 不客气:)

答案 3 :(得分:-2)

使用

// For getting imageview height
imgObj.getMeasuredHeight()


// For getting imageview width
imgObj.getMeasuredWidth();


//For getting image height inside ImageView
 imgObj.getDrawable().getIntrinsicHeight();


//For getting image width inside ImageView
 imgObj.getDrawable().getIntrinsicWidth();