Android學習之自定義圓環(huán) 顏色交替

Android學習之自定義圓環(huán) 顏色交替
原文出處:http://blog.csdn.net/lmj623565791/article/details/24500107

1萝快、自定義View的屬性
``
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="integer"/>
<attr name="secondColor" format="integer"/>
<attr name="widthCircle" format="dimension"/>
<attr name="speed" format="integer"/>
<declare-styleable name="customCircleView">
<attr name="firstColor"></attr>
<attr name="secondColor" />
<attr name="widthCircle"></attr>
<attr name="speed" />
</declare-styleable>
</resources>

2儿普、在View的構造方法中獲得我們自定義的屬性
public CircleProgressBar(Context context) {
    this(context,null);
}


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

public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //獲取自定義屬性的值
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
            R.styleable.customCircleView,defStyleAttr,0);
    int typeSize = typedArray.getIndexCount();
    for(int i = 0; i<typeSize;i++){
        int type = typedArray.getIndex(typeSize);
        switch (type){
            case R.styleable.customCircleView_firstColor:
                firstColor = typedArray.getColor(type, Color.BLUE);
                break;
            case R.styleable.customCircleView_secondColor:
                secondColor = typedArray.getColor(type,Color.GREEN);
                break;
            case R.styleable.customCircleView_widthCircle:
                widthCircle = (int) typedArray.getDimension(type, TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));

                break;
            case R.styleable.customCircleView_speed:
                speed = typedArray.getInt(type,20);
                break;
        }
    }

[ 3惰赋、重寫onMesure ]
4了赵、重寫onDraw

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//獲取圓心的x坐標
int center = getWidth()/2;
//獲取半徑锥忿,
int radius = center-widthCircle;
//開始畫圓環(huán)
paint.setStrokeWidth(widthCircle);//設置圓環(huán)的外圍寬度
paint.setAntiAlias(true);//消除鋸齒
paint.setStyle(Paint.Style.STROKE);//設置空心
RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圓環(huán)left top恋昼,right徘郭,bottom位置
if(!isNext){
//第一種顏色旋轉(zhuǎn)
paint.setColor(firstColor);//設置圓環(huán)顏色
canvas.drawCircle(center,center,radius,paint);//畫出圓環(huán)
paint.setColor(secondColor);
//根據(jù)進度畫圓弧
canvas.drawArc(rectF,-90,currProgress,false,paint);

}else {

    paint.setColor(secondColor);
    canvas.drawCircle(center,center,radius,paint);
    paint.setColor(firstColor);
    canvas.drawArc(rectF,-90,currProgress,false,paint);
}

}

/**

  • 自定義圓環(huán)
  • Created by JamesZhang on 2017/4/30.
    */

