TShopping

 找回密碼
 註冊
搜索
查看: 1396|回復: 0
打印 上一主題 下一主題

[教學] 如何使用 ProgressBar 進度條(測試成功)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2019-12-25 16:42:58 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
建立 自訂進度條
1.建立 自訂進度條 class
CustomProgress.java
  1. public class CustomProgress {

  2.    public static CustomProgress customProgress = null;
  3.    private Dialog mDialog;

  4.    public static CustomProgress getInstance() {
  5.        if (customProgress == null) {
  6.            customProgress = new CustomProgress();
  7.        }
  8.        return customProgress;
  9.    }

  10.    public void showProgress(Context context, String message, boolean cancelable) {
  11.        mDialog = new Dialog(context);
  12.     // no tile for the dialog
  13.        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  14.        mDialog.setContentView(R.layout.prograss_bar_dialog);
  15.        mProgressBar = (ProgressBar) mDialog.findViewById(R.id.progress_bar);
  16.     //  mProgressBar.getIndeterminateDrawable().setColorFilter(context.getResources()
  17.     // .getColor(R.color.material_blue_gray_500), PorterDuff.Mode.SRC_IN);
  18.        TextView progressText = (TextView) mDialog.findViewById(R.id.progress_text);
  19.        progressText.setText("" + message);
  20.        progressText.setVisibility(View.VISIBLE);
  21.        mProgressBar.setVisibility(View.VISIBLE);
  22.     // you can change or add this line according to your need
  23.        mProgressBar.setIndeterminate(true);
  24.        mDialog.setCancelable(cancelable);
  25.        mDialog.setCanceledOnTouchOutside(cancelable);
  26.        mDialog.show();
  27.    }

  28.    public void hideProgress() {
  29.        if (mDialog != null) {
  30.            mDialog.dismiss();
  31.            mDialog = null;
  32.        }
  33.    }
  34. }
複製代碼


2.建立 progress layout
prograss_bar_dialog.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="65dp"
  5.     android:background="@android:color/background_dark"
  6.     android:orientation="vertical">

  7.     <TextView
  8.         android:id="@+id/progress_text"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="40dp"
  11.         android:layout_above="@+id/progress_bar"
  12.         android:layout_marginLeft="10dp"
  13.         android:layout_marginStart="10dp"
  14.         android:background="@android:color/transparent"
  15.         android:gravity="center_vertical"
  16.         android:text=""
  17.         android:textColor="@android:color/white"
  18.         android:textSize="16sp"
  19.         android:visibility="gone" />

  20.     <-- Where the style can be changed to any kind of ProgressBar -->

  21.     <ProgressBar
  22.         android:id="@+id/progress_bar"
  23.         style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
  24.         android:layout_width="match_parent"
  25.         android:layout_height="30dp"
  26.         android:layout_alignParentBottom="true"
  27.         android:layout_alignParentLeft="true"
  28.         android:layout_alignParentStart="true"
  29.         android:layout_gravity="center"
  30.         android:background="@color/cardview_dark_background"
  31.         android:maxHeight="20dp"
  32.         android:minHeight="20dp" />

  33. </RelativeLayout>
複製代碼


3.使用progress Dialog code
  1. CustomProgress customProgress = CustomProgress.getInstance();

  2. // now you have the instance of CustomProgres
  3. // for showing the ProgressBar

  4. customProgress.showProgress(#Context, getString(#StringId), #boolean);

  5. // for hiding the ProgressBar

  6. customProgress.hideProgress();
複製代碼


上述已經完成簡易的ProgressBar
效果如圖



4.如要客製 progressbar
CustomProgressBarActivity.java:
  1. public class CustomProgressBarActivity extends AppCompatActivity {

  2.     private TextView txtProgress;
  3.     private ProgressBar progressBar;
  4.     private int pStatus = 0;
  5.     private Handler handler = new Handler();

  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_custom_progressbar);

  10.         txtProgress = (TextView) findViewById(R.id.txtProgress);
  11.         progressBar = (ProgressBar) findViewById(R.id.progressBar);

  12.         new Thread(new Runnable() {
  13.             @Override
  14.             public void run() {
  15.                 while (pStatus <= 100) {
  16.                     handler.post(new Runnable() {
  17.                         @Override
  18.                         public void run() {
  19.                             progressBar.setProgress(pStatus);
  20.                             txtProgress.setText(pStatus + " %");
  21.                         }
  22.                     });
  23.                     try {
  24.                         Thread.sleep(100);
  25.                     } catch (InterruptedException e) {
  26.                         e.printStackTrace();
  27.                     }
  28.                     pStatus++;
  29.                 }
  30.             }
  31.         }).start();

  32.     }
  33. }
複製代碼


