码迷,mamicode.com
首页 > 移动开发 > 详细

Android——自定义圆环View

时间:2016-05-12 13:41:28      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

一般使用自定义View需要重写onDraw()函数,如果需要使用wrap_content样式,则还需要重写onMeasure函数。

通常我们绘制一个圆环是通过绘制两个半径不同圆心相同的颜色不一样的圆

下面介绍使用canvas.drwARc()函数绘制圆环(或者圆弧)

canvas.drawArc(rectF,startDegree,60,false,mPaint);

该函数有四个参数
参数1:一个RectF类型对象,表示该绘制椭圆的外切矩形的坐标。(RectF和Rect的区别在于里面记录的坐标为float参数而Rect记录int参数)
参数2:startDegree:int类型,表示开始绘制的角度
参数3:60:int类型,要绘制多少角度
参数4:false:boolean类型,表示是否绘制圆心到圆弧的连线
参数5:mPaint:Paint类型,绘图的画笔

要绘制一个圆环需要设置绘制的角度为360度,以及参数4为false(不需要绘制到圆心的连线),此外:
mPaint的style需要设置为

//Paint.Style.FILL    :填充内部
//Paint.Style.FILL_AND_STROKE  :填充内部和描边
//Paint.Style.STROKE  :仅描边
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(100);//描边宽度

一个完整的例子:

public class CircleView extends View {
    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint=new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(100);
        mPaint1=new Paint();
        mPaint1.setStyle(Paint.Style.STROKE);
        mPaint1.setStrokeWidth(100);

        textPaint=new Paint();
        textPaint.setTextSize(20);
        textPaint.setColor(Color.BLACK);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size=MeasureSpec.getSize(widthMeasureSpec);
        int mode=MeasureSpec.getMode(widthMeasureSpec);
        int result=0;
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=200;
            if(mode==MeasureSpec.AT_MOST)
                result=Math.min(result,size);
        }
        setMeasuredDimension(result,result);
        LinearGradient gradient=new LinearGradient(0,0,getMeasuredHeight(),getMeasuredWidth(),
                new int[]{Color.RED,Color.YELLOW,Color.BLUE},null, Shader.TileMode.CLAMP);
        SweepGradient sweep=new SweepGradient(0,0,new int[]{Color.YELLOW,Color.GREEN,Color.BLUE,Color.RED},null);
        SweepGradient sweep1=new SweepGradient(0,0,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.RED},null);
        mPaint.setShader(sweep);
        mPaint1.setShader(sweep1);
    }

    private Paint textPaint;
    private Paint mPaint,mPaint1;
    private int startDegree=270;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        startDegree+=10;
        startDegree=startDegree%360;
        float width=getMeasuredWidth();
        float radias=width/2;
        RectF rectF=new RectF((float)(width*0.1),
                (float)(width*0.1),
                (float)(width*0.9),
                (float)(width*0.9));
        canvas.drawArc(rectF,startDegree,60,false,mPaint);
        canvas.drawArc(rectF,startDegree+180,60,false,mPaint);
        canvas.drawArc(rectF,startDegree+270,60,false,mPaint1);
        canvas.drawArc(rectF,startDegree+90,60,false,mPaint1);

        String str="hello";
        float textSize=textPaint.getTextSize();
        canvas.drawCircle(radias, radias, (float) (radias * 0.2), mPaint);
        canvas.drawText(str,radias-(textSize*str.length() / 2), radias,textPaint);
        postInvalidateDelayed(100);
    }
}

Android——自定义圆环View

标签:

原文地址:http://blog.csdn.net/a591193965/article/details/51365800

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!