传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
银弧刀
陆无双抬起头来,只见四名乞丐,一字排在门外,或高或矮,一齐望着自己。她曾用银弧刀伤了一个乞丐,一见这四人来意不善,心中暗暗吃惊。。。杨过听了她声音,也是大吃一惊,只听另一个女人声音道:“那叫化子背上的,明明是师妹的银弧刀,就可惜没能起下来认一下。”
今天我们学习如何利用Android平台“银弧刀”ProgressBar来实现各种样式的进度条,白的黄的都有^_^。实际生活中进度条常常用来提示用户后台正在执行比较耗时的操作,请等待一会儿。当操作执行完毕时,它就随风逝去了。下面给出该情景的案例:
一、案例技术要点
1.ProgressBar布局设置
style="?android:attr/progressBarStyleSmallTitle":小圆形进度条
style="?android:attr/progressBarStyleLarge":大圆形进度条
style="?android:attr/progressBarStyleHorizontal":水平进度条
2.设置有刻度效果的窗口
requestWindowFeature(Window.FEATURE_PROGRESS):设置窗口有进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS):设置窗口有模糊进度条
setProgressBarVisibility(true):设置进度条显示
setProgressBarIndeterminate(true):设置模糊进度条显示
3.android.widget.ProgressBar:Android进度条类
progressBar.setProgress(...):设置进度条刻度值
progressBar.getProgress():获取进度条刻度值
二、案例代码陈列
工程包目录
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.progressbar" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ProgressBarMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>strings.xml
<resources> <string name="app_name">ProgressBar大世界</string> </resources>main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小圆形进度条" /> <ProgressBar style="?android:attr/progressBarStyleSmallTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中圆形进度条" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大圆形进度条" /> <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平进度条" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:max="100" android:progress="30" android:secondaryProgress="60" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加刻度" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减小刻度" /> </LinearLayout> </LinearLayout>ProgressBarMainActivity.java
package com.android.progressbar; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; /** * ProgressBar案例:各种样式的进度条 * 向用户展示当前任务的进度 * @author lynnli1229 */ public class ProgressBarMainActivity extends Activity implements OnClickListener{ private ProgressBar progressBar; private Button button1, button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置有刻度效果的窗口 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); progressBar = (ProgressBar) findViewById(R.id.progressBar); setProgressBarVisibility(true); setProgressBarIndeterminate(true); setProgress(3500); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: progressBar.setProgress((int) (progressBar.getProgress() * 1.2)); progressBar.setSecondaryProgress((int) (progressBar.getSecondaryProgress() * 1.2)); break; case R.id.button2: progressBar.setProgress((int) (progressBar.getProgress() * 0.8)); progressBar.setSecondaryProgress((int) (progressBar.getSecondaryProgress() * 0.8)); break; } } }三、案例效果展示