實作水平進度條的自增長及漸變樣式
話不多說,上效果圖

程序十分容易,望諸君都能學會!
上代碼
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="實作進度條自增長"
android:textSize="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_weight="1"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/jdt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="當前進度條值為:"
android:textSize="50dp" />
</LinearLayout>
</LinearLayout>
在UI界面上先設立一個水平的進度條和用于顯示進度條值的TextView組件
代碼LinearLayout中的weight用于顯示比例
代碼中水平進度條的 progressDrawable 用于設定進度條的漸變
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int i=0;//用于顯示進度條的增長
TextView textView;//顯示當前進度條的值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=findViewById(R.id.progressBar);//帶入
textView=findViewById(R.id.textView2);//帶入
final Timer timer=new Timer();//實體化,設定計時器
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
i++;//自增長
textView.setText("當前進度條值為:"+i+"%");//用于顯示當前進度條的值
if(i==100){
timer.cancel();//當i=100時停止
}else{
progressBar.setProgress(i);//否則隨著i自增長
}
}
});
}
},100,100);//1秒回圈一次
}
}
代碼中已詳細的注釋了,如果還有什么疑問可以提出來,歡迎解答
最后設定進度條漸變
首先在drawable中新建

上代碼
jdt.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/progress">
<clip>
<shape>
<corners android:radius="5dp"/>
<gradient android:angle="0"
android:startColor="#33A72B"
android:endColor="#2C4EAD"/>
</shape>
</clip>
</item>
</layer-list>
shape設定形狀
corners是設定半徑
gradient是設定角度意思
后面分別跟隨的是起始顏色和結束顏色
祝好!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/261076.html
標籤:其他
