【问题标题】:Inverse drawing of Bitmap on Canvas在 Canvas 上逆向绘制 Bitmap
【发布时间】:2012-08-07 19:28:24
【问题描述】:

如何将绘制到 Canvas 中的图像反转?

我有以下:

        canvas.save();
        canvas.drawBitmap(image.current(), null, currentBounds(), Paints.BLANK);
        canvas.restore();

如何使当前图像在 x 轴上翻转到 currentBounds() 中?

我已经找到了一些表明使用 Matrix 的答案,但我想知道是否有更简单的方法?这样的画图打开了一些标志。

编辑:

以下是我对矩阵变换的尝试:

    Rect currentBounds = currentBounds();


    currentBounds.offset((int)offset.x(), (int)offset.y());

    float scale = image.current().getWidth() / currentBounds.width();

    Matrix matrix = new Matrix();
    matrix.setScale(- scale - 1, scale + 1);
    matrix.postTranslate(currentBounds.left, currentBounds.top);

    canvas.drawBitmap(image.current(), matrix, Paints.BLANK);
    canvas.drawRect(currentBounds, Paints.STROKE_BLUE);

本次抽签结果如下:

https://dl.dropbox.com/u/28683814/game.png

可以看出,精灵正在从 0,0 向左绘制,并且没有完全完成 currentBounds(),我做错了什么?

【问题讨论】:

  • 我认为this 是最好的方法。它使用矩阵,但并不难。
  • 使用矩阵应该是直截了当的。
  • 可能是最好的方法,请阅读我编辑的答案以查看新错误

标签: android android-canvas


【解决方案1】:

使用

canvas.scale(-1,0,width/2,height/2);

正如我自己的回答 here 所述。

不过,我要指出的是,您需要大约一分钟的时间来理解矩阵,它会让您成为一个更好的 Android 程序员。

【讨论】:

  • 实际上,这使我的绘制消失了我了解矩阵,我只是想知道我会如何处理游戏上应用的比例(边界不是原始位图大小),但可以做到也经过矩阵。我试试看。
【解决方案2】:

使用这个。我认为这很容易。

        canvas.save();
        canvas.rotate(180, x, y);
        canvas.drawBitmap(bitmap, x, y, null);
        canvas.restore();

【讨论】:

  • 这个旋转,我需要翻转当前矩阵
【解决方案3】:

嗯,我是这样解决的:

    Rect currentBounds = currentBounds();
    currentBounds.offset((int)offset.x(), (int)offset.y());

    float scale = (float) currentBounds.width() / (float) image.current().getWidth();

    boolean leftMovement = movingLeft();

    Matrix matrix = new Matrix();
    matrix.setScale(leftMovement ? -scale : scale, scale);
    matrix.postTranslate(leftMovement ? currentBounds.right : currentBounds.left, currentBounds.top);

    canvas.drawBitmap(image.current(), matrix, Paints.BLANK);

但是这个“leftMovement ? currentBounds.right : currentBounds.left”看起来不对

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2013-06-08
    相关资源
    最近更新 更多