Android, move bitmap along a path?

后端 未结 3 1847
無奈伤痛
無奈伤痛 2020-11-27 21:23

I would like to know if it\'s possible to select coordinates from a path to draw a bitmap over time, for example, I have an image of a sun, and I would like to move it, over

3条回答
  •  盖世英雄少女心
    2020-11-27 22:03

    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;
        };
    };
    

提交回复
热议问题