public class CircleProgressBar extends View {
private int firstColor;//第一種顏色
private int secondColor;//第二種顏色
private int widthCircle;//圓環(huán)寬度
private int speed;//速度
private Paint paint;//畫筆
private int currProgress;//當前進度
private boolean isNext;//是否下一輪
public CircleProgressBar(Context context) {
this(context,null);
}

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

public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //獲取自定義屬性的值
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
            R.styleable.customCircleView,defStyleAttr,0);
    int typeSize = typedArray.getIndexCount();
    for(int i = 0; i<typeSize;i++){
        int type = typedArray.getIndex(typeSize);
        switch (type){
            case R.styleable.customCircleView_firstColor:
                firstColor = typedArray.getColor(type, Color.BLUE);
                break;
            case R.styleable.customCircleView_secondColor:
                secondColor = typedArray.getColor(type,Color.GREEN);
                break;
            case R.styleable.customCircleView_widthCircle:
                widthCircle = (int) typedArray.getDimension(type, TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));

                break;
            case R.styleable.customCircleView_speed:
                speed = typedArray.getInt(type,20);
                break;
        }
    }
    typedArray.recycle();
    paint = new Paint();
     //繪圖線程
    new Thread(new Runnable() {
        @Override
        public void run() {
            //
            while (true){
                //進度增加
                currProgress++;
                //如果當前進度到360吐辙,設置進度為0调缨,更換顏色
                if(currProgress==360){
                    currProgress = 0;
                    if(isNext){
                       isNext = false;
                    }else {
                        isNext = true;
                    }
                    postInvalidate();
                    try {
                        Thread.sleep(speed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }).start();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //獲取圓心的x坐標
    int center = getWidth()/2;
    //獲取半徑疮鲫,
    int radius = center-widthCircle;
    //開始畫圓環(huán)
    paint.setStrokeWidth(widthCircle);//設置圓環(huán)的外圍寬度
    paint.setAntiAlias(true);//消除鋸齒
    paint.setStyle(Paint.Style.STROKE);//設置空心
    RectF rectF = new RectF(center-radius,center-radius,center+radius,center+radius);//控制圓環(huán)left top,right弦叶,bottom位置
    if(!isNext){
        //第一種顏色旋轉(zhuǎn)
        paint.setColor(firstColor);//設置圓環(huán)顏色
        canvas.drawCircle(center,center,radius,paint);//畫出圓環(huán)
        paint.setColor(secondColor);
        //根據(jù)進度畫圓弧
        canvas.drawArc(rectF,-90,currProgress,false,paint);

    }else {

        paint.setColor(secondColor);
        canvas.drawCircle(center,center,radius,paint);
        paint.setColor(firstColor);
        canvas.drawArc(rectF,-90,currProgress,false,paint);
    }

}

public int getFirstColor() {
    return firstColor;
}

public void setFirstColor(int firstColor) {
    this.firstColor = firstColor;
}

public int getSecondColor() {
    return secondColor;
}

public void setSecondColor(int secondColor) {
    this.secondColor = secondColor;
}

public int getWidthCircle() {
    return widthCircle;
}

public void setWidthCircle(int widthCircle) {
    this.widthCircle = widthCircle;
}

public int getSpeed() {
    return speed;
}

public void setSpeed(int speed) {
    this.speed = speed;
}

public Paint getPaint() {
    return paint;
}

public void setPaint(Paint paint) {
    this.paint = paint;
}

public int getCurrProgress() {
    return currProgress;
}

public void setCurrProgress(int currProgress) {
    this.currProgress = currProgress;
}

public boolean isNext() {
    return isNext;
}

public void setNext(boolean next) {
    isNext = next;
}

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末俊犯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子伤哺,更是在濱河造成了極大的恐慌燕侠,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件立莉,死亡現(xiàn)場離奇詭異绢彤,居然都是意外死亡,警方通過查閱死者的電腦和手機蜓耻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門茫舶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人刹淌,你說我怎么就攤上這事饶氏〖ズ模” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵疹启,是天一觀的道長葛账。 經(jīng)常有香客問我,道長皮仁,這世上最難降的妖魔是什么籍琳? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮贷祈,結果婚禮上趋急,老公的妹妹穿的比我還像新娘。我一直安慰自己势誊,他們只是感情好呜达,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著粟耻,像睡著了一般查近。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上挤忙,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天霜威,我揣著相機與錄音,去河邊找鬼册烈。 笑死戈泼,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的赏僧。 我是一名探鬼主播大猛,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼淀零!你這毒婦竟也來了挽绩?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤驾中,失蹤者是張志新(化名)和其女友劉穎唉堪,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哀卫,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡巨坊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了此改。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片趾撵。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出占调,到底是詐尸還是另有隱情暂题,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布究珊,位于F島的核電站薪者,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏剿涮。R本人自食惡果不足惜言津,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望取试。 院中可真熱鬧悬槽,春花似錦、人聲如沸瞬浓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猿棉。三九已至磅叛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間萨赁,已是汗流浹背弊琴。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留位迂,地道東北人访雪。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓详瑞,卻偏偏與公主長得像掂林,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子坝橡,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

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