您的位置:首页 > 移动开发 > Android开发

Android(java)学习笔记130:ProgressBar使用的

2015-07-27 09:53 615 查看
首先我们看例程如下:

1.main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认进度条:"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress1"
//没有设置style,默认样式,中度旋转的圆形进度条
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小圆形进度条:"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress3"
style="?android:attr/progressBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大圆形进度条:"
android:layout_gravity="center_vertical"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress3"
style="?android:attr/progressBarStyleLarge"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="条形进度条:"
android:id="@+id/tv"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="progress2"
android:id="@+id/porb"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
/>
</LinearLayout>


(2)接下来是MainActivity.java文件:

package com.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
//使用Runnable接口
public class MainActivity extends Activity implements Runnable {
private Thread th ;            //声明一条线程
private ProgressBar pb ;     //声明一个进度条对象
private boolean stateChage; //标识进度值最大最小的状态
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例进度条对象
pb = (ProgressBar)findViewById(R.id.porb);
th = new Thread(this);//实例线程对象,当前MainActivity实现了Runnable接口,这样MainActivity本身可以作为参数实例化Thread()对象
th.start();//启动线程
}

@Override
public void run() {//实现Runnable接口抽象函数
while(true){
int current = pb.getProgress();//得到当前进度值
int currentMax = pb.getMax();//得到进度条的最大进度值
int secCurrent = pb.getSecondaryProgress();//得到底层当前进度值
//以下代码实现进度值越来越大,越来越小的一个动态效果
if(stateChage==false){
if(current>=currentMax){
stateChage=true;
}else{
//设置进度值
pb.setProgress(current+1);
//设置底层进度值
pb.setSecondaryProgress(current+1);
}
}else{
if(current<=0){
stateChage=false;
}else{
pb.setProgress(current-1);
pb.setSecondaryProgress(current-1);
}
}
// 设置长方形进度条进度变大和变小的速率快慢
             try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


总结:

1:Android的ProgressBar样式

style="?android:attr/progressBarStyleHorizontal"  长形进度条

style="?android:attr/progressBarStyleLarge"   超大号圆形ProgressBar

style="?android:attr/progressBarStyleLargeInverse"  超大号圆形ProgressBar,Inverse反向转向的

style="?android:attr/progressBarStyleSmall" 小号圆形ProgressBar

style="?android:attr/progressBarStyleSmallTitle" 标题型圆形ProgressBar

等等还有很多

2.setProgress和setSecondaryProgress区别:这么说你肯定明白:网络视频加载的时候有个播放进度和缓冲进度,播放进度一般可以用setProgerss来显示,而setSecondaryProgress就是用来显示缓冲进度的,同样的360的雷神引擎也是一样,setProgress是文件写入本地进度,setSecondaryProgress就是文件加载进度。还不明就自己写一个Demo测试一遍。写一个把复制文件的程序,setProgress表示往流里面读数据,setSecondaryProgerss表示从流里面输出数据


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: