SeekBar的使用很简单,和其他控件一样,先在布局文件中声明,然后在Activity中使用。
下面演示下使用SeekBar改变图片的透明度:
1.java文件
1 package gdp.seekbartest; 2 3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.widget.ImageView; 8 import android.widget.SeekBar; 9 import android.widget.SeekBar.OnSeekBarChangeListener; 10 11 public class MianActivity extends Activity { 12 private ImageView imageView ; 13 private SeekBar seekBar ; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.mian); 18 19 imageView = (ImageView)findViewById(R.id.imageView); 20 seekBar = (SeekBar)findViewById(R.id.seekBar); 21 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 22 23 @Override 24 public void onStopTrackingTouch(SeekBar arg0) { 25 // TODO Auto-generated method stub 26 27 } 28 29 @Override 30 public void onStartTrackingTouch(SeekBar arg0) { 31 // TODO Auto-generated method stub 32 33 } 34 35 @SuppressLint("NewApi") 36 @Override 37 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 38 // TODO Auto-generated method stub 39 imageView.setImageAlpha(arg1); 40 } 41 }); 42 } 43 44 @Override 45 public boolean onCreateOptionsMenu(Menu menu) { 46 // Inflate the menu; this adds items to the action bar if it is present. 47 getMenuInflater().inflate(R.menu.mian, menu); 48 return true; 49 } 50 51 }
2.xml文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 6 <ImageView 7 android:id="@+id/imageView" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:src="@drawable/home" /> 11 12 <!-- 定义seekbar,并改变滑块外观 --> 13 14 <SeekBar 15 android:id="@+id/seekBar" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:max="255" 19 android:progress="255" 20 /> 21 </LinearLayout>