【问题标题】:How to scale Bitmap drawn on the Canvas while saving rotation and translation specified with Matrix如何在保存使用 Matrix 指定的旋转和平移时缩放画布上绘制的位图
【发布时间】:2016-06-11 05:31:40
【问题描述】:

Sample Image

大家好。我是使用矩阵在 android 中进行图像处理的新手。我正在开发一个在设备屏幕上显示位图 #1 的应用程序。

位图 #1 非常大,大约 2592 X 1456,但已按比例缩小以适应设备的屏幕尺寸,仅用于显示目的。

然后我在 Bitmap #1 Canvas 上绘制了嘴唇 Bitmap (#2),使用矩阵(带有旋转、缩放、平移),如上图所示。

正是我想要实现的是保存最终位图的副本,向后缩放到原始大小 (2592 x 1456)。

我尝试通过缩放 Bitmap #1 矩阵来实现它。

这是我迄今为止尝试过的:

    // adjust the matrix to the original image size
    // new matrix big
    Matrix newMatrix = new Matrix(); 
    // copy matrix from small matrix
    newMatrix = matrix; 
    RectF src = new RectF(0,0,backgroundImage.getWidth(), backgroundImage.getHeight());
    RectF dst = new RectF(0,0, origBackground.getWidth(), origBackground.getHeight());
    newMatrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
    canvas.drawBitmap(bitmap, newMatrix, paint);

我的问题是,在生成的位图 #1 中,嘴唇位图 (#2) 被放置在 x=0 和 y=0 处,而不是在所需的坐标处,缺少指定的旋转。

【问题讨论】:

  • 嗨@xAF。这是在画布上绘制的。我没有使用任何自动适应屏幕的小部件。我正在使用矩阵来做到这一点。
  • 请查看我建议的编辑(希望我能很好地理解您的问题)

标签: android canvas matrix matrix-multiplication


【解决方案1】:

出于缩放目的,更好的选择是使用 Canvas 自己的 save()restore() 方法。

this answer

为了您的目的,使用translate(x,y) 在特定坐标处绘制,rotate(deg,x,y) 在固定在特定坐标处的特定旋转角度处绘制。

希望这将有助于解决您的问题。

【讨论】:

  • 嗨@xAF 这是正确的,但我希望它在矩阵解决方案中。因此,我将您的答案转换为矩阵解决方案。以上是我的回答。谢谢
【解决方案2】:

经过一个小时的尝试。我终于得到了答案。下面是我一直在使用的源代码。感谢@xAF。

干杯,

public Bitmap saveBitmap()
   {
    Bitmap bm = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(bm);
    // draw the background photo with its original size
    mCanvas.drawBitmap(bitmap1, 0, 0, null);

    // put the lips and adjust the matrix to the original image size
    Matrix newMatrix = new Matrix(); // new big matrix
    // copy the screen small matrix (with rotate, scale, translation)
    newMatrix.set(matrix);
    float scaleWidth = ((float) bitmap1.getWidth()) / bitmap2.getWidth();
    float scaleHeight = ((float) bitmap1.getHeight()) / bitmap2.getHeight();

    newMatrix.postScale(scaleWidth, scaleHeight, 0, 0);
    mCanvas.drawBitmap(bitmapLips, newMatrix, paint);

    return bm;
}

【讨论】:

    【解决方案3】:

    感谢这样的 setRectToRect 函数,我设法做到了:

     val matrix = Matrix().apply {
                matrix.setRectToRect(
                    RectF(0f, 0f, icon.getWidth().toFloat(), icon.getHeight().toFloat()),
                    RectF(0f, 0f, 100f, 100f),
                    Matrix.ScaleToFit.CENTER
                )
                matrix.postTranslate(centerX - 50f, centerX - 50f)
            } 
     canvas.drawBitmap(
                icon,
                matrix,
                Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG)
            )
    

    在这里我用宽度 = 100f 和高度 = 100f 调整位图的大小 然后我仍然可以翻译或旋转它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多