SeekBar

seekbar,拖动条类似进度条progressbar,不同的是用户可以控制

01)使用一个seekbar,先在xml文件中定义,如下:

<SeekBar android:id="@+id/main_seekbar" 
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"  
    android:layout_weight="1"
    android:progressDrawable="@drawable/main_bg_bar"  
    android:indeterminateDrawable="@drawable/main_bg_bar"              
    android:thumb="@drawable/thumb_bar"
    android:background="@null"
    />

其中,@drawable/main_bg_bar的代码如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
        <item android:id="@*android:id/background" android:drawable="@drawable/volumn_bg" />  
        <item android:id="@*android:id/secondaryProgress">  
            <scale android:drawable="@drawable/volumn_front" android:scaleWidth="100%" />  
        </item>  
        <item android:id="@*android:id/progress">  
            <scale android:drawable="@drawable/volumn_primary" android:scaleWidth="100%" />  
        </item>  
    </layer-list>  

02)在java代码中实例化,并对其进行简单操作

import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

final SeekBar mSeekBar = (SeekBar)findViewById(R.id.main_seekbar) ;
mSeekBar.setMax(1000); //设置最大进度
mSeekBar.setProgress(1); //设置当前进度 
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        //手动拖动进度条时,可获得当前进度
        long mPosOverride = mSeekBar.getProgress();

    }
});

04)补充说明,thumb_bar可使用多个图片,达到L的效果,一个很大的拖动条
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>

<animated-selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
    <item android:state_enabled="false" android:state_pressed="true">
        <bitmap android:src="@drawable/scrubber_control_off_mtrl_alpha" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" android:alpha="@dimen/my_disabledAlpha" />
    </item>
    <item android:state_enabled="false">
        <bitmap android:src="@drawable/scrubber_control_off_mtrl_alpha" android:gravity="center" android:tint="@color/my_colorControlNormal_thumb" android:alpha="@dimen/my_disabledAlpha" />
    </item>
    <item android:state_pressed="true" android:id="@+id/pressed">
        <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_005" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
    </item>
    <item android:id="@+id/not_pressed">
        <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_000" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
    </item>
    <transition android:fromId="@+id/not_pressed" android:toId="@+id/pressed">
        <animation-list>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_000" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_001" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_002" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_003" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_004" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_to_pressed_mtrl_005" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
        </animation-list>
    </transition>
    <transition android:fromId="@+id/pressed" android:toId="@+id/not_pressed">
        <animation-list>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_000" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_001" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_002" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_003" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_004" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
            <item android:duration="15">
                <bitmap android:src="@drawable/scrubber_control_from_pressed_mtrl_005" android:gravity="center" android:tint="@color/my_colorControlActivated_thumb" />
            </item>
        </animation-list>
    </transition>
</animated-selector>

使用到的图片如下:
这里写图片描述这里写图片描述]  (http://img.blog.csdn.net/20160317095750999)![这里写图片描述这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述

你可能感兴趣的:(SeekBar)