activity_custom_progressbar.xml:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context="com.skholingua.android.custom_progressbar_circular.MainActivity" >


  10.     <RelativeLayout
  11.         android:layout_width="wrap_content"
  12.         android:layout_centerInParent="true"
  13.         android:layout_height="wrap_content">

  14.         <ProgressBar
  15.             android:id="@+id/progressBar"
  16.             style="?android:attr/progressBarStyleHorizontal"
  17.             android:layout_width="250dp"
  18.             android:layout_height="250dp"
  19.             android:layout_centerInParent="true"
  20.             android:indeterminate="false"
  21.             android:max="100"
  22.             android:progress="0"
  23.             android:progressDrawable="@drawable/custom_progressbar_drawable"
  24.             android:secondaryProgress="0" />


  25.         <TextView
  26.             android:id="@+id/txtProgress"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content"
  29.             android:layout_alignBottom="@+id/progressBar"
  30.             android:layout_centerInParent="true"
  31.             android:textAppearance="?android:attr/textAppearanceSmall" />
  32.     </RelativeLayout>
  33. </RelativeLayout>
複製代碼


custom_progressbar_drawable.xml
:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:fromDegrees="-90"
  4.     android:pivotX="50%"
  5.     android:pivotY="50%"
  6.     android:toDegrees="270" >

  7.     <shape
  8.         android:shape="ring"
  9.         android:useLevel="false" >
  10.         <gradient
  11.             android:centerY="0.5"
  12.             android:endColor="#FA5858"
  13.             android:startColor="#0099CC"
  14.             android:type="sweep"
  15.             android:useLevel="false" />
  16.     </shape>
  17. </rotate>
複製代碼

效果如圖


5.Determinate(準確) ProgressBar
A determinate ProgressBar shows the current progress towards a specific maximum value.
(設定進度條最大值)
Horizontal determinate ProgressBar
(水平準確進度條)
  1. <ProgressBar
  2.     android:id="@+id/progressBar"
  3.     android:indeterminate="false"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="10dp"
  6.     style="@android:style/Widget.ProgressBar.Horizontal"/>
複製代碼

Vertical determinate ProgressBar

(垂直準確進度條)
  1. <ProgressBar
  2.     android:id="@+id/progressBar"
  3.     android:indeterminate="false"
  4.     android:layout_width="10dp"
  5.     android:layout_height="match_parent"
  6.     android:progressDrawable="@drawable/progress_vertical"
  7.     style="@android:style/Widget.ProgressBar.Horizontal"/>
複製代碼


res/drawable/progress_vertical.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <item android:id="@android:id/background">
  4.         <shape>
  5.             <corners android:radius="3dp"/>
  6.             <solid android:color="@android:color/darker_gray"/>
  7.         </shape>
  8.     </item>
  9.     <item android:id="@android:id/secondaryProgress">
  10.         <clip android:clipOrientation="vertical" android:gravity="bottom">
  11.             <shape>
  12.                 <corners android:radius="3dp"/>
  13.                 <solid android:color="@android:color/holo_blue_light"/>
  14.             </shape>
  15.         </clip>
  16.     </item>
  17.     <item android:id="@android:id/progress">
  18.         <clip android:clipOrientation="vertical" android:gravity="bottom">
  19.             <shape>
  20.                 <corners android:radius="3dp"/>
  21.                 <solid android:color="@android:color/holo_blue_dark"/>
  22.             </shape>
  23.         </clip>
  24.     </item>
  25. </layer-list>
複製代碼


Ring determinate ProgressBar
(環型準確進度條)
  1. <ProgressBar
  2.     android:id="@+id/progressBar"
  3.     android:indeterminate="false"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content"
  6.     android:progressDrawable="@drawable/progress_ring"
  7.     style="@android:style/Widget.ProgressBar.Horizontal"/>
複製代碼


res/drawable/progress_ring.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <item android:id="@android:id/secondaryProgress">
  4.         <shape
  5.             android:shape="ring"
  6.             android:useLevel="true"
  7.             android:thicknessRatio="24"
  8.             android:innerRadiusRatio="2.2">
  9.             <corners android:radius="3dp"/>
  10.             <solid android:color="#0000FF"/>
  11.         </shape>
  12.     </item>

  13.     <item android:id="@android:id/progress">
  14.         <shape
  15.             android:shape="ring"
  16.             android:useLevel="true"
  17.             android:thicknessRatio="24"
  18.             android:innerRadiusRatio="2.2">
  19.             <corners android:radius="3dp"/>
  20.             <solid android:color="#FFFFFF"/>
  21.         </shape>
  22.     </item>
  23. </layer-list>
複製代碼


To use the determinate ProgressBar in an Activity.
Activity下用進度條
  1. ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  2. progressBar.setSecondaryProgress(100);
  3. progressBar.setProgress(10);
  4. progressBar.setMax(100);
複製代碼

Indeterminate(不準確) ProgressBar
An indeterminate ProgressBar shows a cyclic animation without an indication of progress.
Basic indeterminate ProgressBar (spinning wheel)
  1. <ProgressBar
  2.     android:id="@+id/progressBar"
  3.     android:indeterminate="true"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content"/>
複製代碼

Horizontal indeterminate ProgressBar (flat bar)
  1. <ProgressBar
  2.     android:id="@+id/progressBar"
  3.     android:indeterminate="true"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content"
  6.     style="@android:style/Widget.ProgressBar.Horizontal"/>
