Android进度条(星级评分)使用详解(二)

来源:互联网 发布:js延迟调用函数函数 编辑:程序博客网 时间:2024/06/09 17:02
一、SeekBar拖动条使用
    SeekBar继承于ProgressBar,ProgressBar所支持的XML属性和方法完全适用于SeekBar。拖动条和进度条的区别是:进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块位置来标识数值并且运行用户拖动滑块来改变值。因此,拖动条通常用于对系统的某种数值进行调节,比如调节音量、图片的透明度等。
1.拖动条效果

2.代码实现
功能:通过拖动滑块该动态改变图片的透明度
public class MainActivity extends ActionBarActivity { private ImageView image = null; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  image = (ImageView)findViewById(R.id.image);  SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);  seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   //当拖动条发生改变时触发该方法   public void onProgressChanged(SeekBar seekBar, int progress,     boolean fromUser) { //Notification that the progress level has changed    image.setImageAlpha(progress);//动态改变图片的透明度   }   public void onStopTrackingTouch(SeekBar seekBar) {//Notification that the user has finished a touch gesture        }   public void onStartTrackingTouch(SeekBar seekBar) {//Notification that the user has started a touch gesture         }      }); }}
其中,界面布局文件main.xml为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/image"        android:layout_width="fill_parent"        android:layout_height="250dp"        android:src="@drawable/hehe"/>    <!--定义一个拖动条,并改变它的滑块外观  --> <SeekBar     android:id="@+id/seekBar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:max="255"                    //设置拖动条的最大值     android:progress="255"             //设置拖动条当前默认值     android:thumb="@drawable/android" /> </LinearLayout>
注:SeekBar允许用户改变拖动条的滑块外观,通过android:thumb属性指定一个Drawable对象实现。
二、RatingBar星际评分条使用
    星际评分条与拖动条都继承于AbsSeekBar,它们都允许用户通过拖动来改变进度。RatingBar与SeekBar最大的区别是:RatingBar通过星星来表示。RatingBar支持的常见XML属性如下:
    android:isIndicator:设置该星级评分条是否允许用户改变(true为不允许修改)
    android:numStarts:设置该星级评分总共有多少个星级
    android:rating:设置该星级评分条默认的星级
    android:stepSize:设置每次需要改变多少个星级
1.星级评分效果

2.代码实现
功能:通过星级评分改变图片的透明度 
protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  image = (ImageView) findViewById(R.id.image);  RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);  ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {   public void onRatingChanged(RatingBar ratingBar, float rating,     boolean fromUser) {    image.setImageAlpha((int)(rating*255)/5);// 动态改变图片的透明度   }  });
其中,RatingBar定义(main.xml)为:
<RatingBar      android:id="@+id/ratingBar"      android:layout_width="wrap_content"      android:layout_height="wrap_content"     android:max="255"     android:numStars="5"     android:stepSize="0.5"     android:progress="255"/>


0 0