最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ProgressBar組件使用教程

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

1. 前言

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

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

2. ProgressBar屬性介紹

2.1 XML屬性

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

android:indeterminateBehavior

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

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

2.2 API屬性

當(dāng)然ProgressBar也提供如下方法來操作進(jìn)度條:

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

3. 水平進(jìn)度條

在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就是水平進(jìn)度條軌跡的顯示Drawable。我們繼續(xù)去看下progress_horizontal 的源碼

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- 進(jìn)度條的背景色-->
    <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>
    <!-- 緩沖進(jìn)度條的背景色-->
    <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>
    <!-- 下載過程中進(jìn)度條的顏色-->
    <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. 圓形進(jìn)度條

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

我們進(jìn)去 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 ,進(jìn)去再看下代碼:

<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)圓形進(jìn)度條的顏色,修改屬性為:android:indeterminateTint="#ff0000"

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

代碼:

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

沒加這句話是默認(rèn)灰色圓形進(jìn)度條, 加了之后就變成了紅色圓形進(jìn)度條。

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. 實例演示

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

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("下載進(jìn)度:" + 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="水平進(jìn)度條"
        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="圓形進(jìn)度條"
        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)文章

  • android貝塞爾曲線實現(xiàn)波浪效果

    android貝塞爾曲線實現(xiàn)波浪效果

    這篇文章主要為大家詳細(xì)介紹了android貝塞爾曲線實現(xiàn)波浪效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android使用控件ImageView加載圖片的方法

    Android使用控件ImageView加載圖片的方法

    這篇文章主要為大家詳細(xì)介紹了Android使用ImageView加載圖片的方法,Android ImageView如何加載網(wǎng)絡(luò)圖片資源,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 解析Android橫豎屏切換的問題

    解析Android橫豎屏切換的問題

    本篇文章是對Android中橫豎屏切換的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android自定義View實現(xiàn)公交成軌跡圖

    Android自定義View實現(xiàn)公交成軌跡圖

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)公交成軌跡圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android實現(xiàn)類似微信的文本輸入框 效果

    Android實現(xiàn)類似微信的文本輸入框 效果

    本文給大家介紹一下微信的文本輸入框是如何實現(xiàn)的,其實那只是個普通的文本框設(shè)了一個特殊的背景而已。具體微信怎么實現(xiàn)的,大家可以反編譯下,這里介紹下如何實現(xiàn)這個背景
    2017-05-05
  • Android之高效加載大圖的方法示例

    Android之高效加載大圖的方法示例

    這篇文章主要介紹了Android之高效加載大圖的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 淺析Android 的 MediaPlayer類

    淺析Android 的 MediaPlayer類

    本文主要介紹了Android的mediaplayer類作用和用法,并附上了關(guān)鍵代碼,有需要的朋友可以參考下
    2014-10-10
  • Android自定義View實現(xiàn)彈性小球效果

    Android自定義View實現(xiàn)彈性小球效果

    前段時間看到一個功能,是一個小球沿著固定軌跡彈動的效果,那么這篇文章小編給大家分享在Android中如何自定義View來實現(xiàn)彈性小球的效果,有需要的可以參考借鑒。
    2016-09-09
  • 解析Android 8.1平臺SystemUI 導(dǎo)航欄加載流程

    解析Android 8.1平臺SystemUI 導(dǎo)航欄加載流程

    這篇文章主要介紹了Android 8.1平臺SystemUI 導(dǎo)航欄加載流程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • Android ZxingPlus精簡的二維碼框架示例代碼

    Android ZxingPlus精簡的二維碼框架示例代碼

    下面小編就為大家分享一篇Android ZxingPlus精簡的二維碼框架示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評論

镇赉县| 南投县| 美姑县| 浏阳市| 宜章县| 肥东县| 新津县| 大悟县| 新野县| 扶余县| 柏乡县| 贵南县| 新丰县| 桓台县| 高平市| 望谟县| 宜黄县| 化德县| 荔波县| 龙游县| 塔城市| 虞城县| 牟定县| 睢宁县| 水富县| 郎溪县| 斗六市| 平凉市| 盐城市| 阿坝县| 健康| 大新县| 东至县| 黑龙江省| 图片| 红河县| 京山县| 镇巴县| 海晏县| 晋州市| 吉木萨尔县|