android progressBar和seekBar的小DEMO

 

 

这里演示的是android ProgressBar的条形进度条,因为android条形进度条初始时静态的而不像圆形的进度条是动态的;

而seekbar是android的拖动条,使用场景比如在音乐播放或者视频播放的时候~~~~

xml布局文件(需要注意,在使用条形进度条的时候要选择的style类型为

"?android:attr/progressBarStyleHorizontal"

):

<RelativeLayout 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: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:background="#BBBB0000">

    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="70"/>
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pb"
        android:text="拖动"/>

</RelativeLayout>

 

mainActivity.java文件:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements Runnable, OnSeekBarChangeListener {
    private ProgressBar pb;
    private Thread th;
    boolean statechange;
    SeekBar seekbar;
    private String tag = "SEEKBAR";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar)findViewById(R.id.pb);
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        seekbar.setOnSeekBarChangeListener(this);
        th = new Thread(this);
        th.start();
    }
    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        Log.v(tag, "拖动改变");
    }
    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        Log.v(tag, "改变中");
    }
    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        Log.v(tag, "停止改变");
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true){
            int current = pb.getProgress();
            int currentMax = pb.getMax();
            int seccurrent = pb.getSecondaryProgress();
            if (statechange == false) {
                if (current < currentMax) {
                    pb.setProgress(current +1);
                    pb.setSecondaryProgress(current +1);
                }else if (current == currentMax) {
                    statechange = true;
                }
            }
            else{
                if (current > 0) {
                    pb.setProgress(current -1);
                    pb.setSecondaryProgress(current -1);
                }else if (current == 0) {
                    statechange = false;
                }
            }
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
    
}   

运行效果(进度条的业务代码是实现模拟进度的不断变化)

图片居然不能上传了今天 ,等一下再补上吧。

 

android progressBar和seekBar的小DEMO,布布扣,bubuko.com

android progressBar和seekBar的小DEMO

上一篇:七款Debug工具推荐:iOS


下一篇:javascript与 ios通讯解决办法