Android中SeekBar拖動條使用方法詳解
本文實例為大家分享了Android中SeekBar拖動條使用方法的具體代碼,供大家參考,具體內(nèi)容如下
SeekBar控件效果展示

拖動條SeekBar繼承了ProgressBar,因此ProgressBar所支持的xml屬性和方法完全適合SeekBar。只是進(jìn)度條ProgressBar采用顏色填充來表明進(jìn)度完成程度,拖動條SeekBar則通過滑塊的外置來標(biāo)識——拖動滑塊允許進(jìn)度值的改變。(例如:條件Android系統(tǒng)的音量)
如上圖,通過拖動SeekBar滑塊,實現(xiàn)圖片透明度的修改。實現(xiàn)代碼如下:
創(chuàng)建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:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/pineapple" /> <!--android:thumb 自定義一個Drawable對象(設(shè)置滑塊的小圖標(biāo))--> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="250" android:progress="150" android:thumb="@drawable/test" /> </LinearLayout>
滑塊最大值為250,當(dāng)前值為150??赏ㄟ^拖動滑塊進(jìn)行改變。android:thumb 為滑塊自定義一個Drawable對象(設(shè)置滑塊的小圖標(biāo)),使滑塊更加好看。
創(chuàng)建Activity操作實現(xiàn)類:
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() {、
//滑塊位置變動時觸發(fā)該方法
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
//設(shè)置圖片透明度
imageView.setImageAlpha(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
SeekBar滑塊位置變動時,ImageVIew的透明度將變?yōu)樵撏蟿訔lSeekBar的當(dāng)前值,將看到頂部圖片展示的效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計(1)
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android 自定義imageview實現(xiàn)圖片縮放實例詳解
這篇文章主要介紹了Android 自定義imageview實現(xiàn)圖片縮放實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android創(chuàng)建與解析XML(三)——詳解Sax方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(三)——詳解Sax方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務(wù)器上傳照片
這篇文章主要介紹了Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務(wù)器上傳照片,需要的朋友可以參考下2020-05-05

