前篇 如何使用 ProgressBar 進度條(測試成功)
在MainActivity.java下測試是沒有問題的
但是多數會用到這,都不知道寫到第幾層了
這時就都會用context代替Activity了Google翻了許文章,也測試了不少
終於找到解法
Ring determinate ProgressBar
環型準確進度條完成圖
ProgressBar
layout/progress_ring.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/white"
- android:orientation="vertical">
- <TextView
- android:id="@+id/progress_notice"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30dp"
- android:layout_marginTop="20dp"
- android:textStyle="bold"
- android:textColor="@color/colorPrimaryBlue"
- />
- <ProgressBar
- android:id="@+id/progressBar"
- android:indeterminate="false"
- android:layout_width="250dp"
- android:layout_height="250dp"
- android:layout_centerInParent="true"
- android:progressDrawable="@drawable/progress_ring"
- style="@android:style/Widget.ProgressBar.Horizontal"/>
- <TextView
- android:id="@+id/txtprogress"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_centerInParent="true"
- android:textSize="30dp"
- android:textStyle="bold"
- />
- </RelativeLayout>
複製代碼
res/drawable/progress_ring.xml
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/secondaryProgress">
- <shape
- android:shape="ring"
- android:useLevel="true"
- android:innerRadiusRatio="3.5"
- android:thickness ="20dp">
- <corners android:radius="3dp"/>
- <solid android:color="#FA5858"/>
- </shape>
- </item>
- <item android:id="@android:id/progress">
- <shape
- android:shape="ring"
- android:useLevel="true"
- android:innerRadiusRatio="3.5"
- android:thickness ="20dp">
- <corners android:radius="3dp"/>
- <solid android:color="#0099CC"/>
- </shape>
- </item>
- </layer-list>
複製代碼
在context下用進度條
- processingtitle = (String) context.getText(R.string.processingtitle);
- processingcontext = (String) context.getText(R.string.processingcontext);
-
- copytime =60000;
- pStatus = 0;
- handler = new Handler();
- /* set Activity progressBar into context */
- Activity a = (Activity) context;
- a.setContentView(R.layout.progress_ring);
- txtProgress = (TextView) ((Activity)context).findViewById(R.id.txtprogress);
- progressNotice = (TextView) ((Activity)context).findViewById(R.id.progress_notice);
- progressBar = (ProgressBar) ((Activity)context).findViewById(R.id.progressBar);
- /* progressBar count copytime transfer to percentage */
- new Thread(new Runnable() {
- @Override
- public void run() {
- progressBar.setVisibility(View.VISIBLE);
- progressBar.setSecondaryProgress(100);
- progressBar.setProgress(pStatus);
- progressBar.setMax(100);
- progressNotice.setText( processingtitle +" "+ processingcontext);
- while (pStatus <= 100) {
- handler.post(new Runnable() {
- @Override
- public void run() {
- progressBar.setProgress(pStatus);
- //Log.d("testpathi", "i="+pStatus + " copytime:"+(copytime/100)*pStatus);
- txtProgress.setText(pStatus + " %");
- }
- });
- try {
- Thread.sleep(copytime/100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- pStatus++;
- }
- }
- }).start();
複製代碼
這裡一定要用Handler,不用會無法更新
在這說明如何利用Shape畫出漸層的ProgressBar以下為parameter說明:
Android 提供五種shape:rectangle(矩形)、oval(橢圓)、line(直線)、ring(圓環)。
以下為此篇五個參數對五個參數的說明:- innerRadiusRatio : 該值是以比例的形式來指定內圓半徑。內圓半徑等於該shape的寬除以該值。或者說該值的倒數代表了內圓半徑佔整個shape寬的比例。默認值是9。當該值等於2的時候,內圓就將佔滿整個shape,從而我們將看不到圓環。
- thicknessRatio : 以比例的形式來指定圓環的寬窄。其算法與innerRadiusRatio相同。
- useLevel : 值為true意味著這是一個levelListDrawable(關於levelListDrawable又是另一個話題了)。當我們要畫一個圓環是,應當而且必須將該值設為false,否則會看不到畫面。
這邊我是使用innerRadiusRatio與thicknessRatio此兩個參數來設定內環與外環的比率。當然亦可以全部使用。
最後一個參數android:useLevel,一定要設為false才可以看到畫面
gradient
- android:angle - 必須為45的倍數,0表示從左到右,90表示從上到下。
- android:endColor - 漸變結束的顏色
- android:startColor - 漸變開始的顏色
- android:centerColor - 漸變中間的顏色
- android:type - ["linear" | "radial" | "sweep"] 共有3種漸變類型,線性(default)、放射漸變、掃描式漸變,這邊我使用sweep
Android 提供的Shape真的可以用在很多地方,尤其是對custom view的部分,可以節省很多時間喔!
參考文章
https://segmentfault.com/a/1190000006700118
https://www.jianshu.com/p/38bd13e0ded8
https://stackoverflow.com/questions/10782344/displaying-progressbar-using-threading
https://segmentfault.com/a/1190000006700118
https://medium.com/@hsiangyu69/android-%E5%88%A9%E7%94%A8shape%E7%95%AB%E5%87%BA%E6%BC%B8%E5%B1%A4%E7%9A%84progressbar-a39fe0030569
https://blog.csdn.net/pingchuanyang/article/details/9230349
https://demonuts.com/circular-progress-bar/
|