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. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)2. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息3. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. CSS hack用法案例詳解6. asp中response.write("中文")或者js中文亂碼問(wèn)題7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明10. ASP實(shí)現(xiàn)加法驗(yàn)證碼
