按鈕圓圈進(jìn)度條

public class CircularProgressLayout extends FrameLayout {
private static final long DEFAULT_UPDATE_INTERVAL = 16L;
private static final float DEFAULT_ROTATION = 0.75F;
private CircularProgressDrawable mProgressDrawable;
private CircularProgressLayoutController mController;
private float mStartingRotation;
private long mTotalTime;

public CircularProgressLayout(Context context) {
this(context, (AttributeSet) null);
}

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

public CircularProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}

public CircularProgressLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// super(context, attrs, defStyleAttr, defStyleRes);
// } else {
// super(context, attrs);
// }

this.mStartingRotation = 0.75F;
this.mProgressDrawable = new CircularProgressDrawable(context);
this.mProgressDrawable.setProgressRotation(0.75F);
this.mProgressDrawable.setStrokeCap(Cap.BUTT);
this.setBackground(this.mProgressDrawable);
this.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {

@Override
public void onChildViewAdded(View parent, View child) {
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.gravity = 17;
child.setLayoutParams(params);
}

@Override
public void onChildViewRemoved(View parent, View child) {
}
});
this.mController = new CircularProgressLayoutController(this);
Resources r = context.getResources();
TypedArray a = r.obtainAttributes(attrs, R.styleable.CircularProgressLayout);
int type = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
type = a.getType(R.styleable.CircularProgressLayout_colorSchemeColors);
} else {
type = a.getInt(R.styleable.CircularProgressLayout_colorSchemeColors, 0);
}
if (type != 1 && a.hasValue(R.styleable.CircularProgressLayout_colorSchemeColors)) {
this.setColorSchemeColors(a.getColor(R.styleable.CircularProgressLayout_colorSchemeColors, -16777216));
} else {
int arrayResId = a.getResourceId(R.styleable.CircularProgressLayout_colorSchemeColors, R.array.circular_progress_layout_color_scheme_colors);
this.setColorSchemeColors(this.getColorListFromResources(r, arrayResId));
}

this.setStrokeWidth((float) a.getDimensionPixelSize(R.styleable.CircularProgressLayout_strokeWidth, r.getDimensionPixelSize(R.dimen.circular_progress_layout_stroke_width)));
this.setBackgroundColor(a.getColor(R.styleable.CircularProgressLayout_backgroundColor, ContextCompat.getColor(context, R.color.circular_progress_layout_background_color)));
this.setIndeterminate(a.getBoolean(R.styleable.CircularProgressLayout_indeterminate, false));
a.recycle();
}
}
private int[] getColorListFromResources(Resources resources, int arrayResId) {
TypedArray colorArray = resources.obtainTypedArray(arrayResId);
int[] colors = new int[colorArray.length()];

for (int i = 0; i < colorArray.length(); ++i) {
colors[i] = colorArray.getColor(i, 0);
}

colorArray.recycle();
return colors;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (this.getChildCount() != 0) {
View childView = this.getChildAt(0);
this.mProgressDrawable.setCenterRadius((float) Math.min(childView.getWidth(), childView.getHeight()) / 2.0F);
} else {
this.mProgressDrawable.setCenterRadius(0.0F);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
this.mController.reset();
}

@Override
public void setBackgroundColor(@ColorInt int color) {
this.mProgressDrawable.setBackgroundColor(color);
}

@ColorInt
public int getBackgroundColor() {
return this.mProgressDrawable.getBackgroundColor();
}

@NonNull
public CircularProgressDrawable getProgressDrawable() {
return this.mProgressDrawable;
}

public void setIndeterminate(boolean indeterminate) {
this.mController.setIndeterminate(indeterminate);
}

public boolean isIndeterminate() {
return this.mController.isIndeterminate();
}

public void setTotalTime(long totalTime) {
if (totalTime <= 0L) {
throw new IllegalArgumentException("Total time should be greater than zero.");
} else {
this.mTotalTime = totalTime;
}
}

public long getTotalTime() {
return this.mTotalTime;
}

public void startTimer() {
this.mController.startTimer(this.mTotalTime, 16L);
this.mProgressDrawable.setProgressRotation(this.mStartingRotation);
}

public void stopTimer() {
this.mController.stopTimer();
}

public boolean isTimerRunning() {
return this.mController.isTimerRunning();
}

public void setStartingRotation(float rotation) {
this.mStartingRotation = rotation;
}

public float getStartingRotation() {
return this.mStartingRotation;
}

public void setStrokeWidth(float strokeWidth) {
this.mProgressDrawable.setStrokeWidth(strokeWidth);
}

public float getStrokeWidth() {
return this.mProgressDrawable.getStrokeWidth();
}

public void setColorSchemeColors(int... colors) {
this.mProgressDrawable.setColorSchemeColors(colors);
}

public int[] getColorSchemeColors() {
return this.mProgressDrawable.getColorSchemeColors();
}

@Nullable
public CircularProgressLayout.OnTimerFinishedListener getOnTimerFinishedListener() {
return this.mController.getOnTimerFinishedListener();
}

public void setOnTimerFinishedListener(@Nullable CircularProgressLayout.OnTimerFinishedListener listener) {
this.mController.setOnTimerFinishedListener(listener);
}

public interface OnTimerFinishedListener {
void onTimerFinished(CircularProgressLayout var1);
}
}

控制類

class CircularProgressLayoutController {
    final CircularProgressLayout mLayout;
    @VisibleForTesting
    CountDownTimer mTimer;
    private boolean mIsIndeterminate;
    boolean mIsTimerRunning;
    @Nullable
    CircularProgressLayout.OnTimerFinishedListener mOnTimerFinishedListener;

