码迷,mamicode.com
首页 > 其他好文 > 详细

竖着的Seekbar

时间:2015-05-05 18:27:30      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

 

视频播放器页面音量控制Seekbar实现竖直的效果。竖直只是将Seekbar转了90度或-90度,我们可以把画布转一个角度,然后交给系统去画,具体的做法就是重写ondraw()调整画布,然后调用super.onDraw()

 

向上的Seekbar

protected void onDraw(Canvas c) {
    c.rotate(-90);
    c.translate(-height,0);//height是你的verticalseekbar的高
    super.onDraw(c);
}

 

向下的seekbar则应该是:

protected void onDraw(Canvas c) {
    c.rotate(90);
    c.translate(0,-width);//width是你的verticalseekbar的宽
    super.onDraw(c);
}

 

示例代码

public class VerticalSeekBar extends SeekBar {
    public VerticalSeekBar(Context context) {
        super(context);
    }
    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

        protected void onDraw(Canvas c) {
            c.rotate(-90);
            c.translate(-getHeight(), 0);
            super.onDraw(c);
        }

        @Override
        public synchronized void setProgress(int progress) {
            super.setProgress(progress);
            onSizeChanged(getWidth(), getHeight(), 0, 0);  
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (!isEnabled()) {
                return false;
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_UP:
                    setProgress((int) ((int) getMax()- (getMax() * event.getY() / getHeight())));
                    break;
                default:
                    return super.onTouchEvent(event);
            }
            return true;
        }
    }  

 

竖着的Seekbar

标签:

原文地址:http://www.cnblogs.com/huangzx/p/4479640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!