自定义view

来源:互联网 发布:ubuntu gnome16.04美化 编辑:程序博客网 时间:2024/06/16 14:31
public class Myview extends View {    Paint paint;    RectF rectF;    float width;    float height;    float radius;    float small;    float larg;    float txtwidth;    int txtcolor;    public Myview(Context context) {        super(context);        init();    }    private void init() {        paint = new Paint(Paint.ANTI_ALIAS_FLAG);//启用抗锯齿        paint.setStyle(Paint.Style.STROKE);//空心        paint.setStrokeWidth(txtwidth);//设置线宽        paint.setColor(txtcolor);//颜色    }    public Myview(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Myview, 0, 0);//获取设置的style        try {            txtwidth = typedArray.getDimension(R.styleable.Myview_border_width, 2);//默认高            txtcolor = typedArray.getColor(R.styleable.Myview_border_color, 0xff000000);//默认颜色        } finally {            typedArray.recycle();//回收typedArray;        }        init();    }    //改变view的大小    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        rectF = new RectF(getLeft(), getTop(), getRight(), getBottom());//获得上下左右的位置        width = rectF.right - rectF.left;//计算宽        height = rectF.bottom - rectF.top;//计算高        /*         判断要是高比宽大的时候 便把宽除以4 赋值给radius 反之         */        if (width < height) {            radius = width / 4;        } else {            radius = height / 4;        }        small = 10;//单纯赋值        larg = 20;//单纯赋值    }    public Myview(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    //画布    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawColor(0xff000000);//设置画布的颜色        paint.setColor(0x66555555);//设置画笔颜色        //该方法用于在画布上绘制圆角矩形,通过指定RectF对象以及圆角半径来实现。该方法是绘制圆角矩形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆角矩形。        /*        参数说明 rect:RectF对象。 rx:x方向上的圆角半径。 ry:y方向上的圆角半径。 paint:绘制时所使用的画笔。         */        canvas.drawRoundRect(new RectF(rectF.centerX() - (float) 0.9 * width / 2, rectF.centerY() - (float) 0.9 * width / 2, rectF.centerX() + (float) 0.9 * width / 2, rectF.centerY() + (float) 0.9 * height / 2), 30, 30, paint);        paint.setColor(txtcolor);        canvas.drawCircle(rectF.centerX(), rectF.centerY(), radius, paint);//绘制圆形        float startx, starty;        float stopx, stopy;        for (int i = 0; i < 60; i++) {            startx = radius * (float) Math.cos(Math.PI / 180 * i * 6);//正旋值            starty = radius * (float) Math.sin(Math.PI / 180 * 6 * i);//余旋值   math 一个专门提供计算的类            if (i % 5 == 0) {                stopx = startx + larg * (float) Math.cos(Math.PI / 180 * i * 6);                stopy = starty + larg * (float) Math.sin(Math.PI / 180 * i * 6);            } else {                stopx = startx + small * (float) Math.cos(Math.PI / 180 * i * 6);                stopy = starty + small * (float) Math.sin(Math.PI / 180 * i * 6);            }            startx += rectF.centerX();            starty += rectF.centerY();            stopx = rectF.centerX();            stopy = rectF.centerY();            canvas.drawLine(startx, starty, stopx, stopy, paint);//绘制直线        }        canvas.drawCircle(rectF.centerX(), rectF.centerY(), 20, paint);        canvas.rotate(60, rectF.centerX(), rectF.centerY());//旋转        canvas.drawLine(rectF.centerX(), rectF.centerY(), rectF.centerX(), rectF.centerY() - radius, paint);    }}
0 0