public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View{
float phase;
PathEffect[] effects=new PathEffect[7];
int[] colors;
private Paint paint;
Path path;
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint=new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
path=new Path();
path.moveTo(0, 0);
for(int i=1;i<=15;i++){
path.lineTo(i*20, (float)Math.random()*60);
}
colors=new int[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.GREEN,Color.MAGENTA,Color.RED,Color.YELLOW};
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
effects[0] =null;
effects[1] =new CornerPathEffect(10);
effects[2] =new DiscretePathEffect(3.0f, 5.0f);
effects[3] =new DashPathEffect(new float[]{20,10,5,10}, phase);
Path p=new Path();
p.addRect(0, 0, 8, 8, Path.Direction.CCW);
effects[4]=new PathDashPathEffect(p, 12, phase,PathDashPathEffect.Style.ROTATE );
effects[5]=new ComposePathEffect(effects[2], effects[4]);
effects[6]=new SumPathEffect( effects[4], effects[3]);
canvas.translate(8, 8);
for(int i=0;i<effects.length;i++){
paint.setPathEffect(effects[i]);
paint.setColor(colors[i]);
canvas.drawPath(path, paint);
canvas.translate(0, 60);
}
phase+=1;
invalidate();
}
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this));
}
class TextView extends View{
final String DRAW_STR="疯狂的Java讲义";
Path[] paths=new Path[3];
Paint paint;
public TextView(Context context) {
this(context,null);
}
public TextView(Context context, AttributeSet attrs) {
super(context, attrs);
paths[0]=new Path();
paths[0].moveTo(0, 0);
for(int i=1;i<=7;i++){
//生成7个点,随机生成他们的Y坐标,并将他们连成一条path
paths[0].lineTo(i*30, (float)Math.random()*30);
}
paths[1]=new Path();
RectF rectf=new RectF(0,0,200,120);
paths[1].addOval(rectf, Path.Direction.CCW);
paths[2]=new Path();
paths[2].addArc(rectf, 60, 180);
//初始化画笔
paint=new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(1);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.translate(40, 40);
//设置从右边开始绘制(右对齐)
paint.setTextAlign(Paint.Align.RIGHT);
paint.setTextSize(20);
//绘制路径
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(paths[0], paint);
//沿着路径绘制一段文本
paint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(DRAW_STR, paths[0], -8, 20, paint);
//对canvas进行坐标变换,画布下移60
canvas.translate(0, 60);
//绘制路径
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(paths[1], paint);
//沿着路径绘制一段文件
paint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(DRAW_STR, paths[1], -20, 20, paint);
//对Canvas进行坐标变换:画布下移120
canvas.translate(0, 120);
//绘制路径
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(paths[2], paint);
//沿着路径绘制一段文本
paint.setStyle(Paint.Style.FILL);
canvas.drawTextOnPath(DRAW_STR, paths[2], -10, 20, paint);
}
}
}