缩放后获取可绘制的显示大小

Get drawable displayed size after scaling(缩放后获取可绘制的显示大小)

本文介绍了缩放后获取可绘制的显示大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:
假设 ImageView 大小不等于缩放后的可绘制对象,如何在缩放后以像素为单位获取可绘制对象的显示大小?

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?

很多方法只是返回Drawable的原始大小或者只是ImageView的大小.

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-tire.png

1 --> ImageView 大小
2 --> 我想以px为单位的缩放图像大小

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

谢谢.

推荐答案

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

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.

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

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];

这篇关于缩放后获取可绘制的显示大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:缩放后获取可绘制的显示大小

基础教程推荐