ProgressBar的应用(一)、长形进度条

来源:互联网 发布:淘宝店铺软文 编辑:程序博客网 时间:2024/06/06 03:07

ProgressBar的Xml属性

属性 描述 android:animationResolution 超时的动画帧之间的毫秒 ;必须是一个整数值,如“100”。 android:indeterminate 是否允许使用不确定模式,在不确定模式下,进度条动画无限循环 android:indeterminateBehavior 定义当进度达到最大时,不确定模式的表现;该值必须为repeat或者cycle,repeat表示进度从0重新开始;cycle表示进度保持当前值,并且回到0 android:indeterminateDrawable 定义不确定模式是否可拉 android:indeterminateDuration 时间不定的动画 android:indeterminateOnly 限制为不定模式 android:interpolator android:max 定义进度的最大值 android:maxHeight 进度Widget最大高 android:miniHeight 进度Widget最小高 android:maxWidth 进度Widget最大宽 android:minWidth 进度Widget最小宽 android:mirrorForRtl 定义了相关画板如果需要反映在RTL模式 android:progress 设置进度的默认值,值介于0到max之间 android:progressDrawable android:secondaryProgress 定义二级进度值,值介于0到max。该进度在主进度和背景之间。比如用于网络播放视频时,二级进度用于表示缓冲进度,主进度用于表示播放进度。

不同样式的进度条的使用

Activity代码:

import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.ProgressBar;import android.widget.TextView;public class MainActivity extends Activity {    private ProgressBar mProgress2;    private ProgressBar mProgress3;    private int mProgressStatus2 = 0;    private int mProgressStatus3 = 0;    private int i = 0;    private int k;    private int cycle = 0;    private TextView textView3;    private TextView textView2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mProgress2 = (ProgressBar) findViewById(R.id.progressBar2);        mProgress3 = (ProgressBar) findViewById(R.id.progressBar3);        textView2 = (TextView) this.findViewById(R.id.Bar2_runtime);        textView3 = (TextView) this.findViewById(R.id.Bar3_runtime);        // Bar3        new Thread(new Runnable() {            @Override            public void run() {                while (mProgressStatus3 < 100) {                    for (; i < 101; i++) {                        try {                            Thread.sleep(50);                            mProgressStatus3 = doWork(i);                            Message msg = new Message();                            msg.what = i * 50;                            mHandler3.sendMessage(msg);                            mHandler3.post(new Runnable() {                                @Override                                public void run() {                                    mProgress3.setProgress(mProgressStatus3);                                }                            });                        } catch (Exception ex) {                            ex.printStackTrace();                        }                    }                }                // textView3.setText(Long.toString(date_3.getTime()-time));//不能在线程中改UI            }        }).start();        // Bar2        new Thread(new Runnable() {            @Override            public void run() {                while (mProgressStatus2 < 105) {                    for (k = 0; k < 100; k++) {                        try {                            Thread.sleep(50);                            mProgressStatus2 = doWork(k);                            Message msg = new Message();                            msg.what = (k + 100 * cycle) * 50;                            mHandler2.sendMessage(msg);                            mHandler2.post(new Runnable() {                                @Override                                public void run() {                                    mProgress2.setProgress(mProgressStatus2);                                }                            });                        } catch (Exception ex) {                            ex.printStackTrace();                        }                    } //                    cycle++;                }            }        }).start();    }    public Handler mHandler3 = new Handler() {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            setTime(msg.what, textView3);        }    };    public Handler mHandler2 = new Handler() {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            setTime(msg.what, textView2);        }    };    protected void setTime(int i, TextView textView) {        long diff = (long) i;        long hours = diff / (1000 * 60 * 60);        long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);        long seconds = (diff - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;        System.out.println("" + hours + "小时" + minutes + "分" + seconds + "秒");        textView.setText("" + hours + "小时" + minutes + "分" + seconds + "秒");    }    protected int doWork(int i) {        return i;    }}

Xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <ProgressBar        android:id="@+id/progressBar1"        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_width="200dp"        android:layout_height="20dp"        android:indeterminate="true"        android:indeterminateBehavior="cycle" />    <TextView         android:id="@+id/Bar1_runtime"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="已运行:"        android:textColor="#FFFFFF"/>    <ProgressBar        android:id="@+id/progressBar2"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="200dp"        android:layout_height="20dp"         android:max="100"/>    <TextView         android:id="@+id/Bar2_runtime"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="已运行:"        android:textColor="#FF0000"/>    <ProgressBar        android:id="@+id/progressBar3"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:max="100" />    <TextView         android:id="@+id/Bar3_runtime"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="已运行:"        android:textColor="#FF0000"/></LinearLayout>

运行之后:

这里写图片描述

过一会

这里写图片描述

可以看到
第一个进度条虽然没有进度显示但一直有动画,不会停止。
第二个进度条有进度显示,当进度到达100后会继续从0开始,下面是运行时间。
第三个进度条在到达100后停止动画。
代码下载:多种长形进度条的使用

0 0