【发布时间】:2014-09-17 15:59:33
【问题描述】:
我有一个自定义视图 (1066 x 738),我正在传递一个位图图像 (720x343)。我想缩放位图以适应自定义视图而不超出父级的范围。
我想实现这样的目标:
我应该如何计算位图大小?
我如何计算新的宽度/高度:
public static Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight)
{
int bWidth = b.getWidth();
int bHeight = b.getHeight();
int nWidth = reqWidth;
int nHeight = reqHeight;
float parentRatio = (float) reqHeight / reqWidth;
nHeight = bHeight;
nWidth = (int) (reqWidth * parentRatio);
return Bitmap.createScaledBitmap(b, nWidth, nHeight, true);
}
但我所做的只是:
【问题讨论】:
-
您可以在自定义视图的 XML 中添加
scaleType标志。 developer.android.com/reference/android/widget/… -
它不是 imageView !
-
啊,我(错误地)假设您的自定义视图扩展了 ImageView...我的错