【问题标题】:How may I know the size of an image that has been scaled?我如何知道已缩放的图像的大小?
【发布时间】:2015-04-27 18:37:26
【问题描述】:

我有一个在 XML 中定义如下的 ImageView。它位于自定义 ViewPager 中。

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:scaleType="fitXY"/>

它就像一个魅力,但我需要知道图像的实际高度,在它被缩放之后。我正在使用自定义 ViewPager 的 onMeasure 方法中的值,如下所示:

View child = getChildAt(0);

child.measure(widthMeasureSpec, MeasureSpec
    .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

int h = child.getMeasuredHeight();

我得到的高度不是缩放后的图像高度,而是缩放前图像的原始尺寸。

谢谢。

【问题讨论】:

    标签: android imageview runtime size scale


    【解决方案1】:

    您覆盖 onMeasure 函数以获取图像矩阵并找到比例值。像这样

    float[] f = new float[9];
    getImageMatrix().getValues(f);
    final float scaleX = f[Matrix.MSCALE_X];
    final float scaleY = f[Matrix.MSCALE_Y];
    
    // Get the drawable's real width and height
    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);
    log.v("actual dimensions",""+actW+actH);
    

    来源:https://stackoverflow.com/a/15538856/4743812

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-19
      • 2016-04-13
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多