自定义 view<简单画图 圆形>

来源:互联网 发布:jquery数组转换成json 编辑:程序博客网 时间:2024/06/06 02:39
public class MyView extends View {   public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }                                            //属性设置              //     public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        this(context, attrs, defStyleAttr,0);    }        public MyView(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }        public MyView(Context context) {        this(context,null);    }    /*    onMeasure: 测量    int widthMeasureSpec  :宽     int heightMeasureSpec  :高     */    @Override                   //  宽                  //高    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int width = MeasureSpec.getMode(widthMeasureSpec);        int size = MeasureSpec.getSize(widthMeasureSpec);     /*   int atMost = MeasureSpec.AT_MOST;  //        int exactly = MeasureSpec.EXACTLY;  //确定,模式 是给定  控件 具体的宽高        int unspecified = MeasureSpec.UNSPECIFIED;  //        */    }    /*        布局:     boolean changed,  布局 改变     int left,  :左     int top,   ;上     int right,  ;右     int bottom ;下     */    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint1 = new Paint();        Paint paint2 = new Paint();        Paint paint3 = new Paint();        Paint paint4= new Paint();        Paint paint5= new Paint();        Paint paint6= new Paint();        paint1.setColor(Color.BLACK);//黑色        paint2.setColor(Color.YELLOW);//        paint3.setColor(Color.BLUE);//        paint4.setColor(Color.CYAN);//        paint5.setColor(Color.RED);//        paint6.setColor(Color.GRAY);//        /*            cx:float            cy:float            radius: float            paint:Paint         */        paint1.setStyle(Paint.Style.STROKE);     // 园       canvas.drawCircle(300f,300f,200,paint1);       //线        canvas.drawLine(800,200,1000,1000,paint1);       canvas.drawArc(200f,200f,750f,750f,0f,60f,true,paint1);       canvas.drawArc(200f,200f,750f,750f,60f,60f,true,paint2);       canvas.drawArc(200f,200f,750f,750f,120f,60f,true,paint3);       canvas.drawArc(200f,200f,750f,750f,180f,60f,true,paint4);       canvas.drawArc(200f,200f,750f,750f,240f,60f,true,paint5);       canvas.drawArc(200f,200f,750f,750f,300f,60f,true,paint6);    }}