首页 > Java > java教程 > 正文

Android自定义圆环进度条

大家讲道理
发布: 2016-11-10 10:44:10
原创
1499 人浏览过

package com.example.carlos.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
 * Created by carlos on 2015/10/24.
 */
public class CustomView extends View {
    private Paint mPaintBackCircle;
    private Paint mPaintFrontCircle;
    private Paint mPaintText;
    private float mStrokeWith =50;
    private float mHalfStrokeWith = mStrokeWith/2;
    private float mX = 200 + mHalfStrokeWith;
    private float mY = 200 + mHalfStrokeWith;
    private float mRadius = 200;
    private RectF mRectF;
    private int mProgess;
    public CustomView(Context context) {
        super(context);
        init();
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init(){
        mPaintBackCircle = new Paint();
        mPaintBackCircle.setColor(Color.BLUE);
        mPaintBackCircle.setAntiAlias(true);
        mPaintBackCircle.setStyle(Paint.Style.STROKE);
        mPaintBackCircle.setStrokeWidth(mStrokeWith);
        mPaintFrontCircle = new Paint();
        mPaintFrontCircle.setColor(Color.YELLOW);
        mPaintFrontCircle.setAntiAlias(true);
        mPaintFrontCircle.setStyle(Paint.Style.STROKE);
        mPaintFrontCircle.setStrokeWidth(mStrokeWith);
        mPaintText = new Paint();
        mPaintText.setColor(Color.RED);
        mPaintText.setAntiAlias(true);
        mPaintText.setTextSize(50);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mRectF = new RectF(mHalfStrokeWith,mHalfStrokeWith,mRadius*2 + mHalfStrokeWith,mRadius*2 + mHalfStrokeWith);
        mProgess =75;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mX, mY, mRadius, mPaintBackCircle);
        canvas.drawArc(mRectF,0,270,false,mPaintFrontCircle);
        canvas.drawText(mProgess+"%",mX,mY,mPaintText);
    }
}
登录后复制

14495610031.png

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板