0

I'm moving a bitmap along a path on a Canvas. The path has various curves and the bitmap follows along. pm.getMatrix does a really lovely job of handling the position and rotation adjustments along the path when its passed the PathMeasure.POSITION_MATRIX_FLAG and TANGENT_MATRIX_FLAG, however, it rotates the bitmap pivoted on the 0,0 coordinate. I need it to pivot on the center of the bitmap.

I cracked open the matrix in the debugger, and it appears that there is indeed *no spoon. There is however 3 arrays of floats, each containing 3 floats. I'm guessing that if I can get those values, I can probably figure out which of them describes the rotation of the object, and there's probably some way to alter the pivot point? I see no other way to do it... Would love some guidance on at least what those three float arrays actually describe.

PathMeasure pm = new PathMeasure(playerPath, false);
float fSegmentLen = pm.getLength() / numSteps;
Matrix mxTransform = new Matrix();

pm.getMatrix(fSegmentLen * iCurStep, mxTransform,
                    PathMeasure.POSITION_MATRIX_FLAG + PathMeasure.TANGENT_MATRIX_FLAG );
canvas.drawBitmap(playerCar, mxTransform, null);
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

4

try this:

private void setDrawingMatrix(float distance) {
    pm.getMatrix(distance, mxTransform, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
    mxTransform.preTranslate(-playerCar.getWidth() / 2.0f, -playerCar.getHeight() / 2.0f);
}

and then in onDraw method:

canvas.drawBitmap(playerCar, mxTransform, null);

happy driving...

pskink
  • 23,874
  • 6
  • 66
  • 77
  • that totally works... thanks... if you have a moment to explain what it's actually doing, I'd really appreciate it... why does moving the car over -1/2width and -1/2height make it rotate on its center? – Yevgeny Simkin Jun 04 '13 at 22:18
  • dont worry if you dont get it, (the docs dont help much...) it is still a kind of black magic to me, but reading this http://jaypthakkar.blogspot.com/2013/04/the-role-of-transformation-matrices-in.html?m=1 may help... – pskink Jun 04 '13 at 22:41
  • btw, see the difference if you use postTranslate vs preTranslate – pskink Jun 04 '13 at 22:44
  • funny... I'm actually familiar with the stuff at that link. This particular translation is more mysterious because I have no visibility into what pm.getMatrix() actually does. However, I suspect that the reason it works as it does is that preTranslate affects the values that were there to begin with, so, in essence, it shifts the car over by 1/2 its with on both axis and then the rotation still *thinks it's rotating the 0,0 coordinate, it's just that now the center of the car is at that coordinate. (at least that's my guess). – Yevgeny Simkin Jun 04 '13 at 23:41
  • i couldnt explain better :) its a pity that docs are so poor in that matter the best thing is just to call pre/pist Teanslate/Scale/Rotate and see how this affect car's path in comparison to the road – pskink Jun 05 '13 at 08:14
  • speaking of matrices, see below post how cute things you can do with them: http://stackoverflow.com/questions/16729169/how-to-maintain-multi-layers-of-imageviews-and-keep-their-aspect-ratio-based-on/ – pskink Jun 06 '13 at 06:49