0

I'm trying to transfer my bitmap from using rectF translation to using matrix rotation and translation. I feel like I've tried every combination of pre,set,post for rotate and translate. This is a sample of my code

Matrix m;

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, m, null);
}

onTouchEvent(MotionEvent event)
{
m.setRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2); //rotation points to your finger

        if (fingerx1 > bitmapX) {
            xspeed = xspeed + accel;
        }
        if (fingerx1 < bitmapX) {
            xspeed = xspeed - accel;
        }
        if (fingery1 < bitmapY) {
            yspeed = yspeed - accel;
        }
        if (fingery1 > bitmapY) {
            yspeed = yspeed + accel;
        }

}

private Runnable runny = new Runnable() {
    public void run() {
        m.postTranslate(xspeed, yspeed);
        handler.postDelay(this, 50);
    }
};

Not the final code since I don't know which pre,set,post to use in these situations. I feel like this would be the solution though (setRotate, postTranslate).

With this combination, the bitmap looks like it tries to but can't translate.

When I've tried setTranslate(bitmapX + xspeed, bitmapY + yspeed) and postRotate it gives me the right translation but crazy rotation. Using both setRotate, setTranslate gives me crazy glitches. Adding pretranslate and prerotate to the mix just gave me a headache.

TL;DR: Ugh.

EDIT:

bitmapX = playerValues[Matrix.MTRANS_X]; 
bitmapY = playerValues[Matrix.MTRANS_Y];

After trying postRotate and postTranslate together, I see my bitmap rotate around it's center at first and then it moves out of the 0,0 position and continue to rotate around the 0,0 center.x,center.y position.

Basically I need to fix my rotate's center x,y to make them dynamically change with my bitmap's location.

Thanks pskink, I was having a very bad case of matricides.

stegrd
  • 25
  • 6
  • and what exactly are you trying to achieve? – pskink Jul 03 '14 at 14:49
  • Sorry, I must be tired. I'm trying to rotate the bitmap to point towards the finger (so the rotation must center around the bitmap) and translate towards the finger (hence the xspeed and yspeed) – stegrd Jul 03 '14 at 14:50
  • maybe my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue could be helpful, it uses translate, scale and rotate, all using the Matrix – pskink Jul 03 '14 at 15:01
  • I updated my question, all I needed was to get my bitmap's center X and Y! I could use a rectF to wrap it? I would use canvas.drawBitmap to do so, but I'm using that to draw my bitmap to my matrix. Do you know how else I could wrap it or extract the drawable coordinates? – stegrd Jul 03 '14 at 16:25
  • Do you think you could write an example? I'm getting nowhere, i've tried writing the rectf to (0,0,imagewidth,imageheight) then maprect... Perhaps it would be easier to show my old code where I used rectF to move the bitmap – stegrd Jul 03 '14 at 17:14
  • I was thinking so differently before, I am sorry for wasting your time :/ It just clicked that matrix is basically a rect that can be rotated. Thanks for putting up with my slowness :) – stegrd Jul 03 '14 at 17:42
  • I was probably tired before, but now I know what my problem was. postRotate just adds to the current angle, where setrotate is what I want (setting the angle to a specific degree). postRotate is what I want, though. – stegrd Jul 04 '14 at 13:52

0 Answers0