Get drawable displayed size after scaling

无人久伴 提交于 2019-12-20 04:15:30

问题


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.


回答1:


From the image you posted I assume you have scaleType of FIT_CENTER (or CENTER_INSIDE with drawable bigger than imageView) on your ImageView

so you can calculate displayed size like so

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

Note: code not simplified for readability.

Another more robust approach that can be applied to all scaleTypes is to use the matrix that imageView used to scale the image

float[] f = new float[9];
imageView.getImageMatrix().getValues(f);
float displayedWidth = drawableWidth * f[Matrix.MSCALE_X];
float displayedHeight = drawableHeight * f[Matrix.MSCALE_Y];


来源:https://stackoverflow.com/questions/14726004/get-drawable-displayed-size-after-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!