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];
这篇关于缩放后获取可绘制的显示大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:缩放后获取可绘制的显示大小
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01