    CircularProgressLayoutController(CircularProgressLayout layout) {
        this.mLayout = layout;
    }

    @Nullable
    public CircularProgressLayout.OnTimerFinishedListener getOnTimerFinishedListener() {
        return this.mOnTimerFinishedListener;
    }

    public void setOnTimerFinishedListener(@Nullable CircularProgressLayout.OnTimerFinishedListener listener) {
        this.mOnTimerFinishedListener = listener;
    }

    boolean isIndeterminate() {
        return this.mIsIndeterminate;
    }

    boolean isTimerRunning() {
        return this.mIsTimerRunning;
    }

    void setIndeterminate(boolean indeterminate) {
        if (this.mIsIndeterminate != indeterminate) {
            this.mIsIndeterminate = indeterminate;
            if (this.mIsIndeterminate) {
                if (this.mIsTimerRunning) {
                    this.stopTimer();
                }

                this.mLayout.getProgressDrawable().start();
            } else {
                this.mLayout.getProgressDrawable().stop();
            }

        }
    }

    void startTimer(long totalTime, long updateInterval) {
        this.reset();
        this.mIsTimerRunning = true;
        this.mTimer = new CircularProgressLayoutController.CircularProgressTimer(totalTime, updateInterval);
        this.mTimer.start();
    }

    void stopTimer() {
        if (this.mIsTimerRunning) {
            this.mTimer.cancel();
            this.mIsTimerRunning = false;
            this.mLayout.getProgressDrawable().setStartEndTrim(0.0F, 0.0F);
        }

    }

    void reset() {
        this.setIndeterminate(false);
        this.stopTimer();
        this.mLayout.getProgressDrawable().setStartEndTrim(0.0F, 0.0F);
    }

    private class CircularProgressTimer extends CountDownTimer {
        private final long mTotalTime;

        CircularProgressTimer(long totalTime, long updateInterval) {
            super(totalTime, updateInterval);
            this.mTotalTime = totalTime;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            CircularProgressLayoutController.this.mLayout.getProgressDrawable().setStartEndTrim(0.0F, 1.0F - (float) millisUntilFinished / (float) this.mTotalTime);
            CircularProgressLayoutController.this.mLayout.invalidate();
        }

        @Override
        public void onFinish() {
            CircularProgressLayoutController.this.mLayout.getProgressDrawable().setStartEndTrim(0.0F, 1.0F);
            if (CircularProgressLayoutController.this.mOnTimerFinishedListener != null) {
                CircularProgressLayoutController.this.mOnTimerFinishedListener.onTimerFinished(CircularProgressLayoutController.this.mLayout);
            }

            CircularProgressLayoutController.this.mIsTimerRunning = false;java
        }
    }
}

資源文件

<declare-styleable name="CircularProgressLayout">
<!-- Sets the color of the background circle. -->
<attr name="backgroundColor" format="color" />
<!-- Sets the stroke width of the progress indicator. -->
<attr name="strokeWidth" format="dimension" />
<!-- Sets the color scheme used by the progress indicator. This may be an array of colors or
a single color. If an array of colors is used, first color will be used for determinate
progress indicator, while the rest will be shown in order during indeterminate spinner. -->
<attr name="colorSchemeColors" format="reference|color" />
<!-- Sets if the progress should be shown as an indeterminate spinner. -->
<attr name="indeterminate" format="boolean" />
</declare-styleable>


<array name="circular_progress_layout_color_scheme_colors">
<item>@color/circular_progress_layout_red</item>
<item>@color/circular_progress_layout_yellow</item>
<item>@color/circular_progress_layout_green</item>
<item>@color/circular_progress_layout_blue</item>
</array>
<color name="circular_progress_layout_background_color">#00000000</color>
<color name="circular_progress_layout_blue">#FF2196F3</color>
<color name="circular_progress_layout_green">#FF4CAF50</color>xml
<color name="circular_progress_layout_red">#FFF44336</color>
<color name="circular_progress_layout_yellow">#FFFFEB3B</color>

<dimen name="circular_progress_layout_stroke_width">4dp</dimen>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粗仓,一起剝皮案震驚了整個(gè)濱河市弯蚜,隨后出現(xiàn)的幾起案子愤炸,更是在濱河造成了極大的恐慌,老刑警劉巖唐础,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異矾飞,居然都是意外死亡一膨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門洒沦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來豹绪,“玉大人,你說我怎么就攤上這事微谓∩瘢” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵豺型,是天一觀的道長仲智。 經(jīng)常有香客問我,道長姻氨,這世上最難降的妖魔是什么钓辆? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上前联,老公的妹妹穿的比我還像新娘功戚。我一直安慰自己,他們只是感情好似嗤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布啸臀。 她就那樣靜靜地躺著,像睡著了一般烁落。 火紅的嫁衣襯著肌膚如雪乘粒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天伤塌,我揣著相機(jī)與錄音灯萍,去河邊找鬼。 笑死每聪,一個(gè)胖子當(dāng)著我的面吹牛旦棉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播药薯,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼绑洛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了果善?” 一聲冷哼從身側(cè)響起诊笤,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎巾陕,沒想到半個(gè)月后讨跟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鄙煤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年晾匠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梯刚。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凉馆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出亡资,到底是詐尸還是另有隱情澜共,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布锥腻,位于F島的核電站嗦董,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏瘦黑。R本人自食惡果不足惜京革,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一奇唤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧匹摇,春花似錦咬扇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坡垫,卻和暖如春隅居,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背葛虐。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棉钧,地道東北人屿脐。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像宪卿,于是被迫代替她去往敵國和親的诵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

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