在动画路径上更改绘画(android)

时间:2012-08-08 19:47:57

标签: android path

我有一个Path和一个遵循实现路径的对象。如何使移动物体在它已经过的路径上描绘另一种颜色。

这里是图片的链接(可能更好地解释了我的意思):moving object on path

thx求助:)

1 个答案:

答案 0 :(得分:0)

以下是一个如何完成的示例:)

public class MainActivity extends Activity  {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View {
        private Paint mPaint,followingPaint;
        private Path mPath=new Path();
        private PathEffect mEffect;
        private float mPhase;
        private PathMeasure pm;
        private float pmLen; 

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(10);
            followingPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
            followingPaint.setStyle(Paint.Style.STROKE);
            followingPaint.setStrokeWidth(8);

            mPath.moveTo(50, 150);
            mPath.quadTo(100,200,150, 150);
            mPath.quadTo(200,100,300, 150);
            pm=new PathMeasure(mPath,false);
            pmLen = pm.getLength();

        }

        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);

            mEffect = new DashPathEffect(new float[] {pmLen, pmLen}, mPhase+pmLen);
                mPhase -= 4;
            invalidate();     
            followingPaint.setPathEffect(mEffect);
            mPaint.setColor(Color.BLACK);
            followingPaint.setColor(Color.WHITE);
            canvas.drawPath(mPath, mPaint);
            canvas.drawPath(mPath, followingPaint);
        }
    }
}