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

21

代码很简单:

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

注意ImageView使用了fill_parent来设置宽度和高度。

图片cat很小,会被放大以适应ImageView,并保持宽高比。

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

imageView.getDrawable().getIntrinsicHeight()

但是猫图片的原始高度是多少呢?

我尝试过:

imageView.getDrawable().getBounds()

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


你可以使用ViewTreeObserver在运行时获取任何视图的真实尺寸。http://developer.android.com/reference/android/view/ViewTreeObserver.html - Arpit Garg
谢谢,我正在看。顺便问一下:这是获取大小的最佳/唯一方法吗? - Freewind
1
抱歉,即使使用了ViewTreeObserver,我仍然不知道如何获取图像的显示大小。 - Freewind
4个回答

56

以下内容可行:

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)现在代表了视图中图像的实际重新缩放后的尺寸(换句话说,图像的显示大小)。


编辑:我觉得更好的写法(并适用于整数)如下:

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);

别忘了,用整数除以整数会得到整数结果。 :) - abbath
1
如何获取位图的高度和宽度。我的意思是我们从哪里可以获取位图以及如何获取。 - Pravesh
@androiddeveloper 根据您编辑的答案,如何获取位图高度和宽度? - Pragya Mendiratta
1
嗨,@androiddeveloper,如果图像大小为200X50,imageView大小为100X100,那么使用您刚才建议的方法,图像的尺寸将是多少?还有一件事,imageView设置为adjustViewBound=true怎么办? - Nirmal Prajapat
哦,谢谢,上帝。你做得很好。 - newbie
显示剩余4条评论

8
这是一个帮助函数,用于获取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;
}

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
    // after check whether height or width needs adjusting
    if ((float) startBounds.width() / startBounds.height() < (float) realwidth / realheight) {
        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 {
        int adjustedwidth = realwidth*startBounds.height()/realheight;
        int adjustedoffset = (startBounds.width()-adjustedwidth) / 2;
        startScale = (float) startBounds.height() / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - adjustedwidth) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
        startBounds.offset(+adjustedoffset, +0);
    }
} else {
    // Extend start bounds vertically
    // after check whether height or width needs adjusting
    if ((float) startBounds.width() / startBounds.height() > (float) realwidth / realheight) {
        int adjustedwidth = realwidth*startBounds.height()/realheight;
        int adjustedoffset = (startBounds.width()-adjustedwidth) / 2;
        startScale = (float) adjustedwidth / finalBounds.width();
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - startBounds.height()) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
        startBounds.offset(+adjustedoffset, +0);
    } else {
        int adjustedheight = realheight*startBounds.width()/realwidth;
        int adjustedoffset = (startBounds.height()-adjustedheight) / 2;
        startScale = (float) startBounds.width() / finalBounds.width();
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - adjustedheight) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
        startBounds.offset(+0, +adjustedoffset);
    }
}

非常好用,不客气 :)

进一步解释:通常我们会检查图片是否比宽度更高(扩展后的图片高度应与 expandedImageView 的高度匹配)或反之。然后我们检查原始(较小)ImageView(thumbView)中的图片是否与宽度或高度匹配,以便我们可以调整空间。 这样,我们就可以实现平滑的缩放动画,同时不裁剪 thumbView 中的图片,无论其尺寸如何(当使用约束时,它们可能会因设备而异)或图片的尺寸。


1
实际上,我来自一个OCR库,该库返回相对于原始图像的单词位置,并且我必须将它们放置在缩放后的“ImageView”上。 不过,你的起源故事很不错。 - Abandoned Cart

-1

使用

// 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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接