比较样式1111

public class CustomView extends View{
    private String TAG = "CustomView";
    private int mViewHeight;
    private int mViewWidth;
    private Paint mPaint;
    public CustomView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        mViewHeight = h;
        mViewWidth=w;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.translate(mViewWidth/2, mViewHeight/2);

          /*----------------- 1 获取路径路径长度 ------------------------*/
//        Path path = new Path();
//        path.lineTo(0, 200);
//        path.lineTo(200, 200);
//        path.lineTo(200, 0);
//       
//        /**
//         * PathMeasure第二个参数:是否强制关闭,为true时getLength比为false的值小
//         */
//        PathMeasure measure = new PathMeasure(path, false);//600
//        PathMeasure measure2 = new PathMeasure(path, true);//800
//       
//        canvas.drawPath(path, mPaint);

         /*-----------------2 获取下个路径------------------------*/

//        Path path = new Path();
//        //多路径效果需要关闭硬件加速
//        path.addRect(-200, -200, 200, 200, Path.Direction.CW);
//        path.addRect(-300, -300, 300, 300, Path.Direction.CW);
//        PathMeasure measure = new PathMeasure(path, false);
//       
//        //获取下一个路径,有可能获取不到
//        int len1 = (int) measure.getLength();
//        boolean nextPath = measure.nextContour();
//        int len2 = 0;
//        if (nextPath) {
//            len2 = (int) measure.getLength();
//        }
//        Log.i("CustomView", "len1: "+len1+" len2:"+len2);
//       
//        canvas.drawPath(path, mPaint);


        /*-----------------3 截取路径------------------------*/
//        Path path = new Path();
//        //多路径效果需要关闭硬件加速
//        path.addRect(-200, -200, 200, 200, Path.Direction.CW);
//        PathMeasure measure = new PathMeasure(path, false);
//        //获取下一个路径,有可能获取不到
//        int len1 = (int) measure.getLength();
//        Log.i("CustomView", "len1: "+len1);
//        canvas.drawPath(path, mPaint);
//       
//       
//        Path dstPath = new Path();
//        //将截取的路径放到dstPath路径中
//        //startWithMoveTo 是否从上一个起始点开始绘制(是否保持连续性)
//        measure.getSegment(0, 800, dstPath, true);
//        mPaint.setColor(Color.GREEN);
//        canvas.drawPath(dstPath, mPaint);



        /*-----------------4 截取路径------------------------*/
        Path path = new Path();
        //多路径效果需要关闭硬件加速
        path.addCircle(0, 0, 300, Path.Direction.CW);
        PathMeasure measure = new PathMeasure(path, false);
        float[] pos = new float[2];
        float[] tan = new float[2];//tan = y/x
        measure.getPosTan(measure.getLength()/4, pos, tan);

        Log.i(TAG, "pos:"+pos);
        Log.i(TAG, "tan:"+tan);


        canvas.drawPath(path, mPaint);


    }

}

 
 

你可能感兴趣的:(Android,基础)