【问题标题】:Android resize bitmap keeping aspect ratio [duplicate]Android调整位图大小保持纵横比[重复]
【发布时间】: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);
    }

但我所做的只是:

【问题讨论】:

标签: android bitmap scale


【解决方案1】:

您应该尝试使用为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);

【讨论】:

  • 不适合我。我尝试使用它来减小图像大小、分辨率,但没有奏效。
  • @DroidWormNarendra 你可以打电话给Bitmap.createScaledBitmap(out, reqWidth, reqHeight, false); ,这将减小图像大小。
  • @matiash ,请看一下这个问题stackoverflow.com/questions/42927337/…
  • 为我工作..
猜你喜欢
  • 2013-06-26
  • 2012-04-15
  • 2012-05-01
  • 2012-11-15
  • 1970-01-01
相关资源
最近更新 更多