获取绘制显示尺寸缩放后

获取绘制显示尺寸缩放后

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

问题描述

下面是我的问题:
怎样才能获得绘制的显示尺寸的ImageView的内侧scalling后像素,假设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?

很多方法只返回绘制对象的原始大小或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-titre.png

1 - > ImageView的大小
2 - >我想在像素缩放后的图像尺寸

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

感谢你。

推荐答案

从您发布的图片我假设你有FIT_CENTER的scaleType(或CENTER_INSIDE比ImageView的绘制大)在您的ImageView

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

注:code没有简化,可读性

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

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

09-02 13:30