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];
这篇关于缩放后获取可绘制的显示大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:缩放后获取可绘制的显示大小
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
