随笔 - 91,  文章 - 0,  评论 - 1,  阅读 - 87621

 

视频播放器页面音量控制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;
        }
    }  
复制代码

 

posted on   道无涯  阅读(1223)  评论(0编辑  收藏  举报
(评论功能已被禁用)

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示