Android——自訂圓環View

來源:互聯網
上載者:User

標籤:

一般使用自訂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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.