自定義SeekBar(簡單點的)

1.SeekBar的基本屬性

android:max="100" //滑動條的最大值
android:progress="60" //滑動條的當前值
android:secondaryProgress="70" //二級滑動條的進度
android:thumb = "@mipmap/sb_icon" //滑塊的drawable

2.SeekBar的監聽事件

setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt_cur.setText("當前進度值:" + 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();
                //發開進度條時會觸發
            }
        });

3.SeekBar的簡單自定義
效果圖:
這裏寫圖片描述
1)滑塊的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

<?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後,設置下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"/>

參考文章:http://www.runoob.com/w3cnote/android-tutorial-seekbar.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章