我有一个路径和一个遵循已实现路径的对象。如何使移动对象在它经过的路径上绘制另一种颜色。
这里是图像的链接(也许它更好地解释了我的意思):移动物体在路径上
谢谢你的帮助:)
这是一个如何完成的示例:)
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);
}
}
}