Android中SeekBar拖動(dòng)條使用方法詳解
本文實(shí)例為大家分享了Android中SeekBar拖動(dòng)條使用方法的具體代碼,供大家參考,具體內(nèi)容如下
SeekBar控件效果展示
拖動(dòng)條SeekBar繼承了ProgressBar,因此ProgressBar所支持的xml屬性和方法完全適合SeekBar。只是進(jìn)度條ProgressBar采用顏色填充來(lái)表明進(jìn)度完成程度,拖動(dòng)條SeekBar則通過(guò)滑塊的外置來(lái)標(biāo)識(shí)——拖動(dòng)滑塊允許進(jìn)度值的改變。(例如:條件Android系統(tǒng)的音量)
如上圖,通過(guò)拖動(dòng)SeekBar滑塊,實(shí)現(xiàn)圖片透明度的修改。實(shí)現(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: android:layout_width='match_parent' android:layout_height='wrap_content' android:src='http://www.gepszalag.com/bcjs/@drawable/pineapple' /> <!--android:thumb 自定義一個(gè)Drawable對(duì)象(設(shè)置滑塊的小圖標(biāo))--> <SeekBar android: 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。可通過(guò)拖動(dòng)滑塊進(jìn)行改變。android:thumb 為滑塊自定義一個(gè)Drawable對(duì)象(設(shè)置滑塊的小圖標(biāo)),使滑塊更加好看。
創(chuàng)建Activity操作實(shí)現(xiàn)類:public class SeekBarActivity extends AppCompatActivity { private ImageView imageView;//圖片 private SeekBar seekBar;//拖動(dòng)條 @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() {、 //滑塊位置變動(dòng)時(shí)觸發(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滑塊位置變動(dòng)時(shí),ImageVIew的透明度將變?yōu)樵撏蟿?dòng)條SeekBar的當(dāng)前值,將看到頂部圖片展示的效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC獲取多級(jí)類別組合下的產(chǎn)品2. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車3. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)4. Docker 容器健康檢查機(jī)制5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效6. python使用openpyxl庫(kù)讀寫Excel表格的方法(增刪改查操作)7. python中asyncio異步編程學(xué)習(xí)8. python os.listdir()亂碼解決方案9. Python使用socket_TCP實(shí)現(xiàn)小文件下載功能10. ASP實(shí)現(xiàn)文件上傳的方法
