Android进度条函数,Android实用笔记——使用ProgressBar实现进度条
1、ProgressBar的分類
可以精確顯示進度(可以顯示刻度或者精確百分比)
不可以精確顯示精度(一直轉,類似于一個過場動畫)
2、關鍵屬性和方法
指定ProgressBar顯示風格
style="?android:attr/progressBarStyleLarge"? ? 大環形進度條
style="?android:attr/progressBarStyleSmall" ? ?小環形進度條
style="?android:attr/progressBarStyleHorizontal" ? ?水平進度條
ProgressBar的關鍵屬性
android:max="100"? ? 最大顯示進度
android:progress="50"? ? 第一顯示進度
android:secondaryProgress="80"? ? 第二顯示進度
android:indenterminate="true"? ? 設置是否精確顯示(true表示不精確顯示進度,false表示精確顯示進度)
ProgressBar的關鍵方法
setProgress(int)? ? 設置第一進度
setSecondaryProgress(int)????設置第二進度
setProgress()? ? 獲取第一進度
getSecondaryProgress()????獲取第二進度
incrementProgressBy(int)? ? 增加或減少第一進度
incrementSecondaryProgressBy(int)? ? 增加或減少第二進度
getMax()? ? 獲取最大進度
3、標題欄中的ProgressBar
package com.example.myandroidprogressbar;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1、啟用窗口特征,啟用帶進度何不帶進度的兩種進度條
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
//顯示兩種進度條
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
//Max=10000
setProgress(600);
}
}
4、界面中四種不同形式的進度條
修改activity_main.xml文件如下
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progressBar1"
android:layout_toRightOf="@+id/progressBar1" />
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/horiz"
android:layout_toRightOf="@+id/progressBar2" />
android:id="@+id/horiz"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_below="@+id/progressBar1"
android:max="100"
android:progress="50"
android:secondaryProgress="80" />
5、通過按鈕改變進度條的數值并動態改變
修改activity_main.xml文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/horiz"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="80" />
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/horiz"
android:layout_below="@+id/horiz"
android:text="@string/add" />
android:id="@+id/reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/add"
android:layout_below="@+id/add"
android:text="@string/reduce" />
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/reduce"
android:layout_below="@+id/reduce"
android:text="@string/reset" />
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/reset"
android:layout_alignRight="@+id/horiz"
android:layout_below="@+id/reset"
android:layout_marginTop="16dp" />
修改MainActivity.java文件
package com.example.myandroidprogressbar;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
//1、聲明部分控件
private ProgressBar progress;
private Button add;
private Button reduce;
private Button reset;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2、初始化控件
init();
}
private void init() {
// TODO Auto-generated method stub
//3、綁定
progress=(ProgressBar) findViewById(R.id.horiz);
add=(Button) findViewById(R.id.add);
reduce=(Button) findViewById(R.id.reduce);
reset=(Button) findViewById(R.id.reset);
text=(TextView) findViewById(R.id.text);
//4、獲取各進度條進度
//獲取第一進度條的進度
//int first=progress.getProgress();
//湖區第二進度條的進度
//int second=progress.getSecondaryProgress();
//獲取進度條的最大進度
//int max=progress.getMax();
//5、將數據顯示到textView中
//text.setText("第一進度的百分比:"+(int)(first/(float)max*100)+
//"% 第二進度的百分比:"+(int)(second/(float)max*100)+"%");
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
}
//6、設置按鈕點擊響應事件,改變進度條數據
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.add:
//增加第一進度和第二進度10個刻度
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
case R.id.reduce:
//減少第一進度和第二進度10個刻度
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
//重置
progress.setProgress(50);
progress.setSecondaryProgress(80);
break;
}
//7、因為每一次點擊都會調用onClick這個函數,并且一定會執行switch這個判斷。
//所以沒必要在每一個分支中刷新textView的內容,只需要在switch分支外面寫就好了,
//之前的向textView中寫入數據的應該注釋掉
text.setText("第一進度的百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+
"% 第二進度的百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");
}
}
6、對話框形式的進度條(ProgressDialog)
修改activity_main.xml文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:text="顯示進度條對話框" />
修改MainActivity.java文件
package com.example.myandroidprogressdialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
//1、聲明progressDialog的對象以及按鈕
private ProgressDialog prodialog;
private Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
//2、綁定
show=(Button) findViewById(R.id.show);
//3、設置監聽器
show.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//4、設置頁面顯示風格
//新建progressDialog對象
prodialog=new ProgressDialog(MainActivity.this);
//給對話框設置顯示風格
prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置標題
prodialog.setTitle("哈哈哈");
//設置對話框里的而文字信息
prodialog.setMessage("我好聰明啊");
//設置圖標
prodialog.setIcon(R.drawable.ic_launcher);
//5、設定關于progressBar的一些屬性
//設定最大進度
prodialog.setMax(100);
//設定初始化進度
prodialog.incrementProgressBy(50);
//進度條是明確顯示進度的
prodialog.setIndeterminate(false);
//6、設定一個確定按鈕
prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"快點愛上我", Toast.LENGTH_SHORT);
}
});
//7、是否可以通過返回按鈕退出對話框
prodialog.setCancelable(true);
//8、顯示progressDialog
prodialog.show();
}
}
、
7、自定義ProgressBar的樣式
首先我們需要知道系統預定義的水平進度條的樣式是如何加載的。這樣我們需要回到activity_main.xml文件中來。
我們注意到下面一行代碼:
style="?android:attr/progressBarStyleHorizontal"
事實上,他真正的加載文件位于
style="@android:style/Widget.ProgressBar.Horizontal"
我們通過ctrl+鼠標左鍵點擊引號中的位置,就可以看到系統自帶的橫向進度條的樣式
false
@android:drawable/progress_horizontal
@android:drawable/progress_indeterminate_horizontal
20dip
20dip
true
其中的android:progressDrawable可以去加載一個自己自定義的布局樣式的一個文件。我們同樣用ctrl+鼠標左鍵打開這里的樣式文件,看看系統是如何設定的
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
不難發現,我們可以自己對他的顏色進行修改,以達到自定義的目的。這里我簡單把顏色修改了一下
android:startColor="#B9A4FF"
android:centerColor="#C6B7FF"
android:centerY="0.75"
android:endColor="#C3B2FF"
android:angle="270"
/>
android:startColor="#57E8FF"
android:centerColor="#74EBFF"
android:centerY="0.75"
android:endColor="#8EEFFF"
android:angle="270"
/>
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
運行即可得到自定義進度條的結果,可自行與本文之前的截圖對比,可以看到很明顯的差別,盡管顏色丑了點。
希望大家能以小見大,做一些更有意思的嘗試。
總結
以上是生活随笔為你收集整理的Android进度条函数,Android实用笔记——使用ProgressBar实现进度条的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OJ1160: 矩阵的最大值(指针专题)
- 下一篇: Wordpress 修改 mysql 插