1. 程式人生 > >自定義圓環進度條

自定義圓環進度條

一:動畫效果:


二:原始碼:

(1)res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" 
format="dimension"></attr> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name=
"style"> <enum name="TIME_TICKER" value="0"></enum> <enum name="PROGRESS" value="1"></enum> </attr> </declare-styleable> </resources>
(2)myview.java:
public class MyView extends View {
    private final static String TAG = "RoundProgressBar01"
; private Paint paint; private int roundColor; private int roundProgressColor; private int textColor; private float textSize; private float roundWidth; private int max; private int progress; private boolean textIsDisplayable; private int style; public static final int STROKE = 0; public static final int FILL = 1; public MyView(Context context) { super(context); init(); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.MyView); roundColor = mTypedArray.getColor(R.styleable.MyView_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.MyView_roundProgressColor, Color.GREEN); textColor = mTypedArray.getColor(R.styleable.MyView_textColor, Color.GREEN); textSize = mTypedArray.getDimension(R.styleable.MyView_textSize, 15); roundWidth = mTypedArray.getDimension(R.styleable.MyView_roundWidth, 20); max = mTypedArray.getInteger(R.styleable.MyView_max, 100); textIsDisplayable = mTypedArray.getBoolean(R.styleable.MyView_textIsDisplayable, true); style = mTypedArray.getInt(R.styleable.MyView_style, 0); mTypedArray.recycle(); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public void init(){ paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int centre = getWidth()/2; //獲取圓心的x座標 int radius = (int) (centre - roundWidth/2); //圓環的半徑 paint.setColor(roundColor); //設定圓環的顏色 paint.setStyle(Paint.Style.STROKE); //設定空心 paint.setStrokeWidth(roundWidth); //設定圓環的寬度 paint.setAntiAlias(true); //消除鋸齒 canvas.drawCircle(centre, centre, radius, paint); //畫出圓環 /** * 畫進度百分比 */ paint.setStrokeWidth(0); paint.setColor(textColor); paint.setTextSize(textSize); paint.setTypeface(Typeface.DEFAULT_BOLD); //設定字型 //中間的進度百分比,先轉換成float在進行除法運算,不然都為0 int percent = (int)(((float)progress / (float)max) * 100); //測量字型寬度,我們需要根據字型的寬度設定在圓環中間 float textWidth = paint.measureText(percent + "%"); Log.i(TAG, "onDraw:"+"textIsDisplayable:"+textIsDisplayable+"--percent:"+percent+"--style == STROKE:"+(style == STROKE)); //if(textIsDisplayable && percent != 0 && style == STROKE){ if(textIsDisplayable && style == STROKE){ //畫出進度百分比 canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); } /** * 畫圓弧 ,畫圓環的進度 */ //設定進度是實心還是空心 paint.setStrokeWidth(roundWidth); //設定圓環的寬度 paint.setColor(roundProgressColor); //設定進度的顏色 //用於定義的圓弧的形狀和大小的界限 RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius); switch (style) { case STROKE:{ paint.setStyle(Paint.Style.STROKE); //根據進度畫圓弧 canvas.drawArc(oval, 0, 360 * progress / max, false, paint); break; } case FILL:{ paint.setStyle(Paint.Style.FILL_AND_STROKE); if(progress !=0) //根據進度畫圓弧 canvas.drawArc(oval, 0, 360 * progress / max, true, paint); break; } } } public synchronized int getMax() { return max; } public synchronized void setMax(int max) { if(max < 0){ throw new IllegalArgumentException("max not less than 0"); } this.max = max; } public synchronized int getProgress() { return progress; } public synchronized void setProgress(int progress) { if(progress < 0){ throw new IllegalArgumentException("progress not less than 0"); } if(progress > max){ progress = max; } if(progress <= max){ this.progress = progress; postInvalidate(); } } public int getCricleColor() { return roundColor; } public void setCricleColor(int cricleColor) { this.roundColor = cricleColor; } public int getCricleProgressColor() { return roundProgressColor; } public void setCricleProgressColor(int cricleProgressColor) { this.roundProgressColor = cricleProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; } public synchronized void setStyle(int style){ this.style = style; } //改變顏色的方法 public void setMyColor(int color){ roundColor = color; invalidate(); } }
(3):MainActivity.java:
private MyView roundProgressBar_01 = null;
    private int TIME_TICKER = 0;
    private int progress = 0;
    private final int UPDATE_UI_01 = 1;
    private boolean flag;

    private Handler handler = new Handler(){
        @Override
public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
super.handleMessage(msg);

            switch(msg.what){
                case UPDATE_UI_01:
                    progress++;
                    if(progress > 100){
                        progress = 0;
                    }
                    handler.sendEmptyMessageDelayed(UPDATE_UI_01, 300);
                    updateUIRoundProgressBar_01(progress);
                    break;
            }
        }
    };
    private Button start,chongzhi,change;
    private MyView myView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控制元件
start = (Button) findViewById(R.id.start);
        chongzhi = (Button) findViewById(R.id.chongzhi);
        change = (Button) findViewById(R.id.change);
        myView = (MyView) findViewById(R.id.roundProgressBar_01);
        init();
        //開始的點選事件
start.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                handler.sendEmptyMessageDelayed(UPDATE_UI_01, 300);
            }
        });
        //重置的點選事件
chongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                progress = 0;
            }
        });
        //改變顏色的點選事件
change.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                if(flag){
                    myView.setMyColor(Color.RED);
                    flag = false;
                }else {
                    myView.setMyColor(Color.BLUE);
                    flag = true;
                }
            }
        });
    }

    private void init() {
        // TODO Auto-generated method stub
roundProgressBar_01 = (MyView) findViewById(R.id.roundProgressBar_01);
        roundProgressBar_01.setStyle(TIME_TICKER);
        roundProgressBar_01.setProgress(1);
        progress = 1;
    }

    private void updateUIRoundProgressBar_01(int progress){
        roundProgressBar_01.setProgress(progress);
    }
}
(4)佈局檔案:res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom_progress="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.lk.MainActivity">

    <Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改變外層圓環顏色"/>
    <com.bwie.lk.MyView
android:layout_gravity="center"
android:id="@+id/roundProgressBar_01"
android:layout_width="80dip"
android:layout_height="80dip"
custom_progress:roundColor="#3300d1d1"
custom_progress:roundProgressColor="@android:color/black"
custom_progress:textColor="#9A32CD"
custom_progress:textIsDisplayable="true"
custom_progress:roundWidth="10dip"
custom_progress:textSize="18sp"
/>

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="100dp">
        <Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="開始"/>
        <Button
android:id="@+id/chongzhi"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="重置"/>
    </LinearLayout>

</LinearLayout>

相關推薦

安卓定義進度的顯示

效果如上圖所示關鍵點:1.需要解決的問題是 自定義view的 適配問題 在不同的 手機上 顯示出來的效果一致2.所需要的 工具類  一個轉換工具 px與dp之間的互轉package com.shenlei.servicemoneynew.util; import androi

定義進度

一:動畫效果: 二:原始碼: (1)res/values/attrs.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable

定義控制元件Android進度

Android自定義控制元件學習之圓環進度條,學習了一段時間,自定義控制元件,對一些自定義所需要的Api,基本上也掌握了,但是對於比較複雜的自定義控制元件,他們的座標計算 一直比較噁心(可能,我數學比較差~),記錄自定義控制元件學習過程。    對於圓環進度條的自定義

Android繪圖:定義View之——矩形進度進度、填充型進度、時鐘

主函式 這幾種進度條的主函式都是類似的,所以下面我只給出了一個填充型進度條的主函式,其他幾個主函式只是在這基礎上改動一下按鈕id(即與各自佈局裡面的id相同即可),還有改動一下相對應的類即可。 public class MainActivity

