比較スタイル1111

3305 ワード

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);


    }

}