複製代碼

Other built-in ProgressBar styles
  1. style="@android:style/Widget.ProgressBar.Small"
  2. style="@android:style/Widget.ProgressBar.Large"
  3. style="@android:style/Widget.ProgressBar.Inverse"
  4. style="@android:style/Widget.ProgressBar.Small.Inverse"
  5. style="@android:style/Widget.ProgressBar.Large.Inverse"
複製代碼

To use the indeterminate ProgressBar in an Activity
  1. ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  2. progressBar.setVisibility(View.VISIBLE);
  3. progressBar.setVisibility(View.GONE);
複製代碼

Material Linear ProgressBar
A linear progress indicator should always fill from 0% to 100% and never decrease in value.
It should be represented by bars on the edge of a header or sheet that appear and disappear.
To use a material Linear ProgressBar just use in your xml:
  1. <ProgressBar
  2.     android:id="@+id/my_progressBar"  
  3.     style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content"/>
複製代碼

Indeterminate
To create indeterminate ProgressBar set the android:indeterminate attribute to true.
  1. <ProgressBar
  2.     android:id="@+id/my_progressBar"  
  3.     style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content"
  6.     android:indeterminate="true"/>
複製代碼


Determinate
To create determinate ProgressBar set the android:indeterminate attribute to false and use the android:max and the android:progress attributes:
  1. <ProgressBar  
  2.     android:id="@+id/my_progressBar"
  3.     style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  4.     android:indeterminate="false"
  5.     android:max="100"
  6.     android:progress="10"/>
複製代碼

Just use this code to update the value:
  1. ProgressBar progressBar = (ProgressBar) findViewById(R.id.my_progressBar);  
  2. progressBar.setProgress(20);
複製代碼

Buffer
To create a buffer effect with the ProgressBar set the android:indeterminate attribute to false and use the android:max, the android:progress and the android:secondaryProgress attributes:
  1. <ProgressBar  
  2.     android:id="@+id/my_progressBar"
  3.     style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content"
  6.     android:indeterminate="false"
  7.     android:max="100"
  8.     android:progress="10"
  9.     android:secondaryProgress="25"/>
複製代碼

The buffer value is defined by android:secondaryProgress attribute.
Just use this code to update the values:
  1. ProgressBar progressBar = (ProgressBar) findViewById(R.id.my_progressBar);
  2. progressBar.setProgress(20);
  3. progressBar.setSecondaryProgress(50);  
複製代碼
Indeterminate and Determinate
To obtain this kind of ProgressBar just use an indeterminate ProgressBar using the android:indeterminate attribute to true.
  1. <ProgressBar  
  2.     android:id="@+id/progressBar"
  3.     style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  4.     android:indeterminate="true"/>
複製代碼

Then when you need to switch from indeterminate to determinate progress use setIndeterminate() method .
  1. ProgressBar progressBar = (ProgressBar) findViewById(R.id.my_progressBar);  
  2. progressBar.setIndeterminate(false);
複製代碼
Tinting ProgressBar#
Using an AppCompat theme, the ProgressBar's color will be the colorAccent you have defined.
5.0
To change the ProgressBar color without changing the accent color you can use theandroid:theme attribute overriding the accent color:
  1. <ProgressBar  
  2.     android:theme="@style/MyProgress"
  3.     style="@style/Widget.AppCompat.ProgressBar" />

  4. <!-- res/values/styles.xml -->
  5. <style name="MyProgress" parent="Theme.AppCompat.Light">  
  6.     <item name="colorAccent">@color/myColor</item>
  7. </style>  
複製代碼

To tint the ProgressBar you can use in the xml file the attributes android:indeterminateTintMode and android:indeterminateTint
  1. <ProgressBar
  2.     android:indeterminateTintMode="src_in"
  3.     android:indeterminateTint="@color/my_color"
  4. />
複製代碼


另法1.
  1. public void increase_splash_bar (int from, int to)
  2.     {
  3.         final Handler handler1 = new Handler();
  4.         class Task implements Runnable {

  5.             int start,end;

  6.             Task(int a,int b) { start = a; end = b;}

  7.             @Override
  8.             public void run() {
  9.                 for (int i =start ; i <= end; i++) {
  10.                     final int value = i;
  11.                     try {
  12.                         Thread.sleep(20);
  13.                     } catch (InterruptedException e) {
  14.                         e.printStackTrace();
  15.                     }
  16.                     handler1.post(new Runnable() {
  17.                         @Override
  18.                         public void run() {
  19.                             progressBar.setProgress(value);
  20.                         }
  21.                     });
  22.                     if (i >=100){
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         progressBar.setVisibility(View.VISIBLE);
  28.         Thread t = new Thread(new Task(from, to));   //call it
  29.         t.start();

  30.     }
複製代碼

在Activity.java呼叫
  1. increase_splash_bar(0,100);
複製代碼


文章出處
https://stackoverflow.com/questions/42452738/android-update-progressbar-with-handler/42453199



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-19 15:44 , Processed in 0.064113 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表