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

垂直的SeekBar:VerticalSeekBar的更多相关文章

  1. 垂直的seekbar

    看下效果: 1 package org.qianyukun.core.views; import android.content.Context; import android.graphics.Ca ...

  2. android 垂直 SeekBar 源代码(VerticalSeekBar)[转]

    主要是继承 AbsSeekBar 然后修改下面这些方法 onProgressRefresh() //当进度条数据更新的时候,例如我们拖动滑动条的时候,这个方法被调用 setThumbPos() //这 ...

  3. 自定义垂直拖动的seekbar进度条

    系统自定义的seekbar为横向拖动的样式,需要纵向的时则需要自己定义,网上很多说了重写系统SeekBar中onDraw()的方法,但是我使用的时候不知道为什么拖动条和点偏离了,不在一条直线上,好气. ...

  4. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  5. Android 自定义seekbar中,thumb被覆盖掉一部分问题

    (图一)  (图二)    (图三) 做一个自定义的seekbar,更改其背景图片: <com.android.Progress android:id="@+id/focus_seek ...

  6. Android SeekBar 和 draw9patch 的使用

    今天要使用一个SeekBar控件,其实我觉得Android默认样式已经很不错了,无奈设计不同意,而且SeekBar左右两边也有图片,默认样式和图片也确实不协调,因此这里使用图片自定义SeekBar样式 ...

  7. 自定义seekbar中,thumb被覆盖掉一部分问题

  8. [Android学习笔记]SeekBar的使用

    一.SeekBar滑动条的使用 xml声明: <SeekBar android:id="@+id/seekbar" android:layout_width="20 ...

  9. android 打造不同的Seekbar

    最近项目需要用到双向的seekbar,网上找了好多野不能达到要求,偶然一次机会看到了大众点评的例子,然后我最他做了优化,并对常用的seekbar做了总结. 向上两张图: 比如双向seekbar pub ...

随机推荐

  1. 导出Excel之Epplus使用教程3(图表设置)

    导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...

  2. ECMAScript6-下一代Javascript标准

    介绍 ECMAScript6是下一代Javascript标准,这个标准将在2015年6月得到批准.ES6是Javascript的一个重大的更新,并且是自2009年发布ES5以来的第一次更新. 它将会在 ...

  3. NPOI在.net中的操作Excel

    1.读取 using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode.Open, FileAccess.R ...

  4. Fortran向C传递NULL值

    在很多C或C++的头文件定义中,NULL被指定定义为0,这里不再具体展开 gfortran的手册关于iso c binding的章节,定义NULL如下 Moreover, the following ...

  5. Xcode 4.6.3 Bug - .m 文件不能正常打开,uitableveiwController

    当打开.m文件时尤其是自定义的继承uitableviewcontroler的m 文件.不能滑动,不能正常显示. 解决方法: 用文本编辑器打开这个文件,关闭xcode .然后在继承uitableview ...

  6. ruby实现简易计算器

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 回到家里,用的还是windows系统,ruby的编辑器换成了Aptana Studio 3 p ...

  7. nginx(五)nginx与php的安装配置

    经过前面学习,对nginx有个大概的了解,来配置LNMP;只要是在系统安装过程中选择安装比较齐全的包,基本上系统都能满足安装要求,下面是我一个一个测试的,基本上全部安装所需的库文件,放心安装: [ro ...

  8. zb的生日

    http://acm.nyist.net/JudgeOnline/problem.php?pid=325 zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 ...

  9. JQgrid for asp.net

     转载自http://blog.csdn.net/shiworkyue/article/details/8283716 JQgrid for asp.net 网上资料较少,自己总结了些不全,能用到的可 ...

  10. fcitx-sogoupinyin下载地址和安装

    伴随着Deepin 12.12 beta的发布,搜狗输入法也与我们见面了.在发布前几日Deepiner也通过各种途径向我们展示了搜狗Linux输入法,当然也掉足了胃口. 来自官方的截图: 当然令很多U ...