垂直拖动条的实现

  1. public class VerticalSeekBar extends AbsSeekBar {  
  2.   
  3.     private Drawable mThumb;  
  4.   
  5.     public interface OnSeekBarChangeListener {  
  6.         void onProgressChanged(VerticalSeekBar VerticalSeekBar, int progress, boolean fromUser);  
  7.   
  8.         void onStartTrackingTouch(VerticalSeekBar VerticalSeekBar);  
  9.   
  10.         void onStopTrackingTouch(VerticalSeekBar VerticalSeekBar);  
  11.     }  
  12.   
  13.     private OnSeekBarChangeListener mOnSeekBarChangeListener;  
  14.   
  15.     public VerticalSeekBar(Context context) {  
  16.         this(context, null);  
  17.     }  
  18.   
  19.     public VerticalSeekBar(Context context, AttributeSet attrs) {  
  20.         this(context, attrs, android.R.attr.seekBarStyle);  
  21.     }  
  22.   
  23.     public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {  
  24.         super(context, attrs, defStyle);  
  25.     }  
  26.   
  27.     public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {  
  28.         mOnSeekBarChangeListener = l;  
  29.     }  
  30.   
  31.     void onStartTrackingTouch() {  
  32.         if (mOnSeekBarChangeListener != null) {  
  33.             mOnSeekBarChangeListener.onStartTrackingTouch(this);  
  34.         }  
  35.     }  
  36.   
  37.     void onStopTrackingTouch() {  
  38.         if (mOnSeekBarChangeListener != null) {  
  39.             mOnSeekBarChangeListener.onStopTrackingTouch(this);  
  40.         }  
  41.     }  
  42.   
  43.     void onProgressRefresh(float scale, boolean fromUser) {  
  44.         Drawable thumb = mThumb;  
  45.         if (thumb != null) {  
  46.             setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);  
  47.             invalidate();  
  48.         }  
  49.         if (mOnSeekBarChangeListener != null) {  
  50.             mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);  
  51.         }  
  52.     }  
  53.   
  54.     private void setThumbPos(int w, Drawable thumb, float scale, int gap) {  
  55.         int available = w - getPaddingLeft() - getPaddingRight();  
  56.         int thumbWidth = thumb.getIntrinsicWidth();  
  57.         int thumbHeight = thumb.getIntrinsicHeight();  
  58.         available -= thumbWidth;  
  59.           
  60.         // The extra space for the thumb to move on the track  
  61.         available += getThumbOffset() * 2;  
  62.           
  63.         int thumbPos = (int) (scale * available);  
  64.           
  65.         int topBound, bottomBound;  
  66.         if (gap == Integer.MIN_VALUE) {  
  67.             Rect oldBounds = thumb.getBounds();  
  68.             topBound = oldBounds.top;  
  69.             bottomBound = oldBounds.bottom;  
  70.         } else {  
  71.             topBound = gap;  
  72.             bottomBound = gap + thumbHeight;  
  73.         }  
  74.         thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);  
  75.     }  
  76.   
  77.     @Override  
  78.     protected void onDraw(Canvas c) {  
  79.         c.rotate(-90);//反转90度,将水平SeekBar竖起来    
  80.         c.translate(-getHeight(), 0);//将经过旋转后得到的VerticalSeekBar移到正确的位置,注意经旋转后宽高值互换   
  81.         super.onDraw(c);  
  82.     }  
  83.   
  84.     @Override  
  85.     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  86.         super.onMeasure(heightMeasureSpec, widthMeasureSpec);  
  87.         setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());//宽高值互换  
  88.     }  
  89.   
  90.     @Override  
  91.     public void setThumb(Drawable thumb) {  
  92.         mThumb = thumb;  
  93.         super.setThumb(thumb);  
  94.     }  
  95.   
  96.     @Override  
  97.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  98.         super.onSizeChanged(h, w, oldw, oldh);//宽高值互换  
  99.     }  
  100.   
  101.     //与源码完全相同,仅为调用宽高值互换处理的onStartTrackingTouch()方法  
  102.     @Override  
  103.     public boolean onTouchEvent(MotionEvent event) {  
  104.         if (!isEnabled()) {  
  105.             return false;  
  106.         }  
  107.         switch (event.getAction()) {  
  108.             case MotionEvent.ACTION_DOWN:  
  109.                 setPressed(true);  
  110.                 onStartTrackingTouch();  
  111.                 trackTouchEvent(event);  
  112.                 break;  
  113.   
  114.             case MotionEvent.ACTION_MOVE:  
  115.                 trackTouchEvent(event);  
  116.                 attemptClaimDrag();  
  117.                 break;  
  118.   
  119.             case MotionEvent.ACTION_UP:  
  120.                 trackTouchEvent(event);  
  121.                 onStopTrackingTouch();  
  122.                 setPressed(false);  
  123.                 // ProgressBar doesn't know to repaint the thumb drawable  
  124.                 // in its inactive state when the touch stops (because the  
  125.                 // value has not apparently changed)  
  126.                 invalidate();  
  127.                 break;  
  128.   
  129.             case MotionEvent.ACTION_CANCEL:  
  130.                 onStopTrackingTouch();  
  131.                 setPressed(false);  
  132.                 invalidate(); // see above explanation  
  133.                 break;  
  134.         }  
  135.         return true;  
  136.     }  
  137.   
  138.     //宽高值互换处理  
  139.     private void trackTouchEvent(MotionEvent event) {  
  140.         final int height = getHeight();  
  141.         final int available = height - getPaddingBottom() - getPaddingTop();  
  142.         int Y = (int) event.getY();  
  143.         float scale;  
  144.         float progress = 0;  
  145.         if (Y > height - getPaddingBottom()) {  
  146.             scale = 0.0f;  
  147.         } else if (Y < getPaddingTop()) {  
  148.             scale = 1.0f;  
  149.         } else {  
  150.             scale = (float) (height - getPaddingBottom() - Y) / (float) available;  
  151.         }  
  152.         final int max = getMax();  
  153.         progress = scale * max;  
  154.         setProgress((int) progress);  
  155.     }  
  156.   
  157.     private void attemptClaimDrag() {  
  158.         if (getParent() != null) {  
  159.             getParent().requestDisallowInterceptTouchEvent(true);  
  160.         }  
  161.     }  
  162. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值