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

Android開發(fā)使用ProgressBar實現(xiàn)進(jìn)度條功能示例

 更新時間:2019年03月23日 11:41:35   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)使用ProgressBar實現(xiàn)進(jìn)度條功能,結(jié)合實例形式分析了Android進(jìn)度條ProgressBar的具體樣式、布局與功能實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Android開發(fā)使用ProgressBar實現(xiàn)進(jìn)度條功能。分享給大家供大家參考,具體如下:

進(jìn)度條ProgressBar的使用主要有兩種方向;

1.使用官方默認(rèn)樣式

2.使用自定義樣式

先看效果:

詳細(xì)代碼實現(xiàn)文末給出

關(guān)于系統(tǒng)自帶樣式:

style="@android:style 中有許多系統(tǒng)自帶樣式,大家可以更具自身喜好選擇。

如果不選擇 style 系統(tǒng)會默認(rèn)使用上圖中紅色的樣式。

關(guān)于自定義樣式:

這里我們最好看看源碼 很容易理解

主要分為三個部分:當(dāng)前進(jìn)度、緩沖進(jìn)度、以及背景 三個屬性

這里我們通過在drawable里新建my_bar.xml來實現(xiàn)

這里有個注意點 很多人寫了xml后發(fā)現(xiàn) 直接就顯示滿進(jìn)度 而不是緩慢增長

由于是替換系統(tǒng)自帶樣式,所以id必須與系統(tǒng)保持一致:(如:android:id="@android:id/background"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!--定義軌道背景-->
  <item android:id="@android:id/background"
    android:drawable="@drawable/no"/>
  <!--定義軌道上已完成部分的樣式-->
  <item android:id="@android:id/progress"
    android:drawable="@drawable/ok"/>
</layer-list>

這里對比下系統(tǒng)源碼就很好理解了:

這里的模擬方法采用的是線程結(jié)合Handler

由于線程不能直接改變控件屬性 所以需要用Handler來接受線程發(fā)出的Message

具體方法如下:

public class MainActivity extends Activity {
  //記錄ProgressBar的完成進(jìn)度
  private int sum1=0,sum2 = 0 ;
  ProgressBar bar1,bar2;
  //創(chuàng)建一個負(fù)責(zé)更新進(jìn)度的Handler
  Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      //表明消息是本程序發(fā)送的
      if (msg.what == 0x111){
        bar1.setProgress(sum1);
        bar2.setProgress(sum2);
      }
    }
  };
  //模擬耗時
  Thread thread = new Thread(){
    @Override
    public void run() {
      while (sum2 < 100){
        //bar1獲取完成工作的百分比
        if (sum1 > 100){
          sum1 = 100;
          if (sum2<100){
            sum2 += (int) (Math.random()*25);
          }else {
            sum2 = 100;
            thread.stop();
          }
          sum1=0;
        }else {
          sum1 = sum1 + (int) (Math.random()*25);
        }
        try{
          Thread.sleep(1000);
        }catch (InterruptedException e){
          e.printStackTrace();
        }
        //更新ProgressBar
        mHandler.sendEmptyMessage(0x111);
      }
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bar1 = (ProgressBar) findViewById(R.id.bar);
    bar2 = (ProgressBar) findViewById(R.id.bar2);
    thread.start();
  }
}

最后在給出布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical">
  <android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetStart="0dp"
    android:background="#9FB6CD">
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <ProgressBar
        android:id="@+id/toolbar_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true" />
    </RelativeLayout>
  </android.support.v7.widget.Toolbar>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--定義一個大環(huán)型進(jìn)度條-->
    <ProgressBar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@android:style/Widget.ProgressBar.Large"/>
    <!--定義一個中等大小環(huán)形進(jìn)度條-->
    <ProgressBar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <!--定義一個小進(jìn)度條-->
    <ProgressBar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@android:style/Widget.ProgressBar.Small"/>
  </LinearLayout>
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="任務(wù)完成的進(jìn)度"/>
  <!--定義一個大水平進(jìn)度條-->
  <ProgressBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
  <!--頂一個水平進(jìn)度條,并改變軌道外觀-->
  <ProgressBar
    android:id="@+id/bar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progressDrawable="@drawable/my_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android自定義可左右滑動和點擊的折線圖

    Android自定義可左右滑動和點擊的折線圖

    這篇文章主要為大家詳細(xì)介紹了Android自定義可左右滑動和點擊的折線圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Android控件系列之CheckBox使用介紹

    Android控件系列之CheckBox使用介紹

    CheckBox和Button一樣,也是一種古老的控件,它的優(yōu)點在于,不用用戶去填寫具體的信息,只需輕輕點擊,缺點在于只有“是”和“否”兩種情況,但我們往往利用它的這個特性,來獲取用戶的一些信息
    2012-11-11
  • 解決Android Studio 代碼自動提示突然失效的問題

    解決Android Studio 代碼自動提示突然失效的問題

    這篇文章主要介紹了解決Android Studio 代碼自動提示突然失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android?RecyclerBarChart繪制使用教程

    Android?RecyclerBarChart繪制使用教程

    這篇文章主要為大家介紹了Android?RecyclerBarChart繪制使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android Notification的多種用法總結(jié)

    Android Notification的多種用法總結(jié)

    這篇文章主要介紹了Android Notification的多種用法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android Imageloader的配置的實現(xiàn)代碼

    Android Imageloader的配置的實現(xiàn)代碼

    這篇文章主要介紹了Android Imageloader的配置的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Jetpack?Compose?Canvas繪制超詳細(xì)介紹

    Jetpack?Compose?Canvas繪制超詳細(xì)介紹

    Canvas?是允許您在屏幕上指定區(qū)域并在此區(qū)域上執(zhí)行繪制的組件。您必須使用修飾符指定尺寸,無論是通過Modifier.size修飾符指定確切尺寸,還是通過Modifier.fillMaxSize,ColumnScope.weight等相對于父級指定精確尺寸。如果父級包裝了此子級,則僅必須指定確切尺寸
    2022-10-10
  • Android實現(xiàn)圖片左右滑動效果

    Android實現(xiàn)圖片左右滑動效果

    現(xiàn)在滑動效果用的比較多,尤其是在手機(jī)端上面,本文介紹了Android實現(xiàn)圖片左右滑動效果,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-10-10
  • Android 使用FragmentTabHost實現(xiàn)底部菜單功能

    Android 使用FragmentTabHost實現(xiàn)底部菜單功能

    這篇文章主要介紹了Android 使用FragmentTabHost實現(xiàn)底部菜單功能,詳細(xì)給大家介紹了FragmentTabHost配合Fragment的使用方法,需要的朋友可以參考下
    2017-12-12
  • Android仿網(wǎng)易云音樂播放界面

    Android仿網(wǎng)易云音樂播放界面

    這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易云音樂播放界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論