自定義view:ProgressBar 前景色、背景色、平滑顯示進度(簡略版)

一、要實現的效果:

1、底色;2、進度色(有斜條紋);3、有文字顯示狀態;4、平滑地展示進度(有前進的過程)。

二、實現如下:

1、佈局

使用FrameLayout + textView實現文字狀態顯示。

ProgressBar的 屬性android:progressDrawable中設置 底色、進度色(實際就是讓設計切的一張圖片,clip標籤可以實現進度顯示效果)。

            <!-- 進度條 -->
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_4"
                android:layout_marginBottom="@dimen/dp_4">

                <ProgressBar
                    android:id="@+id/flash_sale_progress_bar"
                    android:layout_width="match_parent"
                    android:layout_height="8dp"
                    android:layout_gravity="center"
                    style="@android:style/Widget.ProgressBar.Horizontal"
                    android:progressDrawable="@drawable/flash_sale_homepage_progress_drawable"
                    android:progress="60"/>

                <TextView
                    android:id="@+id/flash_sale_progress_bar_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:layout_marginLeft="4dp"
                    android:layout_gravity="center_vertical"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/common_color_fffeff"
                    android:textSize="8dp"
                    tools:text="Terjual(100%)" />

            </FrameLayout>
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  設置背景色  -->
    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <solid android:color="#fffccaca" />
            <corners android:radius="100dp" />
        </shape>
    </item>

    <!--  設置進度條顏色  -->
    <item android:id="@android:id/progress">
        <!--clip標籤控制這個drawable的剪切區域,以及相相對於容器的對齊方式,android中的進度條就是使用一個ClipDrawable實現效果的,它根據level的屬性值,決定剪切區域的大小-->
        <clip>
            <bitmap android:src="@drawable/flash_sale_home_page_progress_foreground"/>
        </clip>
    </item>

</layer-list>

2、代碼中設置進度

  • 使用屬性動畫,可實現平滑進度顯示。時間、速度模式 均可設置。
    /**
     * 設置ProgressBar的進度(平滑的增長)
     * @param progressBar progressBar
     * @param progress 取值0-100
     */
    public static void setProgressSmooth(ProgressBar progressBar, int progress) {

        ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", progress);
        // 1 second
        animation.setDuration(1000);
        //先加速在減速
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        animation.start();

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章