• 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();
                }
            }
        }
        
    }   

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

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

  • 相关阅读:
    Spring Boot发布2.6.2、2.5.8:升级log4j2到2.17.0
    如何优雅地读写HttpServletRequest和HttpServletResponse的请求体
    更快的Maven来了
    Spring Cloud Gateway过滤器精确控制异常返回(实战,控制http返回码和message字段)
    NumPy学习笔记
    来自Java程序员的Python新手入门小结
    Java应用日志如何与Jaeger的trace关联
    Jaeger知识点补充
    分布式调用链跟踪工具Jaeger?两分钟极速体验
    Spring Cloud Gateway过滤器精确控制异常返回(实战,完全定制返回body)
  • 原文地址:https://www.cnblogs.com/tiejiangweigaibianercunzai/p/3892234.html
Copyright © 2020-2023  润新知