Android定義控制元件之百分比進度

首先我們先來看一下效果 分析 我們來看這個進度條應該分為3個小部分 1.中間的圓 2.外邊的圓環 3.中間的文字 分開畫 這3部分就是需要我們自己畫出來的,因此我們需要3根畫筆 //設定中心園的畫筆

使用 Android Studio定義View03——進度

整理總結自鴻洋的部落格:http://blog.csdn.net/lmj623565791/article/details/24500107 要實現的效果如圖: 分析一下,需要這麼幾個屬性:兩個顏色、一個速度、一個圓環寬度 com.cctvjiatao.customvie

Android定義帶動畫進度

1.首先是自定義類 package com.yx.yxcustomprogress; import android.content.Context; import android.content.res.TypedArray; import android.graphi

定義View載入進度首頁面

1.主頁面佈局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:

Android定義View-圓形進度

好幾天不寫部落格了,這段時間一直沒時間,感覺一直在忙,但是進度不大。 好了,言歸正傳,最近專案裡要用到這麼一個自定義view,是一個圓形的進度圓環,現在學習下怎麼來自定義它。 原始碼下載地址 自定義之前先分析一下,這個自定義View主要有以下幾

android定義的弧形進度和圓形進度-SemicircleProgress

SemicircleProgress 兩個自定義圓形和弧形進度條 第一個SemicircleProgress可以對中間和下面的字自定義 第二個CircularProgressar可

android定義ProgressBar 修改進度樣式 出現setProgress無效問題

在很多時候android系統提供的進度條樣式是不足以滿足我們的需求的,因此在大多數的時候,我們為了美觀,亦或者為了實現自己的功能,需要在系統的基礎上進行修改其樣式,當然你也可以自己從頭寫一個,不過個人覺得還是比較麻煩,好吧,廢話不多說了,進入正題。 昨天在做一個下載功能時,

定義Qt圓形進度

主要程式碼: #include "diaplot.h" #include "qpainter.h" #include "qdebug.h" DialPlot::DialPlot(QWidget *parent) : QWidget(parent) {

iOS開發之定義式Slider

#pragma mark - UIControl functions //開始跟蹤觸控 -(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super beginTrackingWithTouch:

定義View圓形進度 跳轉頁面

效果展示 1.匯入依賴 implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 2.建立自定義Vie

定義弧形漸變進度功能

/** * 畫筆物件的引用 */ private Paint paint; private Paint smallcicrlPaint; /** 分段顏色 */ private static final int[] SECTION_COLORS = {

Android定義View--圓形進度RoundProgress

要實現的效果 需要知道的知識點 字型的高度和寬度是怎麼測? 字型的高度就是textSize的大小。 字型的寬度怎麼測量呢?Paint畫筆中有測量字型寬度的api,如下: //測量字型的寬度 float width =

Android 定義 View 圓形進度總結

Android 自定義圓形進度條總結 最近擼了一個圓形進度條的開源專案,算是第一次完完整整的使用自定義 View 。在此對專案開發思路做個小結,歡迎大家 Star 和 Fork 該專案總共實現了三種圓形進度條效果 CircleProgress:圓形進度條,可以實現仿 QQ

【百度echarts】實現進度-程式碼示例

//柱狀圖 var asd =document.getElementById('pie'); var pieChart = echarts.init(asd); var labelTop = {//上層樣式 normal

Android定義View——投票進度

效果展示 功能屬性介紹 <!-- MatchSupportProgressBar --> <declare-styleable

定義漂亮的進度

最近在鴻洋的部落格上看到一個自定義的ProgressBar,在github上也看到了這種型別的Progress,地址為:https://github.com/daimajia/NumberProgressBar ,我第一反應就是,我自己能不能也寫一個呢,先上效果圖 Col