Android中SeekBar拖動條使用方法詳解
本文實例為大家分享了Android中SeekBar拖動條使用方法的具體代碼,供大家參考,具體內容如下
SeekBar控件效果展示
拖動條SeekBar繼承了ProgressBar,因此ProgressBar所支持的xml屬性和方法完全適合SeekBar。只是進度條ProgressBar采用顏色填充來表明進度完成程度,拖動條SeekBar則通過滑塊的外置來標識——拖動滑塊允許進度值的改變。(例如:條件Android系統的音量)
如上圖,通過拖動SeekBar滑塊,實現圖片透明度的修改。實現代碼如下:
創建xml布局文件(activity_seek_bar.xml)<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.SeekBarActivity'> <ImageView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:src='http://www.lshqa.cn/bcjs/@drawable/pineapple' /> <!--android:thumb 自定義一個Drawable對象(設置滑塊的小圖標)--> <SeekBar android: android:layout_width='match_parent' android:layout_height='wrap_content' android:max='250' android:progress='150' android:thumb='@drawable/test' /></LinearLayout>
滑塊最大值為250,當前值為150。可通過拖動滑塊進行改變。android:thumb 為滑塊自定義一個Drawable對象(設置滑塊的小圖標),使滑塊更加好看。
創建Activity操作實現類:public class SeekBarActivity extends AppCompatActivity { private ImageView imageView;//圖片 private SeekBar seekBar;//拖動條 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seek_bar); imageView = (ImageView)findViewById(R.id.image); seekBar = (SeekBar)findViewById(R.id.seekbar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {、 //滑塊位置變動時觸發該方法 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { //設置圖片透明度 imageView.setImageAlpha(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }}
SeekBar滑塊位置變動時,ImageVIew的透明度將變為該拖動條SeekBar的當前值,將看到頂部圖片展示的效果。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: