import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class mainActivity extends Activity { Button btn; ProgressBar pb1, pb2; int count = 0; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { pb1.setProgress(msg.arg1); pb2.setProgress(msg.arg1); super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pb1 = (ProgressBar) findViewById(R.id.pb1); pb2 = (ProgressBar) findViewById(R.id.pb2); btn = (Button) findViewById(R.id.btn); pb1.setIndeterminate(false); pb2.setIndeterminate(false); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { for(int i = 0; i < 100; i++) { try { count++; Thread.sleep(100); Message msg = handler.obtainMessage(); msg.arg1 = count; msg.sendToTarget(); } catch(Exception e) { e.printStackTrace(); } } } }).start(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/pb1" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"></ProgressBar> <ProgressBar android:id="@+id/pb2" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" android:max="100" android:secondaryProgress="70"></ProgressBar> <Button android:id="@+id/btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="開始"></Button> </LinearLayout>自定義進度條旋轉所使用的XML文件
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <rotate android:drawable="@drawable/logining_ring" android:fromDegrees="0.0" android:toDegrees="360.0" android:pivotX="50.0%" android:pivotY="50.0%" /> </item> </layer-list>fromDegrees 爲動畫起始時物件的角度
<ProgressBar android:id="@id/pbWeb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="3dip" android:progressDrawable="@drawable/progressbar" />progressbar.xml
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape> <corners android:radius="10dip" /> <solid android:color="@color/white" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="@color/main_color" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="@color/main_color" /> </shape> </clip> </item> </layer-list>
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。html