android_path的子类

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
            path=new Path();

            path.moveTo(0, 0);
            for(int i=1;i<=15;i++){
                //生成的15个点,随机生成他们的Y坐标,并将他们连成一条path
                path.lineTo(i*20, (float)Math.random()*60);

            }

            //初始化7个颜色
            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);

            //-----------下面开始初始化7种路径效果

            //不使用路径效果
            effects[0] =null;

            //使用CornerPathEffect路径效果
            effects[1] =new CornerPathEffect(10);

            //初始化DiscretePathEffect
            effects[2] =new DiscretePathEffect(3.0f, 5.0f);

            //初始化DashPathEffect
            effects[3] =new DashPathEffect(new float[]{20,10,5,10}, phase);

            //初始化PathDashPathEffect
            Path p=new Path();
            p.addRect(0, 0, 8, 8, Path.Direction.CCW);
            effects[4]=new PathDashPathEffect(p, 12, phase,PathDashPathEffect.Style.ROTATE );


            //初始化pathDashPathEffect
            effects[5]=new ComposePathEffect(effects[2], effects[4]);
            effects[6]=new SumPathEffect( effects[4],  effects[3]);

            //将画布移动到(8,8)处开始绘制
            canvas.translate(8, 8);

            //依次使用7种不同路径效果,7种不同的颜色来绘制路径
            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);

        }

    }

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值