zoukankan      html  css  js  c++  java
  • Android 使用ProgressBar实现进度条

    ProgressBar简介
    ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好型。

    课程目标
    (1)制定ProgressBar显示风格
    (2)ProgressBar的分类
    (3)标题上ProgressBar的设置
    (4)ProgressBar的关键属性
    (5)ProgressBar的关键方法
    (6)ProgressBar的基础使用
    (7)自定义ProgressBar样式

    制定ProgressBar显示风格
    style="?android:attr/progressBarStyleLarge" 大环形进度条
    style="?android:attr/progressBarStyleSmall" 小环形进度条
    style="?android:attr/progressBarStyleHorizontal" 水平进度条

    ProgressBar的分类
    (1)可以精确显示进度(可以显示进度或者百分比)
    (2)不可以精确显示进度(一直转啊转,类似于一个过场动画)
    可以精确显示任务的ProgressBar可以用于显示下载任务;对于不可以精确显示进度的ProgressBar的话他对应的任务的事件可能是不可控的,也就是说我可能不知道要过多少时间才能好。

    标题栏上的ProgressBar
    通过一个案例来显示如何在标题栏上显示ProgressBar
    通过启用窗口特征,启用带进度和不带进度的进度条
    启用窗口特征——启用带进度的进度条:
    requestWindowFeature(Window.FEATURE_PROGRESS);
    启用窗口特征——启用不带进度的进度条
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    启动好了之后还要把这两个进度条显示出来
    显示两种进度条:
    setProgressBarVisibility(true)
    setProgressBarIndeterminateVisibility(true);
    设置有进度的进度条的刻度(刻度的范围在0~10000)
    setProgress(3000);
    注意刻度不能设置成10000,因为这个时候progressBar的刻度已经到了100%,这个时候他会消失,因为进度已经完成了。
    (注意:
     requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     这两句话要放在
     setContentView(R.layout.activity_main);
     之前,不然程序运行会出错。

     
    第一显示进度(相当于在优酷上看电影的时候当前播放的进度)
    第二显示进度(相当于在优酷上看电影的时候缓冲对应的进度)
    最大显示进度(相当于一部电影的时长对应的长度)
    通过第一显示进度除以最大显示进度可以得到当前进度的百分比。
    ProgressBar的关键属性
    android:max = "100"  —— 最大显示进度
    android:progress = "50" —— 第一显示进度
    android:secondaryProgress="80" —— 第二显示进度
    android:indeterminate = "true" —— 设置是否精确显示
     true表示不精确显示进度;false表示精确显示进度

    ProgressBar的关键方法
    (1)setProgress(int) 设置第一进度
    (2)setSecondaryProgress(int) 设置第二进度
    (3)getProgress() 获取第一进度
    (4)getSecondaryProgress() 获取的二进度
    (5)incrementProgressBy(int) 增加或减少第一进度
    (6)incrementSecondaryProgressBy(int) 增加或减少第二进度
    (7)getMax() 获取最大进度

    ProgressBar设置一下ProgressBar的以下方法就创建好了ProgressBar。
    接下来是设置ProgressDialog,它是一个弹出的对话框(类似于之前学的DatePickerDialog和TimePickerDialog)
    新建ProgressDialog对象:
    progressDialog = new ProgressDialog(MainActivity.this);
    /* 设定页面显示风格的属性 */
    设置显示风格:
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    设置标题:
    prodialog.setTitle("ProgressDialog的标题");
    设置对话框里的文字信息:
    prodialog.setMessage("ProgressDialog里的文字信息");
    设置图标:
    prodialog.setIcon(R.drawable.ic_launcher)
    /* 设定关于ProgressDialog的一些属性 */
    设定最大进度:
    prodialog.setMax(100);
    设定初始化已经增长到的进度:
    prodialog.incrementProgressBy(50);
    进度条是明确显示进度的:
    prodialog.setIndeterminate(false);
    设定确定按钮:
    progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {   
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(MainActivity.this, "你点击了ProgressDialog的确定键", Toast.LENGTH_SHORT).show();
     }
    });
    设定是否可以取消:
    progressDialog.setCancelable(true);

    <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"
        >
    
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ProgressBar
            android:id="@+id/progressBar3"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ProgressBar
            android:id="@+id/progressBar4"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            />
    
        <Button
            android:id="@+id/button_add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="增加" />
    
        <Button
            android:id="@+id/button_reduce"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="减少" />
    
        <Button
            android:id="@+id/button_reset"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="重置" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    
        <Button
            android:id="@+id/button_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示ProgressDialog" />
    
    </LinearLayout>
    activity_main.xml
    package com.example.progressbar;
    
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends ActionBarActivity implements OnClickListener {
        
        private ProgressBar progressBar;
        private Button addButton;
        private Button reduceButton;
        private Button resetButton;
        private TextView textView;
        private Button showButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            requestWindowFeature(Window.FEATURE_PROGRESS);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            setContentView(R.layout.activity_main);
            setProgressBarVisibility(true);
            setProgressBarIndeterminateVisibility(false);
            setProgress(3000);
            
            progressBar = (ProgressBar) findViewById(R.id.progressBar4);
            progressBar.setSecondaryProgress(10);
            addButton = (Button) findViewById(R.id.button_add);
            reduceButton = (Button) findViewById(R.id.button_reduce);
            resetButton = (Button) findViewById(R.id.button_reset);
            textView = (TextView) findViewById(R.id.textView1);
            showButton = (Button) findViewById(R.id.button_show);
            
            int firstProgressNum = progressBar.getProgress();
            int secondProgressNum = progressBar.getSecondaryProgress();
            int maxProgressNum = progressBar.getMax();
            textView.setText("第一进度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%
    "
                            + "第二进度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%");
            
            addButton.setOnClickListener(this);
            reduceButton.setOnClickListener(this);
            resetButton.setOnClickListener(this);
            showButton.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
            case R.id.button_add:
                if (progressBar.getProgress() + 1 <= progressBar.getMax()) 
                    progressBar.incrementProgressBy(1);
                if (progressBar.getSecondaryProgress() + 1 <= progressBar.getMax())
                    progressBar.incrementSecondaryProgressBy(1);
                break;
            case R.id.button_reduce:
                if (progressBar.getProgress() - 1 >= 0)
                    progressBar.incrementProgressBy(-1);
                if (progressBar.getSecondaryProgress() - 1 >= 0)
                    progressBar.incrementSecondaryProgressBy(-1);
                break;
            case R.id.button_reset:
                progressBar.setProgress(0);
                progressBar.setSecondaryProgress(10);
                break;
            case R.id.button_show:
                ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("ProgressDialog的标题");
                progressDialog.setMessage("ProgressDialog的文字信息");
                progressDialog.setIcon(R.drawable.ic_launcher);
                progressDialog.setMax(100);
                progressDialog.setIndeterminate(false);
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你点击了ProgressDialog的确定键", Toast.LENGTH_SHORT).show();
                    }
                });
                progressDialog.setCancelable(true);
                progressDialog.show();
            }
            int firstProgressNum = progressBar.getProgress();
            int secondProgressNum = progressBar.getSecondaryProgress();
            int maxProgressNum = progressBar.getMax();
            textView.setText("第一进度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%
    "
                            + "第二进度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%");
        }
    }
    MainActivity.java

    自定义进度条样式
    其实横向显示进度条的真正的样式是:
    @android:style/Widget.ProgressBar.Horizontal
    这个样式里面的最重要的其实是其中的style标签的item标签的android:progressDrawable
    progressDrawable属性对应一个文件——这里是@android:drawable/progress_horizontal
    可以通过给ProgressBar添加一个android:progressDrawable属性来覆盖之前的属性来达到(比如:修改颜色)的效果。

    效果:

  • 相关阅读:
    /usr/bin/ld: 找不到 /usr/lib64/libatomic.so.1.0.0
    linux Version mismatch error. This is libtool 2.4.6 Debian-2.4.6-2
    java播放语音文件
    java冒泡排序
    java递归求和
    常见芯片标号说明
    keil5 新建 stm32项目 步骤
    st-link 升级固件时报错“is not in the DFU mode”
    数码管 段选位选
    C51 定时器
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5416539.html
Copyright © 2011-2022 走看看