19

代码很简单:

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

fill_parent注意用于宽度和高度的 ImageView 。

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

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

imageView.getDrawable().getIntrinsicHeight()

但它是图像的原始高度cat

我试过:

imageView.getDrawable().getBounds()

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

4

4 回答 4

55

以下将起作用:

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);
于 2012-11-10T01:51:58.520 回答
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;
}
于 2017-07-04T06:00:42.660 回答
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 中裁剪图片,无论它的尺寸(因为它们可能会在使用约束时从设备到设备发生变化)或图片的尺寸。

于 2019-05-14T15:28:44.037 回答
-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();
于 2012-09-17T16:59:52.323 回答