android垂直seekbar

来源:互联网 发布:webpack 压缩混淆js 编辑:程序博客网 时间:2024/04/25 20:38

这两头要用到垂直seekbar,但是android自带的没有垂直的,到网上找了一个开源的代码,发现里面的滑动块不能随进度条一起变动bug。后来又在这个开源项目里找到原作者做的修改。才实现了滑动块随进度条一起移动。

调用方法有变动,需要用seekbar.setProgressAndThumb(10),而不是seekbar.setProgress(10);

代码如下:

import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.SeekBar;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);}@Overrideprotected 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);}/** * 滑块与进度一起变动(一定要调用这个,不要调用系统setprogeress) * @param progress */public synchronized void setProgressAndThumb(int progress) {setProgress(getMax() - (getMax() - progress));onSizeChanged(getWidth(), getHeight(), 0, 0);}@Overridepublic boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:case MotionEvent.ACTION_UP:int i = 0;i = getMax() - (int) (getMax() * event.getY() / getHeight());setProgressAndThumb(i);Log.i("Progress", getProgress() + "");onSizeChanged(getWidth(), getHeight(), 0, 0);break;case MotionEvent.ACTION_CANCEL:break;}return false;//这里要返回false,如果返回true,就无法监听开始和停止触摸事件}}

demo下载地址:http://download.csdn.net/detail/junfeng120125/5817815