拖动条SeekBar及星级评分条
1.布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".AndroidSeekBarActivity" > 7 8 <ImageView 9 android:id="@+id/img" 10 android:layout_width="match_parent" 11 android:layout_height="240px" 12 android:src="@drawable/mm" /> 13 14 <SeekBar 15 android:id="@+id/sekbar" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:max="255" 19 android:progress="255" 20 android:thumb="@drawable/sj" /> 21 22 <RatingBar 23 android:id="@+id/ratbar" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:max="255" 27 android:numStars="5" 28 android:progress="255" 29 android:stepSize="0.5" /> 30 31 </LinearLayout>
2.逻辑控制
1 package com.example.androidseekbar; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.widget.ImageView; 7 import android.widget.RatingBar; 8 import android.widget.RatingBar.OnRatingBarChangeListener; 9 import android.widget.SeekBar; 10 import android.widget.SeekBar.OnSeekBarChangeListener; 11 12 public class AndroidSeekBarActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_android_seek_bar); 18 19 final ImageView img = (ImageView) this.findViewById(R.id.img); 20 SeekBar sekbar = (SeekBar) this.findViewById(R.id.sekbar); 21 sekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 22 23 @Override 24 public void onStopTrackingTouch(SeekBar seekBar) { 25 // TODO Auto-generated method stub 26 27 } 28 29 @Override 30 public void onStartTrackingTouch(SeekBar seekBar) { 31 // TODO Auto-generated method stub 32 33 } 34 35 @Override 36 public void onProgressChanged(SeekBar seekBar, int progress, 37 boolean fromUser) { 38 // 拖动时触发 39 img.setAlpha(progress); 40 } 41 }); 42 43 RatingBar ratbar=(RatingBar)this.findViewById(R.id.ratbar); 44 ratbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 45 46 @Override 47 public void onRatingChanged(RatingBar arg0, float rating, boolean arg2) { 48 //动态改变图片透明度 49 img.setAlpha((int)(rating*255/5)); 50 } 51 }); 52 } 53 54 @Override 55 public boolean onCreateOptionsMenu(Menu menu) { 56 // Inflate the menu; this adds items to the action bar if it is present. 57 getMenuInflater().inflate(R.menu.activity_android_seek_bar, menu); 58 return true; 59 } 60 61 }