android学习---SeekBar和RatingBar

SeekBar 拖动条:拖动条和滚动条类似,当是拖动条可以拖动滑块改变进度

RatingBar 星级评分条:星级评分条与拖动条相似


SeekBar特有的xml属性

android:thumb    指定一个Arawable对象,作为之定义滑块

android学习---SeekBar和RatingBar


RatingBar特有的xml属性

android:isIndicator   是否允许用户改变(true为不允许修改)

android:numStars   共有多少个星级

android:rating   默认的星级

android:stepSize   每次至少改变多少个星级

android学习---SeekBar和RatingBar



为了知道改变的进度而做相应的操作,我们需要添加监听

SeekBar 的监听 OnSeekBarChangeListener

RatingBar的监听 OnRatingBarChangeListener


下面我们通过实例来熟悉它们

(1)编写布局文件   activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <ImageView 
        android:id="@+id/imgView1"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher"/>
    <SeekBar 
        android:id="@+id/seekBar"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:thumb="@drawable/ic_launcher"
        android:max="255"
        android:progress="255"/>
      <ImageView 
        android:id="@+id/imgView2"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/img01"/>
      <RatingBar 
          android:id="@+id/reatingBar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:max="255"
          android:progress="255"
          android:rating="5"
          android:stepSize="0.5"
          android:numStars="5"/>
    
</LinearLayout>

(2)编写 MainActivity.java

package com.example.bar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
	
	private ImageView imgViewSB = null; //定义ImageView
	private ImageView imgViewRB = null;
	private SeekBar seekBar = null;  //定义SeekBar
	private RatingBar ratingBar = null; //定义RatingBar

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//布局组件
		this.imgViewSB = (ImageView) findViewById(R.id.imgView1);
		this.imgViewRB = (ImageView) findViewById(R.id.imgView2);
		this.seekBar = (SeekBar) findViewById(R.id.seekBar);
		this.ratingBar = (RatingBar) findViewById(R.id.reatingBar);
		
		//seekBar设置监听,改变图片透明度
		this.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				Log.i("onStopTrackingTouch", "停止拖动触发的方法");
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				Log.i("onStartTrackingTouch", "开始拖动触发的方法");
				
			}
			
			
			/**
			 * seekBar: SeekBar对象
			 * progress:拖动条的进度
			 * fromUser:是否用户手动改变
			 */
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				imgViewSB.setAlpha(progress);  //设置图片的透明度
				Log.i("onProgressChanged", "拖动滑块位置发生改变时触发的方法");
				
			}
		});
		
		//ratingBar设置监听,改变图片透明度
		this.ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			
			/**
			 * ratingBar:RatingBar对象
			 * rating :星级的大小
			 * fromUser:是否用户手动改变
			 */
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				imgViewRB.setAlpha((int)(rating*255/5));
				
			}
		});
		
		
	}

	@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;
	}

}

允许之后,效果如下:

android学习---SeekBar和RatingBar

android学习---SeekBar和RatingBar,布布扣,bubuko.com

android学习---SeekBar和RatingBar

上一篇:漫长Appium之路(二)——Appium安装与使用总结


下一篇:U3D中IOS平台泛型方法尽少使用