1

here's my problem:
How can get the displayed size of the drawable inside an ImageView in pixel after scalling, assuming that the ImageView size isn't equal to the scaled drawable?

A lot of methods just return the original size of the Drawable or just the size of the ImageView.

Here is a picture describing what I want :

http://image.noelshack.com/fichiers/2013/06/1360144144-sans-titre.png

1 --> ImageView Size
2 --> The scaled image size that I want to get in px

Thank you.

4

1 回答 1

3

从您发布的图像中,我假设您的 ImageView 上有 FIT_CENTER 的 scaleType(或 CENTER_INSIDE,可绘制比 imageView 大)

所以你可以像这样计算显示的大小

    boolean landscape = imageView.getWidth() > imageView.getHeight();

    float displayedHeight;
    float displayedWidth;

    if (landscape){
        displayedHeight = imageView.getHeight();
        displayedWidth = (float) drawableWidth * imageView.getHeight() / drawableHeight;
    } else {
        displayedHeight = (float) drawableHeight * imageView.getWidth() / drawableWidth;
        displayedWidth = imageView.getWidth();
    }

注意:为了便于阅读,代码没有简化。

可以应用于所有 scaleTypes 的另一种更强大的方法是使用 imageView 用来缩放图像的矩阵

float[] f = new float[9];
imageView.getImageMatrix().getValues(f);
float displayedWidth = drawableWidth * f[Matrix.MSCALE_X];
float displayedHeight = drawableHeight * f[Matrix.MSCALE_Y];
于 2015-01-16T10:58:17.280 回答