位图 - 矩阵运算(缩放,旋转和平移)

时间:2013-08-10 14:25:24

标签: android matrix android-canvas

我需要一些关于矩阵运算的帮助。我想要实现的是:

  • 缩小
  • 移至特定位置
  • 在某种程度上旋转(位图的中心)

我的代码目前看起来像这样:

            Matrix matrix = new Matrix();
            matrix.preRotate(mShip.getRotation(), mShip.getX() + mShip.getCurrentBitmap().getWidth()/2f, mShip.getY()  + mShip.getCurrentBitmap().getHeight()/2f);
            matrix.setScale((1.0f * mShip.getWidth() / mShip.getCurrentBitmap().getWidth()), (1.0f * mShip.getHeight() / mShip.getCurrentBitmap().getHeight()));
            matrix.postTranslate(mShip.getX(), mShip.getY());
            mCanvas.drawBitmap(mShip.getCurrentBitmap(), matrix, mBasicPaint);

但旋转错误的中心,我无法弄清楚如何解决这个问题 - 我已经环顾四周但是只发现了类似的问题,没有解决方案。 我认为我可能不得不将其中一个操作应用于另一个操作,因为它们是按序列执行的,但我无法弄清楚如何操作。

1 个答案:

答案 0 :(得分:4)

试试这段代码:

Matrix matrix = new Matrix();
matrix.setTranslate(-mShip.getCurrentBitmap().getWidth()/2f, -mShip.getCurrentBitmap().getHeight()/2f);
matrix.postRotate(mShip.getRotation());
matrix.postTranslate(mShip.getX(), mShip.getY());
matrix.postScale((1.0f * mShip.getWidth() / mShip.getCurrentBitmap().getWidth()), (1.0f * mShip.getHeight() / mShip.getCurrentBitmap().getHeight()), mShip.getX(), mShip.getY());