作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个自定义 View (1066 x 738),我正在传递一个位图图像 (720x343)。我想缩放位图以适应自定义 View 而不超出父级的范围。
我想实现这样的目标:
我应该如何计算位图大小?
我如何计算新的宽度/高度:
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);
}
但我所做的只是:
最佳答案
您应该尝试使用为 ScaleToFit.CENTER
构建的转换矩阵。例如:
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER);
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
关于Android调整位图大小保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961797/
我有一张 table 。我希望它的数据垂直和水平居中。 我使用了 align="center"valign="middle" ,但它没有用。
我是一名优秀的程序员,十分优秀!