Android 圓形進(jìn)度條

可設(shè)置 線性漸變-背景色-進(jìn)度條顏色-圓弧寬度
效果圖

普通效果.png

漸變效果

改變弧度效果

步驟一:新建自定義控件CirclePercentView繼承View(代碼可直接復(fù)制使用)
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Keep;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圓形進(jìn)度條(可設(shè)置 線性漸變-背景色-進(jìn)度條顏色-圓弧寬度)
 * Created by ZWQ 
 */
public class CirclePercentView extends View {

    public static final int WIDTH_RADIUS_RATIO = 8;     // 弧線半徑 : 弧線線寬 (比例)
    public static final int MAX = 100;
    private Paint mPaint;
    private float progressPercent;
    private int radius;//圓弧寬度
    private RectF rectF;
    private int bgColor, progressColor;
    private int startColor, endColor;
    private LinearGradient gradient;
    private boolean isGradient;

    public CirclePercentView(Context context) {
        super(context);
        init();
    }

    public CirclePercentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CirclePercentView);
        bgColor = typedArray.getColor(R.styleable.CirclePercentView_circleBgColor, getResources().getColor(R.color.gray_cfcfcf));
        progressColor = typedArray.getColor(R.styleable.CirclePercentView_circleProgressColor, getResources().getColor(R.color.orange_ffc032));
        radius = typedArray.getInt(R.styleable.CirclePercentView_circleRadius, WIDTH_RADIUS_RATIO);
        isGradient = typedArray.getBoolean(R.styleable.CirclePercentView_circleIsGradient, false);
        startColor = typedArray.getColor(R.styleable.CirclePercentView_circleStartColor, getResources().getColor(R.color.black_3A3D4E));
        endColor = typedArray.getColor(R.styleable.CirclePercentView_circleEndColor, getResources().getColor(R.color.black_475B80));
        typedArray.recycle();
        init();
    }

    public CirclePercentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());//自定義的View能夠使用wrap_content或者是match_parent的屬性
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        gradient = new LinearGradient(getWidth(), 0, getWidth(), getHeight(), startColor, endColor, Shader.TileMode.MIRROR);
    }

    @SuppressWarnings("Duplicates")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 1贮尉、繪制背景灰色圓環(huán)
        int centerX = getWidth() / 2;
        int strokeWidth = centerX / radius;
        mPaint.setShader(null);//必須設(shè)置為null芹彬,否則背景也會(huì)加上漸變色
        mPaint.setStrokeWidth(strokeWidth); //設(shè)置畫(huà)筆的大小
        mPaint.setColor(bgColor);
        canvas.drawCircle(centerX, centerX, centerX - strokeWidth / 2, mPaint);
        // 2座菠、繪制比例弧
        if (rectF == null) {//外切正方形
            rectF = new RectF(strokeWidth / 2, strokeWidth / 2, 2 * centerX - strokeWidth / 2, 2 * centerX - strokeWidth / 2);
        }
        //3匈子、是否繪制漸變色
        if (isGradient) {
            mPaint.setShader(gradient);//設(shè)置線性漸變
        } else {
            mPaint.setColor(progressColor);
        }
        canvas.drawArc(rectF, -90, 3.6f * progressPercent, false, mPaint);   //畫(huà)比例圓弧
    }

    private void init() {
        mPaint = new Paint();
        //畫(huà)筆樣式
        mPaint.setStyle(Paint.Style.STROKE);
        //設(shè)置筆刷的樣式:圓形
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        //設(shè)置抗鋸齒
        mPaint.setAntiAlias(true);
    }

    @Keep
    public void setPercentage(float percentage) {
        this.progressPercent = percentage;
        invalidate();
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public void setStartColor(int startColor) {
        this.startColor = startColor;
    }

    public void setEndColor(int endColor) {
        this.endColor = endColor;
    }

    public void setGradient(boolean gradient) {
        isGradient = gradient;
    }
}

步驟二:自定義屬性

在styles.xml添加自定義屬性

    <declare-styleable name="CirclePercentView">
        <!--圓弧背景色-->
        <attr name="circleBgColor" format="reference" />
        <!--圓弧進(jìn)度條顏色-->
        <attr name="circleProgressColor" format="reference" />
        <!--圓弧寬度-->
        <attr name="circleRadius" format="integer" />
        <!--漸變開(kāi)始顏色-->
        <attr name="circleStartColor" format="reference" />
        <!--漸變結(jié)束顏色-->
        <attr name="circleEndColor" format="reference" />
        <!--是否漸變-->
        <attr name="circleIsGradient" format="boolean" />
    </declare-styleable>
步驟三:使用
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.yate.jsq.widget.progress.CirclePercentView
        android:id="@id/circle_percent_progress"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        app:circleBgColor="@color/gray_color2"
        app:circleIsGradient="true"
        app:circleProgressColor="@color/black_3A3D4E" />
    
</FrameLayout>

1)動(dòng)畫(huà)效果

    public static final int ANIMATOR_DURATION = 1000;
    private CirclePercentView progressView;

   /**
     * 設(shè)置百分比
     * @param max     最大值
     * @param current 占比
     */
    private void setData(int max, float current) {
        float percentage = (100f * current) / max;
        ObjectAnimator animator = ObjectAnimator.ofFloat(progressView, "percentage", 0, percentage);
        animator.setDuration(ANIMATOR_DURATION);
        animator.start();
    }

調(diào)用setData()方法尤慰,傳入最大值和占比值即可

2)無(wú)動(dòng)畫(huà)效果

        private CirclePercentView progressView;

        progressView.setPercentage(50);//傳入百分比的值
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末关斜,一起剝皮案震驚了整個(gè)濱河市叽粹,隨后出現(xiàn)的幾起案子炭菌,更是在濱河造成了極大的恐慌罪佳,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件黑低,死亡現(xiàn)場(chǎng)離奇詭異赘艳,居然都是意外死亡酌毡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)蕾管,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)枷踏,“玉大人,你說(shuō)我怎么就攤上這事掰曾⌒袢洌” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵旷坦,是天一觀的道長(zhǎng)掏熬。 經(jīng)常有香客問(wèn)我,道長(zhǎng)秒梅,這世上最難降的妖魔是什么旗芬? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮捆蜀,結(jié)果婚禮上疮丛,老公的妹妹穿的比我還像新娘。我一直安慰自己漱办,他們只是感情好这刷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布婉烟。 她就那樣靜靜地躺著娩井,像睡著了一般。 火紅的嫁衣襯著肌膚如雪似袁。 梳的紋絲不亂的頭發(fā)上洞辣,一...
    開(kāi)封第一講書(shū)人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音昙衅,去河邊找鬼扬霜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛而涉,可吹牛的內(nèi)容都是我干的著瓶。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼啼县,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼材原!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起季眷,我...
    開(kāi)封第一講書(shū)人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤余蟹,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后子刮,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體威酒,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了葵孤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片担钮。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖尤仍,靈堂內(nèi)的尸體忽然破棺而出裳朋,到底是詐尸還是另有隱情,我是刑警寧澤吓著,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布鲤嫡,位于F島的核電站,受9級(jí)特大地震影響绑莺,放射性物質(zhì)發(fā)生泄漏暖眼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一纺裁、第九天 我趴在偏房一處隱蔽的房頂上張望诫肠。 院中可真熱鬧,春花似錦欺缘、人聲如沸栋豫。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)丧鸯。三九已至,卻和暖如春嫩絮,著一層夾襖步出監(jiān)牢的瞬間丛肢,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工剿干, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蜂怎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓置尔,卻偏偏與公主長(zhǎng)得像杠步,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子榜轿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容