美文网首页Android开发经验谈Android开发面试题
ProgressBar(进度条)的使用和方法

ProgressBar(进度条)的使用和方法

作者: 你好_摆渡人 | 来源:发表于2019-10-14 13:49 被阅读0次
    image.png
    image.png

    activity:

    package com.example.myapplication;
    
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ProgressBar;
    
    import java.lang.ref.WeakReference;
    
    public class MainActivity extends AppCompatActivity {
        //该程序为模拟填充长度为100的数组
       private int[] data =new int[100];
       private int hasData=0;
       //记录progressBar的完成进度
        int status =0;
        private ProgressBar bar;
        private ProgressBar bar2;
        static class MyHander extends Handler
        {
            private WeakReference<MainActivity>activity;
            MyHander(WeakReference<MainActivity>activity){
                this.activity=activity;
            }
    
            @Override
            public void handleMessage(Message msg) {
                //表明消息是由该程序发送的
                if (msg.what==0x111)
                {
                    activity.get().bar.setProgress(activity.get().status);
                    activity.get().bar2.setProgress(activity.get().status);
                }
            }
        }
        //创建一个负责更新的进度条Handler
        MyHander myHander=new MyHander(new WeakReference<>(this));
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bar=findViewById(R.id.bar);
            bar2=findViewById(R.id.bar2);
            //启动线程来执行任务
            new Thread()
            {
                @Override
                public void run() {
                    while (status<100)
                    {
                        //获取操作的完成百分比
                        status=doWork();
                        //发送消息
                        myHander.sendEmptyMessage(0x11);
                    }
                }
            }.start();
        }
        //模拟一个耗时操作
        public int doWork()
        {
            //为数组元素赋值
            data[hasData++]=(int)(Math.random() * 100);
            try {
                Thread.sleep(100);
            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            return  hasData;
        }
    }
    

    my_bar.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        //定义轨道背景
        <item android:id="@+id/no"
            android:drawable="@color/colorPrimaryDark"/>
        <item android:id="@+id/ok"
            android:drawable="@drawable/ic_launcher_foreground"/>
        </layer-list>
    

    main_activity.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
          //定义一个大环形的进度条
          <ProgressBar
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              style="@android:style/Widget.ProgressBar.Large" />
          //定义一个中等大小的进度条
          <ProgressBar
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
          //定义一个小环进度条
          <ProgressBar
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              style="@android:style/Widget.ProgressBar.Small"/>
      </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="任务完成的进度"/>
        //定义一个水平进度条
        <ProgressBar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            style="@android:style/Widget.ProgressBar.Horizontal"/>
        //定义一个水平进度条改变外观
        <ProgressBar
            android:id="@+id/bar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progressDrawable="@drawable/my_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"/>
    </LinearLayout>
    

    效果图:


    2.gif

    相关文章

      网友评论

        本文标题:ProgressBar(进度条)的使用和方法

        本文链接:https://www.haomeiwen.com/subject/wgkrmctx.html