I've been looking for over a day for a solution to this problem but nothing helps, even the answers here. Documentation doesn't explain anything too.
I am simply trying to get a rotation in the direction of another object. The problem is that the bitmap is not rotated around a fixed point, but rather around the bitmaps (0,0).
Here is the code I am having troubles with:
Matrix mtx = new Matrix();
mtx.reset();
mtx.preTranslate(-centerX, -centerY);
mtx.setRotate((float)direction, -centerX, -centerY);
mtx.postTranslate(pivotX, pivotY);
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true);
this.bitmap = rotatedBMP;
The weird part is, it doesn't matter how I change the values within pre
/postTranslate()
and the float arguments in setRotation()
. Can someone please help and push me in the right direction? :)
Edited: optimized code.
To get Bitmap from resources:
Look at the sample from Google called Lunar Lander, the ship image there is rotated dynamically.
Lunar Lander code sample
I hope the following sequence of code will help you:
If you check the following method from
~frameworks\base\graphics\java\android\graphics\Bitmap.java
this would explain what it does with rotation and translate.
I used this configurations and still have the problem of pixelization :
You can use something like following: