欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ProgressBar組件使用教程

 更新時間:2022年11月03日 11:02:34   作者:broadview_java  
Android ProgressBar分為水平進度條和圓形進度條, 看官方的劃分是Indeterminate Progress(不確定的進度) 和 Determinate Progress(決定進度)下面有2個demo一個是圓形的進度條和一個水平的進度條

1. 前言

進度條是UI界面中一種非常實用的組件,通常用于向用戶顯示某個耗時操作完成的百分比,進度條可以動態(tài)的顯示進度,因為避免長時間地執(zhí)行某個耗時操作時,讓用戶感覺程序失去了響應(yīng),從而更好地提高用戶界面的友好性。

進度條展示有兩種:水平進度條 和 環(huán)形進度條 ,下文分別詳細介紹。首先我們來看下進度條的相關(guān)屬性介紹。

2. ProgressBar屬性介紹

2.1 XML屬性

這里的andorid:progressDrawable用于指定進度條的軌道的繪制形式,該屬性可以指定為一個LayerDrawble對象的引用(該對象可以在XML文件中使用<layer-list>元素來進行配置)。

android:indeterminateBehavior

定義當進度達到最大時,不確定模式的表現(xiàn);該值必須為repeat或者cycle,repeat表示進度從0重新開始;cycle表示進度保持當前值,并且回到0

android:indeterminateDuration=“500”,每轉(zhuǎn)一圈的時間,ms

2.2 API屬性

當然ProgressBar也提供如下方法來操作進度條:

setProgress(int) //設(shè)置第一進度
setSecondaryProgress(int) //設(shè)置第二進度
getProgress() //獲取第一進度
getSecondaryProgress() //獲取第二進度
incrementProgressBy(int) //增加或減少第一進度, 當參數(shù)為正數(shù)時,進度條增加,當參數(shù)為負數(shù)時,進度條減少
incrementSecondaryProgressBy(int) //增加或減少第二進度
getMax() //獲取最大進度

3. 水平進度條

在xml中引用有兩種方式:

A為 style="?android:attr/progressBarStyleHorizontal"

B為 style="@android:style/Widget.ProgressBar.Horizontal"

查看B的源碼,相關(guān)屬性為:

    <style name="Widget.ProgressBar.Horizontal">
        <item name="indeterminateOnly">false</item>
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <item name="mirrorForRtl">true</item>
    </style>

上文中提到的progressDrawable就是水平進度條軌跡的顯示Drawable。我們繼續(xù)去看下progress_horizontal 的源碼

<?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>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    <!-- 緩沖進度條的背景色-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <!-- 下載過程中進度條的顏色-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
</layer-list>

上述代碼就是一個 LayerDrawble圖片,它是層次化的Drawable合集, 根元素為<layer-list> ,每一個item就是一個shape圖片,最后一個item顯示在最上層。

4. 圓形進度條

系統(tǒng)自帶的圓形進度條,都是一直轉(zhuǎn)圈狀態(tài),為不精確顯示進度 默認android:indeterminate屬性值為true 。

我們進去 android:style/Widget.ProgressBar.Large源碼看下

 <style name="Widget.ProgressBar.Large">
        <item name="indeterminateDrawable">@drawable/progress_large_white</item>
        <item name="minWidth">76dip</item>
        <item name="maxWidth">76dip</item>
        <item name="minHeight">76dip</item>
        <item name="maxHeight">76dip</item>
    </style>

就是一個indeterminateDrawable ,進去再看下代碼:

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white_76"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    android:frameDuration="100" />

看看 spinner_white_76 這個圖片:

是一個 rotate 動畫效果。

1. 我們來修改一下系統(tǒng)圓形進度條的顏色,修改屬性為:android:indeterminateTint="#ff0000"

源碼注釋:Tint to apply to the indeterminate progress indicator 翻譯:就是給進度條著色

代碼:

    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:indeterminateTint="#ff0000"/>

沒加這句話是默認灰色圓形進度條, 加了之后就變成了紅色圓形進度條。

2. 自定義轉(zhuǎn)圈動畫

    <ProgressBar
        android:id="@+id/progressbar2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:indeterminateDrawable="@drawable/bg_loading"/>

bg_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:drawable="@drawable/loading"
            android:fromDegrees="0.0"
            android:pivotX="50.0%"
            android:pivotY="50.0%"
            android:toDegrees="359.0"
            android:repeatMode="reverse"/>
    </item>
</layer-list>

loading.xml :

5. 實例演示

我們來寫一個水平進度條,模擬下載任務(wù)進度過程:

public class MainActivity extends AppCompatActivity {
    private ProgressBar horizontalProgress;
    private ProgressBar circleProgress;
    private Button startBtn;
    private TextView mTextView;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what >= 100) {
                horizontalProgress.setProgress(100);
                mTextView.setText("下載完成");
                removeCallbacksAndMessages(null);
                return;
            }
            int progress = msg.what;
            horizontalProgress.setProgress(progress);
            mTextView.setText("下載進度:" + progress + "%");
            Message message = Message.obtain();
            int temp = progress + 4;
            message.what = temp;
            mHandler.sendMessageDelayed(message, 1000);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        horizontalProgress = findViewById(R.id.progressbar1);
        mTextView = findViewById(R.id.tv_progress);
        circleProgress = findViewById(R.id.progressbar2);
        startBtn = findViewById(R.id.start_download);
    }
    @Override
    protected void onResume() {
        super.onResume();
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = Message.obtain();
                msg.what = 1;
                mHandler.sendMessage(msg);
                startBtn.setClickable(false);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_progress"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="水平進度條"
        android:textSize="20sp" />
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/progressbar1"
        android:layout_height="15dp"
        android:layout_width="match_parent"
        android:progress="0"
        android:max="100"/>
    <TextView
        android:id="@+id/tv_progress2"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_width="wrap_content"
        android:text="圓形進度條"
        android:textSize="20sp" />
    <ProgressBar
        android:id="@+id/progressbar2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:indeterminateDrawable="@drawable/bg_loading"
        />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/start_download"
        android:textSize="20sp"
        android:text="開始下載"/>

演示效果圖:

到此這篇關(guān)于Android ProgressBar組件使用教程的文章就介紹到這了,更多相關(guān)Android ProgressBar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論