位图 - 矩阵操作(缩放、旋转和平移)

dam*_*ian 2 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);
Run Code Online (Sandbox Code Playgroud)

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

Chi*_*ode 5

试试这个代码:

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());
Run Code Online (Sandbox Code Playgroud)