代码改变世界

[Android学习笔记]SeekBar的使用

2014-04-03 17:23  hellenism  阅读(491)  评论(0编辑  收藏  举报

一.SeekBar滑动条的使用

xml声明:

    <SeekBar android:id="@+id/seekbar"
        android:layout_width="200dip"
        android:layout_height="wrap_content"/>
xml声明

 

主要事件监听:OnSeekBarChangeListener

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        // 当Progress发生变化时触发
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        // 用户手势开始
    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
                // 用户手势结束
        
    }
OnSeekBarChangeListener

 

 

 

二.自定义Seekbar样式:
使用selector自定义seekbar样式:

进度条样式:seekbar_progress.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"
          android:drawable="@drawable/seekbar_background" />
    
        <!-- 进度度 -->
    <item android:id="@+android:id/progress" 
        android:drawable="@drawable/seekbar_foreground" />
</layer-list>
进度条样式:seekbar_progress.xml

 

按钮样式:seekbar_thumb.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 按下状态 -->
    <item android:drawable="@drawable/seekbar_thumb"
          android:state_pressed="true"/>

    <!-- 普通无焦点状态 -->
    <item android:drawable="@drawable/seekbar_thumb"
          android:state_focused="false"
          android:state_pressed="false"/>

</selector>
按钮样式:seekbar_thumb.xml

 

使用自定义样式,分别添加在android:progressDrawable和android:thumb两个属性上:

<SeekBar android:id="@+id/seekbar"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seekbar_progress" 
android:thumb="@drawable/seekbar_thumb" />
使用自定义样式

 

 

 

三.创建竖直方向的Seekbar:

原理:
扩展Seekbar,重写onDraw方法,把Canvas旋转90度

VerticalSeekbar:

public class VerticalSeekbar extends AbsSeekBar {
    private Drawable mThumb;
    private int height;
    private int width;

    public interface OnSeekBarChangeListener {
        void onProgressChanged(VerticalSeekbar VerticalSeekBar, int progress,
                boolean fromUser);

        void onStartTrackingTouch(VerticalSeekbar VerticalSeekBar);

        void onStopTrackingTouch(VerticalSeekbar VerticalSeekBar);
    }

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    public VerticalSeekbar(Context context) {
        this(context, null);
    }

    public VerticalSeekbar(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.seekBarStyle);
    }

    public VerticalSeekbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
    }

    void onStartTrackingTouch() {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStartTrackingTouch(this);
        }
    }

    void onStopTrackingTouch() {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStopTrackingTouch(this);
        }
    }

    void onProgressRefresh(float scale, boolean fromUser) {

        Drawable thumb = mThumb;
        if (thumb != null) {
            setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);
            invalidate();
        }
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onProgressChanged(this, getProgress(),
                    fromUser);
        }
    }

    private void setThumbPos(int w, Drawable thumb, float scale, int gap) {
    
        int available = w + getPaddingLeft() - getPaddingRight();
        int thumbWidth = thumb.getIntrinsicWidth();
        int thumbHeight = thumb.getIntrinsicHeight();
        available -= thumbWidth;
        // The extra space for the thumb to move on the track
        available += getThumbOffset() * 2;
        int thumbPos = (int) (scale * available);
        int topBound, bottomBound;
        if (gap == Integer.MIN_VALUE) {
            Rect oldBounds = thumb.getBounds();
            topBound = oldBounds.top;
            bottomBound = oldBounds.bottom;
        } else {
            topBound = gap;
            bottomBound = gap + thumbHeight;
        }
        thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);
    }

    protected void onDraw(Canvas c) {
        
        //这里控制的是seekbar旋转的方向,用隐去的这两行配合下边注释掉的trackTouchEvent,可以实现
        //初始位置在上边的seekbar,现在默认的是初始位置在最下边,及0在下边
        /*c.rotate(90);
        c.translate(0, -width);*/
        
        //更改方向
        c.rotate(-90);
        c.translate(-this.getHeight(), 0);
        
        super.onDraw(c);
    }

    private void trackTouchEvent(MotionEvent event)

    {

        final int height = getHeight();

        int y = (int)event.getY();                 

        float scale = 0;

        float progress = 0;

        if(y > height)

        {
            scale = 0.0f;
        }

        else if( y>0 )

        {

            scale = 1-(float)y / (float)height;       

        }

        else

        {

        scale= 1.0f;

        }

        final int max = getMax();

        progress = scale * max;                         

        setProgress(Math.round(progress));                  

        setThumbPos(getHeight(), mThumb, scale, 0) ;    

    }

    
    protected synchronized void onMeasure(int widthMeasureSpec,
            int heightMeasureSpec) {
        height = View.MeasureSpec.getSize(heightMeasureSpec);
        width = View.MeasureSpec.getSize(widthMeasureSpec);
        this.setMeasuredDimension(width, height);

    }

    @Override
    public void setThumb(Drawable thumb) {
        mThumb = thumb;
        super.setThumb(thumb);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldw, oldh);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            setPressed(true);
            onStartTrackingTouch();
            trackTouchEvent(event);
            break;

        case MotionEvent.ACTION_MOVE:
            trackTouchEvent(event);
            attemptClaimDrag();
            break;

        case MotionEvent.ACTION_UP:
            trackTouchEvent(event);
            onStopTrackingTouch();
            setPressed(false);
            break;

        case MotionEvent.ACTION_CANCEL:
            onStopTrackingTouch();
            setPressed(false);
            break;
        }
        return true;
    }

/*    private void trackTouchEvent(MotionEvent event) {
        final int Height = getHeight();
        final int available = Height - getPaddingBottom() - getPaddingTop();
        int Y = (int) event.getY();
        Log.d("6", "trackTouchEvent==>Y"+Y);
        float scale;
        float progress = 0;
        if (Y > Height - getPaddingBottom()) {
            scale = 1.0f;
        } else if (Y < getPaddingTop()) {
            scale = 0.0f;
        } else {
            scale = (float) (Y)
                    / (float) available;
        }

        final int max = getMax();
        progress = scale * max;

        setProgress((int) progress);
    }*/

    private void attemptClaimDrag() {
        if (getParent() != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            KeyEvent newEvent = null;
            switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_UP:
                newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_RIGHT);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_DOWN);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_UP);
                break;
            default:
                newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, event
                        .getKeyCode());
                break;
            }
            return newEvent.dispatch(this);
        }
        return false;
    }
}
VerticalSeekbar

 

使用VerticalSeekbar:

   <com.haidilao.views.VerticalSeekbar
        android:id="@+id/seekbar"
        android:layout_width="25dip"
        android:layout_height="130dip"
        android:paddingTop="1dip" 
        android:layout_marginBottom="12dip"
        android:progressDrawable="@drawable/verticalseekbar_progressdrawable"
        android:thumb="@drawable/verticalseekbar_thumb"
        android:thumbOffset="0dip"
        android:max="100" />
VerticalSeekbar