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];
这篇关于缩放后获取可绘制的显示大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:缩放后获取可绘制的显示大小
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01