user10030086
user10030086

Reputation:

Android rotation matrix not rotating around a point

I have been researching how to rotate Bitmaps in android, the only one I could find was using a Matrix. This works for rotations under 90 degrees, but it seems every 90 degrees the rotation point wobbles. Heres my code, if you want me to post an image/gif HERE of the result please say so. Thank you for your help

    Matrix leftMat = new Matrix();
    leftMat.setRotate ((float) (debug),thrustWidth,0);
    Bitmap leftThruster = Bitmap.createBitmap (thruster,0,0, thruster.getWidth(),thruster.getHeight (),leftMat,true);
    leftThruster.setHasAlpha(true);
    canvas.drawBitmap(leftThruster,(int) (shipX +leftCords[0]),(int) (shipY+leftCords[1]-cameraY),null);

EDIT: It seems that the corners of the image try to meet up with the corners of the "bounding box" but I can't seem to get more than that.

EDIT: Updated code (Not exact copy, but hopefully it gives everything that's important, I also replaced all the variables with constants where applicable for clarity)

    private Bitmap thruster = BitmapFactory.decodeResource(context.getResources(),R.drawable.thruster);
    private float debug = 0;
    public void draw(Canvas canvas) {
        debug ++;
        Matrix leftMat = new Matrix();  
        leftMat.postRotate(debug,0,0);
        Bitmap leftThruster = Bitmap.createBitmap (thruster,0,0, thruster.getWidth(),thruster.getHeight(),leftMat,false);
        leftThruster.setHasAlpha(false);
        canvas.drawBitmap(leftThruster, 300, 300,null);

Upvotes: 1

Views: 923

Answers (2)

UdayaLakmal
UdayaLakmal

Reputation: 4223

Checkout this sample app that demo rotate image with matrix. that I previously implemented.

https://github.com/UdayaLakmal/GlideImageRotate

public Bitmap RotateBitmap(Bitmap source, float angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

that will return rotated bitmap with angle it pass.

Upvotes: 0

user10030086
user10030086

Reputation:

UPDATE: I found a solution, though I don't know why that code doesn't work, I've found an alternate. Instead of canvas.drawBitmap(leftThruster, 300, 300,null); I replaced it with canvas.drawBitmap(thruster,leftMat,null); While also adding leftMat.postTranslate(300,300); to leftMat.

Upvotes: 1

Related Questions