你好,游客 登录 注册 搜索
背景:
阅读新闻

Android之收音机UI实现

[日期:2012-11-26] 来源:Linux社区  作者:weidi1989 [字体:大 中 小]

接下来让我们来看一下那个自定义滚轮的代码TuneWheel:

  1. /** 
  2.  * 调频的滚轮 
  3.  *  
  4.  * @author way 
  5.  *  
  6.  */ 
  7. public class TuneWheel extends View { 
  8.     /** 
  9.      * 滚轮滚动监听接口 
  10.      */ 
  11.     public interface OnTuneWheelValueChangedListener { 
  12.         public void onTuneWheelValueChanged(View v, float changedBy); 
  13.     } 
  14.  
  15.     private static final String TAG = "TuneWheel"; 
  16.     private static final int LINE_SPAN = 8;//线性跨度  
  17.     private static final int DRAG_THRESHOLD = 5;//滚动的最小阈值,即在屏幕x轴上至少滑动5个坐标点才有效  
  18.     private static final float CHANGE_THRESHOLD = 0.1f;//最小的改变阈值,即需要传递出去的那个值的最小值  
  19.     public static final int DIRECTION_PREV = 1;//向左滚动的方向值  
  20.     public static final int DIRECTION_NEXT = 2;//向右滚动的方向值  
  21.  
  22.     private boolean mIsMoving = false;// 是否在滑动  
  23.     private float mLastDragPos;//最终的滚动位置  
  24.     private float mCurrentPos = 0;// 当前位置  
  25.     private float mCurrentChange = 0;// 当前改变值  
  26.     private int mDragEnable = DIRECTION_PREV | DIRECTION_NEXT; 
  27.  
  28.     private OnTuneWheelValueChangedListener mListener = null;// 监听接口对象  
  29.  
  30.     public void setOnValueChangedListener( 
  31.             OnTuneWheelValueChangedListener listener) {// 提供监听方法  
  32.         mListener = listener; 
  33.     } 
  34.  
  35.     /** 
  36.      * 根据传入的方向设置是否允许滚动 
  37.      *  
  38.      * @param direction 
  39.      *            传入的方向 
  40.      * @param enable 
  41.      *            是否能滚动 
  42.      */ 
  43.     public void setDragEnable(int direction, boolean enable) { 
  44.         if (enable) { 
  45.             mDragEnable = mDragEnable | direction; 
  46.         } else { 
  47.             mDragEnable = mDragEnable ^ direction; 
  48.         } 
  49.     } 
  50.  
  51.     /** 
  52.      * 根据输入的方向判断是否允许滚动 
  53.      *  
  54.      * @param direction 
  55.      *            传入的方向 
  56.      * @return 是否能滚动 
  57.      */ 
  58.     public boolean getDragEnable(int direction) { 
  59.         return (mDragEnable & direction) != 0; 
  60.     } 
  61.  
  62.     // 三个构造器  
  63.     public TuneWheel(Context context) { 
  64.         super(context); 
  65.     } 
  66.  
  67.     public TuneWheel(Context context, AttributeSet attrs) { 
  68.         super(context, attrs); 
  69.     } 
  70.  
  71.     public TuneWheel(Context context, AttributeSet attrs, int defStyle) { 
  72.         super(context, attrs, defStyle); 
  73.     } 
  74.  
  75.     // 重写onDraw方法  
  76.     protected void onDraw(Canvas canvas) { 
  77.         Paint mLinePaint = new Paint(); 
  78.         mLinePaint.setARGB(0xEC, 0x00, 0, 0); 
  79.         mLinePaint.setStrokeWidth(0.5f); 
  80.         for (int pos = 0; pos + mCurrentPos < getWidth(); pos += LINE_SPAN) { 
  81.             canvas.drawLine(pos + mCurrentPos, 0, pos + mCurrentPos, 
  82.                     getHeight(), mLinePaint); 
  83.         } 
  84.     } 
  85.  
  86.     // 重写onTouchEvent方法,实现滚动效果  
  87.     public boolean onTouchEvent(MotionEvent event) { 
  88.         if (!this.isEnabled()) 
  89.             return false; 
  90.  
  91.         int action = event.getAction(); 
  92.  
  93.         switch (action) { 
  94.         case MotionEvent.ACTION_DOWN://按下  
  95.             mLastDragPos = event.getX();//获取按下时的x轴坐标值,滚轮只能左右滑动,所以只要获取x值即可  
  96.             break; 
  97.         case MotionEvent.ACTION_MOVE://移动  
  98.             float currentDragPos = event.getX(); 
  99.  
  100.             if (!getDragEnable(getDirection(currentDragPos, mLastDragPos)))//如果当前方向不允许滚动  
  101.                 return false; 
  102.  
  103.             if (!mIsMoving) {//如果当前状态为没有滚动  
  104.                 if (Math.abs(currentDragPos - mLastDragPos) > DRAG_THRESHOLD) {//如果滚动范围大于最小阈值  
  105.                     mIsMoving = true; 
  106.                     mLastDragPos = currentDragPos; 
  107.                 } else { 
  108.                     return false; 
  109.                 } 
  110.             } else {//如果当前状态为一直在滚动  
  111.                 float tempPos = (mCurrentPos + currentDragPos - mLastDragPos) 
  112.                         % LINE_SPAN;//保存一个临时变化值  
  113.  
  114.                 mCurrentChange += calculateChange(currentDragPos, mLastDragPos); 
  115.                 if (Math.abs(mCurrentChange) > CHANGE_THRESHOLD) { 
  116.                     if (mListener != null) { 
  117.                         mListener.onTuneWheelValueChanged(this, mCurrentChange);//将滚动变化值通过监听接口传递出去  
  118.                     } 
  119.                     mCurrentChange = 0; 
  120.                 } 
  121.  
  122.                 mCurrentPos = tempPos; 
  123.                 invalidate();//千万要记得刷新一下界面  
  124.                 mLastDragPos = currentDragPos; 
  125.             } 
  126.             break; 
  127.         case MotionEvent.ACTION_UP://松开  
  128.             if (mIsMoving) { 
  129.                 mIsMoving = false;//松开后更新状态为停止滚动0.  
  130.             } 
  131.             break; 
  132.         } 
  133.  
  134.         return true; 
  135.     } 
  136.  
  137.     /** 
  138.      * 根据改变计算滚动方向 
  139.      *  
  140.      * @param current 
  141.      *            当前位置值 
  142.      * @param last 
  143.      *            最终位置值 
  144.      * @return 方向值 
  145.      */ 
  146.     private int getDirection(float current, float last) { 
  147.         return (int) (Math.signum(current - last) + 3) / 2; 
  148.     } 
  149.  
  150.     /** 
  151.      * 根据改变计算改变值 
  152.      *  
  153.      * @param current 
  154.      *            当前位置值 
  155.      * @param last 
  156.      *            最终位置值 
  157.      * @return 改变值 
  158.      */ 
  159.     private float calculateChange(float current, float last) { 
  160.         float sub = current - last; 
  161.         return Math.signum(sub) * (float) Math.pow(Math.abs(sub), 1.3) / 100; 
  162.     } 
  163. } 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款