public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private class SampleView extends View {
private Paint mPaint;
private Path mPath;
private PathEffect[] mEffects;
private int[] mclolors;
private float mPhase;
private void makeEffects(PathEffect[] e, float phase) {
e[0] = null;
e[1] = new CornerPathEffect(10);
float[] inverse=new float[] { 10, 5, 5, 5 };
e[2] = new DashPathEffect(inverse, phase);
e[3] = new PathDashPathEffect(makePathDash(), 12, phase,
PathDashPathEffect.Style.ROTATE);
e[4]=new ComposePathEffect(e[2], e[1]);
e[5]=new ComposePathEffect(e[3], e[1]);
}
private Path makePathDash() {
Path p = new Path();
p.moveTo(4, 0);
p.lineTo(0, -4);
p.lineTo(8, -4);
p.lineTo(12, 0);
p.lineTo(8, 4);
p.lineTo(0, 4);
return p;
}
public SampleView(Context context) {
super(context);
setFocusable(true);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mPath = makePath();
mEffects = new PathEffect[6];
mclolors = new int[] { Color.BLACK, Color.RED, Color.BLUE,
Color.GREEN, Color.MAGENTA, Color.BLACK };
}
private Path makePath() {
Path path = new Path();
path.moveTo(0, 0);
for (int i = 1; i <= 15; i++) {
path.lineTo(i * 20, (float) Math.random() * 35);
}
return path;
}
@Override
protected void onDraw(Canvas canvas) {
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
canvas.translate(10, 10);
makeEffects(mEffects, mPhase);
mPhase++;
for (int i = 0; i < mEffects.length; i++) {
mPaint.setPathEffect(mEffects[i]);
mPaint.setColor(mclolors[i]);
canvas.drawPath(mPath, mPaint);
canvas.translate(0, 28);
}
invalidate();
}
}
}