【问题标题】:Get image dimensions after it draws in screen绘制屏幕后获取图像尺寸
【发布时间】:2015-02-19 12:26:59
【问题描述】:

我有一个ImageView,高度和宽度为MathParent
在我的活动中,它会将图片从资源加载到ImageView。缩放后如何获取ImageView 中图片的宽度和高度。
我没有在 XML 中设置 android:scaleType
我的意思是这些尺寸!

【问题讨论】:

  • 你为什么要这个?因为可能有比知道图像大小更简单的解决方案。
  • @weston 我需要这个,因为我想在这张图片的固定尺寸上向用户展示一些特定的点。由于我的应用程序将在不同的屏幕尺寸上运行,我不能在我的代码中使用 dppx
  • 是的,所以你不需要大小,将点与我的回答中看到的矩阵映射。

标签: android imageview scale


【解决方案1】:

您可以使用视图用来显示图像的matrix 做很多事情。

这里我计算了绘制图像的比例:

private float scaleOfImageView(ImageView image) {
    float[] coords = new float[]{0, 0, 1, 1};
    Matrix matrix = image.getImageMatrix();
    matrix.mapPoints(coords);
    return coords[2] - coords[0]; //xscale, method assumes maintaining aspect ratio
}

将比例应用于图像尺寸会给出显示的图像尺寸:

private void logImageDisplaySize(ImageView image) {
    Drawable drawable = image.getDrawable();
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    float scale = scaleOfImageView(image);
    float displayedWidth = scale * width;
    float displayedHeight = scale * height;
    Log.d(TAG, String.format("Image drawn at scale: %.2f => %.2f x %.2f",
            scale, displayedWidth, displayedHeight));
}

我怀疑你并不真正关心图像大小,我怀疑你想将触摸点映射回图像上的坐标,这个答案显示了如何做到这一点(也使用图像矩阵):https://stackoverflow.com/a/9945896/360211

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2015-02-28
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    相关资源
    最近更新 更多