我们知道在所有的界面UI中进度条无疑是非常重要的一个,因为它可以给用户一个较为清晰的视觉效果:就是用户的操作的完成情况.这不是简单的完成与未完成,而是以一个进度的方式展示给用户的交互性更强了。

对于Android系统中的进度条如何使用呢?下一是简单的实现,并未做相关的美化处理。

依照惯例,先上效果图:

第一张:

第二张:

其中两个原型的进度条并未做任何的处理,水平进度条利用线程使之不停地增加减少。

具体实现首先看布局文件:

<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">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="简单进度条的展示"
    android:layout_gravity="center_horizontal"/>

  <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    />

  <ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal"/>

  <ProgressBar
    android:id="@+id/progressBar3"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:max="100"
    android:minWidth="180dip"
    android:minHeight="40dip"
    />

</LinearLayout>

之后是MainActivity

package com.kiritor.ui_progressbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements Runnable {

  private ProgressBar bar = null;
  private Thread thread = null;// 声明一个线程
  private boolean stateChange;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bar = (ProgressBar) findViewById(R.id.progressBar3);
    thread = new Thread(this);
    thread.start();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public void run() {
    while (true) {
      int current = bar.getProgress();// 得到当前进度值
      int currentMax = bar.getMax();// 得到进度条的最大进度值
      //int secCurrent = bar.getSecondaryProgress();// 得到底层当前进度值
      // 以下代码实现进度值越来越大,越来越小的一个动态效果
      if (stateChange == false) {
        if (current >= currentMax) {
          stateChange = true;
        } else {
          // 设置进度值
          bar.setProgress(current + 1);
          // 设置底层进度值
          bar.setSecondaryProgress(current + 1);
        }
      } else {
        if (current <= 0) {
          stateChange = false;
        } else {
          bar.setProgress(current - 1);
          bar.setSecondaryProgress(current - 1);
        }
      }
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  }
}

以上就是进度条的简单的用法,之后笔者会实现一些“特别”进度条,漂亮的,另类的!代码较为简单就不给源码了Over!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

02-04 09:59
查看更多