Android关于三种进度条的使用
1.SeekBar --拖动条
2.RatingBar --星级评分条
3.ProgressBar ---普通进度条
关于这三个进度条,我做了个简单的实例来复习这几个进度条的用法,用法比较简单,但这三个进度条比较使用,在手机使用总很容易碰到。我想大家都懂的。
下面是一个实例:
项目名称:SeekBarAndRatingBarAndProgressBar
名字有点坑爹,只是为了明确表示
效果图:
说明:从Android2.2开始,就用match_parent来代替fill_parent,两个表示的意思是完全一样的
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="match_parent"
- android:layout_height="240px"
- android:src="@drawable/lijiang"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/seekBar"
- />"
- <!-- 定义一个拖动条 -->
- <SeekBar
- android:id="@+id/seekBar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="255"
- android:progress="255"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/ratingBar"/>
- <!-- 定义一个星级评分条 -->
- <RatingBar
- android:id="@+id/ratingBar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numStars="5"
- android:max="255"
- android:progress="255"
- android:stepSize="0.5"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/progressBar"
- />
- <ProgressBar
- android:id="@+id/progressBar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="255"
- style="@android:style/Widget.ProgressBar.Horizontal"
- />
- </LinearLayout>
- package org.wwj.bar;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import android.widget.RatingBar;
- import android.widget.RatingBar.OnRatingBarChangeListener;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- public class MainActivity extends Activity {
- ImageView image = null;
- private SeekBar seekBar = null;
- private RatingBar ratingBar = null;
- //该程序模拟填充长度为255的数组
- private int[] data = new int[255];
- int hasData = 0;
- //记录ProgressBar的完成进度
- int status = 0;
- ProgressBar progressBar = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- image = (ImageView) findViewById(R.id.image);
- seekBar = (SeekBar) findViewById(R.id.seekBar);
- ratingBar = (RatingBar) findViewById(R.id.ratingBar);
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- //为seekBar设置监听事件
- seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- System.out.println("SeekBar--->" + seekBar.getProgress());
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- System.out.println("SeekBar---->" + seekBar.getProgress());
- }
- //当拖动条的滑块位置发生改变时触发该方法
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
- //动态改变图片的透明度
- image.setAlpha(progress);
- }
- });
- ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
- @Override
- public void onRatingChanged(RatingBar ratingBar, float rating,
- boolean fromUser) {
- // TODO Auto-generated method stub
- //动态改变图片的透明度,其中255是星级评分条的最大值
- //5个星星就代表最大值255
- image.setAlpha((int) (rating * 255) / 5);
- }
- });
- //创建一个负责更新的进度的Handler
- final Handler mHandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- //表明消息是由该程序发送的
- if(msg.what == 0x111){
- progressBar.setProgress(status);
- }
- };
- };
- //启动线程来执行任务
- new Thread(){
- public void run(){
- while(status < 255){
- //获取耗时操作的完成百分比
- status = doWork();
- //发送消息到Handler
- Message m = new Message();
- m.what = 0x111;
- //发送消息
- mHandler.sendMessage(m);
- }
- }
- }.start();
- }
- //模拟一个耗时的操作
- public int doWork(){
- //为数组元素赋值
- data[hasData++] = (int) (Math.random() * 255);
- try{
- Thread.sleep(100);
- }
- catch (InterruptedException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- return hasData;
- }
- }