Android, move bitmap along a path?

Yes, it’s possible to move image along path. I will provide simple solution to show the principle. The following code will move and rotate your image. If you don’t need rotation remove the TANGENT_MATRIX_FLAG flag.

import android.graphics.*;
//somewhere global
int iCurStep = 0;// current step

//don't forget to initialize
Path pathMoveAlong = new Path();
private static Bitmap bmImage = null;

@Override
protected void onDraw(Canvas canvas) {
    Matrix mxTransform = new Matrix();
    PathMeasure pm = new PathMeasure(pathMoveAlong, false);
    float fSegmentLen = pm.getLength() / 20;//20 animation steps

    if (iCurStep <= 20) {
        pm.getMatrix(fSegmentLen * iCurStep, mxTransform,
            PathMeasure.POSITION_MATRIX_FLAG + PathMeasure.TANGENT_MATRIX_FLAG);
        canvas.drawBitmap(bmImage, mxTransform, null);
        iCurStep++;
        invalidate();
    } else {
        iCurStep = 0;
    };
};

Leave a Comment