最近項目需要,做一個進度條,要求前面跑70%,後面跑剩下的30%,要求圓形
於是想到了最簡單的辦法,肯定是用安卓自帶的ProgressBar啊,這個View有很多屬性,查詢了很多屬性值
這裏感謝這幾位大佬的博客,給我指點迷津
好啦,如果要正常使用ProgressBar,上面的幾篇文章足夠了,這裏直接回到主題,我要自定義一個進度條,並且可以設置動畫從0到100,或者50到200,並且可以定義動畫的時間。那麼直接上代碼吧。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="@dimen/dimen_value_dp60"
android:layout_height="@dimen/dimen_value_dp60"
android:layout_centerInParent="true"
android:background="@drawable/circle_shape"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/circular_progress_bar"
/>
這裏要說一下,爲啥我上面說的圓形,這裏要設置style爲水平呢,其實這就如上面大佬的文章裏面說的那樣,因爲有這一行android:indeterminate=“false” 代表着允許使用不確定模式,在不確定模式下,進度條動畫無限循環,也就是模糊模式,圓形本身就是模糊模式,所以這裏不用擔心,當然你也可以去掉上面的屬性,或者改成大圓小圓的style都可以
下面來看Activity裏面的代碼
public void jindutiao(final ProgressBar view, int startprogress, int endprogress) {//進度條的控件,以及起始的值
progressBar.setVisibility(View.VISIBLE);
ValueAnimator animator = ValueAnimator.ofInt(startprogress, endprogress).setDuration(800);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setProgress((int) valueAnimator.getAnimatedValue());
}
});
animator.start();
}
我這裏直接寫了一個方法,傳入對應的view,以及起點和終點的數值即可,這裏動畫的執行時間設置的是0.8秒
最後附上佈局裏面的兩個文件:
circle_shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thickness="2dp"
android:useLevel="false"
>
<solid android:color="#28313F" />
</shape>
circular_progress_bar
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270"
>
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="3dp"
android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->
<gradient
android:angle="0"
android:endColor="@color/color_6DC6DA"
android:startColor="@color/color_6DC6DA"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>