Android关于三种进度条的使用

 

Android关于三种进度条的使用

分类: 【Android开发学习之路】   862人阅读  评论(0)  收藏  举报

Android关于三种进度条的使用

1.SeekBar --拖动条

2.RatingBar --星级评分条

3.ProgressBar ---普通进度条

 

关于这三个进度条,我做了个简单的实例来复习这几个进度条的用法,用法比较简单,但这三个进度条比较使用,在手机使用总很容易碰到。我想大家都懂的。

 

下面是一个实例:

项目名称:SeekBarAndRatingBarAndProgressBar

名字有点坑爹,只是为了明确表示

 

效果图:

                      

 

说明:从Android2.2开始,就用match_parent来代替fill_parent,两个表示的意思是完全一样的

 

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.     <ImageView   
  8.         android:id="@+id/image"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="240px"  
  11.         android:src="@drawable/lijiang"  
  12.         />  
  13.     <TextView   
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/seekBar"  
  17.         />"  
  18.     <!-- 定义一个拖动条 -->  
  19.     <SeekBar  
  20.         android:id="@+id/seekBar"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:max="255"  
  24.         android:progress="255"  
  25.         />  
  26.     <TextView   
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="@string/ratingBar"/>  
  30.     <!-- 定义一个星级评分条 -->  
  31.     <RatingBar  
  32.         android:id="@+id/ratingBar"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:numStars="5"  
  36.         android:max="255"  
  37.         android:progress="255"  
  38.         android:stepSize="0.5"  
  39.         />  
  40.     <TextView  
  41.         android:layout_width="match_parent"  
  42.         android:layout_height="wrap_content"  
  43.         android:text="@string/progressBar"  
  44.         />  
  45.     <ProgressBar   
  46.         android:id="@+id/progressBar"  
  47.         android:layout_width="match_parent"  
  48.         android:layout_height="wrap_content"  
  49.         android:max="255"  
  50.         style="@android:style/Widget.ProgressBar.Horizontal"  
  51.         />  
  52. </LinearLayout>  


 

[java]  view plain copy
  1. package org.wwj.bar;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.widget.ImageView;  
  8. import android.widget.ProgressBar;  
  9. import android.widget.RatingBar;  
  10. import android.widget.RatingBar.OnRatingBarChangeListener;  
  11. import android.widget.SeekBar;  
  12. import android.widget.SeekBar.OnSeekBarChangeListener;  
  13.   
  14. public class MainActivity extends Activity {  
  15.       
  16.       
  17.     ImageView image = null;  
  18.     private SeekBar seekBar = null;  
  19.     private RatingBar ratingBar = null;  
  20.     //该程序模拟填充长度为255的数组  
  21.     private int[] data = new int[255];  
  22.     int hasData = 0;  
  23.     //记录ProgressBar的完成进度  
  24.     int status = 0;  
  25.     ProgressBar progressBar = null;  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         image = (ImageView) findViewById(R.id.image);  
  31.         seekBar = (SeekBar) findViewById(R.id.seekBar);  
  32.         ratingBar = (RatingBar) findViewById(R.id.ratingBar);  
  33.         progressBar = (ProgressBar) findViewById(R.id.progressBar);  
  34.         //为seekBar设置监听事件  
  35.         seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  36.               
  37.             @Override  
  38.             public void onStopTrackingTouch(SeekBar seekBar) {  
  39.                 // TODO Auto-generated method stub  
  40.                 System.out.println("SeekBar--->" + seekBar.getProgress());  
  41.             }  
  42.               
  43.             @Override  
  44.             public void onStartTrackingTouch(SeekBar seekBar) {  
  45.                 // TODO Auto-generated method stub  
  46.                 System.out.println("SeekBar---->" + seekBar.getProgress());  
  47.             }  
  48.             //当拖动条的滑块位置发生改变时触发该方法  
  49.             @Override  
  50.             public void onProgressChanged(SeekBar seekBar, int progress,  
  51.                     boolean fromUser) {  
  52.                 // TODO Auto-generated method stub  
  53.                 //动态改变图片的透明度  
  54.                 image.setAlpha(progress);  
  55.             }  
  56.         });  
  57.         ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {  
  58.               
  59.             @Override  
  60.             public void onRatingChanged(RatingBar ratingBar, float rating,  
  61.                     boolean fromUser) {  
  62.                 // TODO Auto-generated method stub  
  63.                 //动态改变图片的透明度,其中255是星级评分条的最大值  
  64.                 //5个星星就代表最大值255  
  65.                 image.setAlpha((int) (rating * 255) / 5);  
  66.             }  
  67.         });  
  68.     //创建一个负责更新的进度的Handler  
  69.     final Handler mHandler = new Handler(){  
  70.         public void handleMessage(android.os.Message msg) {  
  71.             //表明消息是由该程序发送的  
  72.             if(msg.what == 0x111){  
  73.                 progressBar.setProgress(status);  
  74.             }  
  75.         };  
  76.     };  
  77.       
  78.     //启动线程来执行任务  
  79.     new Thread(){  
  80.         public void run(){  
  81.             while(status < 255){  
  82.                 //获取耗时操作的完成百分比  
  83.                 status = doWork();  
  84.                 //发送消息到Handler  
  85.                 Message m = new Message();  
  86.                 m.what = 0x111;  
  87.                 //发送消息  
  88.                 mHandler.sendMessage(m);  
  89.             }  
  90.         }  
  91.     }.start();  
  92.     }  
  93.     //模拟一个耗时的操作  
  94.     public int doWork(){  
  95.         //为数组元素赋值  
  96.         data[hasData++] = (int) (Math.random() * 255);  
  97.         try{  
  98.             Thread.sleep(100);  
  99.         }  
  100.         catch (InterruptedException e) {  
  101.             // TODO: handle exception  
  102.             e.printStackTrace();  
  103.         }  
  104.         return hasData;  
  105.     }  
  106. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值