標(biāo)注:本文為個(gè)人整理味悄,僅做自己學(xué)習(xí)參考使用全度,請(qǐng)勿轉(zhuǎn)載和轉(zhuǎn)發(fā)
2018-06-13: 初稿。參考博主coder-pig
0. 引言
- Android的基本UI控件闻镶,拖動(dòng)條甚脉,SeekBar。主要用于音樂播放器或者視頻播放器铆农,音量控制或者播放進(jìn)度控制牺氨,都利用這個(gè)SeekBar狡耻。
- 官方的API:SeekBar
- SeekBar是ProgressBar的子類,擁有自己的屬性android:thumb
1. SeekBar基本用法
- 基本屬性
android:max="100" // 滑動(dòng)條的最大值
android:progress="60" // 滑動(dòng)條的當(dāng)前值
android:secondaryProgress="70" // 二級(jí)滑動(dòng)條的進(jìn)度
android:thumb="@mipmap/sb_icon" // 滑塊的drawable
- SeekBar的事件猴凹,SeekBar.OnSeekBarChangelistener 重寫三個(gè)對(duì)應(yīng)的方法
onProgressChange: 進(jìn)度發(fā)生改變時(shí)會(huì)觸發(fā)
onStartTrackingTouch: 按住SeekBar時(shí)會(huì)觸發(fā)
onStopTrackingTouch: 放開SeekBar時(shí)會(huì)觸發(fā)
簡(jiǎn)單的代碼示例
效果圖:
public class MainActivity extends AppCompatActivity {
private SeekBar sb_normal;
private TextView txt_cur;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
bindViews();
}
private void bindViews() {
sb_normal = (SeekBar) findViewById(R.id.sb_normal);
txt_cur = (TextView) findViewById(R.id.txt_cur);
sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txt_cur.setText("當(dāng)前進(jìn)度值:" + progress + " / 100 ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "觸碰SeekBar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "放開SeekBar", Toast.LENGTH_SHORT).show();
}
});
}
}
2. 簡(jiǎn)單SeekBar定制
代碼實(shí)例:
運(yùn)行效果圖:
代碼實(shí)現(xiàn):
- 1.滑塊狀態(tài)Drawable:sb_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/>
<item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/>
</selector>
貼下素材:
- 2.條形欄Bar的Drawable:sb_bar.xml
這里用到一個(gè)layer-list的drawable資源夷狰!其實(shí)就是層疊圖片,依次是:背景郊霎,二級(jí)進(jìn)度條沼头,當(dāng)前進(jìn)度:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#FFFFD042" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#FFFFFFFF" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#FF96E85D" />
</shape>
</clip>
</item>
</layer-list>
3.然后布局引入SeekBar后,設(shè)置下progressDrawable與thumb即可书劝!
<SeekBar
android:id="@+id/sb_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="5.0dp"
android:minHeight="5.0dp"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/sb_thumb"/>
后面的圖也做出來(lái)了进倍,但是沒有理解二級(jí)進(jìn)度條的具體使用規(guī)范,沒看見二級(jí)進(jìn)度條在哪里啊购对,sb_bar中背景和過(guò)程是用上了