接下来让我们来看一下那个自定义滚轮的代码TuneWheel:
- /**
- * 调频的滚轮
- *
- * @author way
- *
- */
- public class TuneWheel extends View {
- /**
- * 滚轮滚动监听接口
- */
- public interface OnTuneWheelValueChangedListener {
- public void onTuneWheelValueChanged(View v, float changedBy);
- }
- private static final String TAG = "TuneWheel";
- private static final int LINE_SPAN = 8;//线性跨度
- private static final int DRAG_THRESHOLD = 5;//滚动的最小阈值,即在屏幕x轴上至少滑动5个坐标点才有效
- private static final float CHANGE_THRESHOLD = 0.1f;//最小的改变阈值,即需要传递出去的那个值的最小值
- public static final int DIRECTION_PREV = 1;//向左滚动的方向值
- public static final int DIRECTION_NEXT = 2;//向右滚动的方向值
- private boolean mIsMoving = false;// 是否在滑动
- private float mLastDragPos;//最终的滚动位置
- private float mCurrentPos = 0;// 当前位置
- private float mCurrentChange = 0;// 当前改变值
- private int mDragEnable = DIRECTION_PREV | DIRECTION_NEXT;
- private OnTuneWheelValueChangedListener mListener = null;// 监听接口对象
- public void setOnValueChangedListener(
- OnTuneWheelValueChangedListener listener) {// 提供监听方法
- mListener = listener;
- }
- /**
- * 根据传入的方向设置是否允许滚动
- *
- * @param direction
- * 传入的方向
- * @param enable
- * 是否能滚动
- */
- public void setDragEnable(int direction, boolean enable) {
- if (enable) {
- mDragEnable = mDragEnable | direction;
- } else {
- mDragEnable = mDragEnable ^ direction;
- }
- }
- /**
- * 根据输入的方向判断是否允许滚动
- *
- * @param direction
- * 传入的方向
- * @return 是否能滚动
- */
- public boolean getDragEnable(int direction) {
- return (mDragEnable & direction) != 0;
- }
- // 三个构造器
- public TuneWheel(Context context) {
- super(context);
- }
- public TuneWheel(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public TuneWheel(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- // 重写onDraw方法
- protected void onDraw(Canvas canvas) {
- Paint mLinePaint = new Paint();
- mLinePaint.setARGB(0xEC, 0x00, 0, 0);
- mLinePaint.setStrokeWidth(0.5f);
- for (int pos = 0; pos + mCurrentPos < getWidth(); pos += LINE_SPAN) {
- canvas.drawLine(pos + mCurrentPos, 0, pos + mCurrentPos,
- getHeight(), mLinePaint);
- }
- }
- // 重写onTouchEvent方法,实现滚动效果
- public boolean onTouchEvent(MotionEvent event) {
- if (!this.isEnabled())
- return false;
- int action = event.getAction();
- switch (action) {
- case MotionEvent.ACTION_DOWN://按下
- mLastDragPos = event.getX();//获取按下时的x轴坐标值,滚轮只能左右滑动,所以只要获取x值即可
- break;
- case MotionEvent.ACTION_MOVE://移动
- float currentDragPos = event.getX();
- if (!getDragEnable(getDirection(currentDragPos, mLastDragPos)))//如果当前方向不允许滚动
- return false;
- if (!mIsMoving) {//如果当前状态为没有滚动
- if (Math.abs(currentDragPos - mLastDragPos) > DRAG_THRESHOLD) {//如果滚动范围大于最小阈值
- mIsMoving = true;
- mLastDragPos = currentDragPos;
- } else {
- return false;
- }
- } else {//如果当前状态为一直在滚动
- float tempPos = (mCurrentPos + currentDragPos - mLastDragPos)
- % LINE_SPAN;//保存一个临时变化值
- mCurrentChange += calculateChange(currentDragPos, mLastDragPos);
- if (Math.abs(mCurrentChange) > CHANGE_THRESHOLD) {
- if (mListener != null) {
- mListener.onTuneWheelValueChanged(this, mCurrentChange);//将滚动变化值通过监听接口传递出去
- }
- mCurrentChange = 0;
- }
- mCurrentPos = tempPos;
- invalidate();//千万要记得刷新一下界面
- mLastDragPos = currentDragPos;
- }
- break;
- case MotionEvent.ACTION_UP://松开
- if (mIsMoving) {
- mIsMoving = false;//松开后更新状态为停止滚动0.
- }
- break;
- }
- return true;
- }
- /**
- * 根据改变计算滚动方向
- *
- * @param current
- * 当前位置值
- * @param last
- * 最终位置值
- * @return 方向值
- */
- private int getDirection(float current, float last) {
- return (int) (Math.signum(current - last) + 3) / 2;
- }
- /**
- * 根据改变计算改变值
- *
- * @param current
- * 当前位置值
- * @param last
- * 最终位置值
- * @return 改变值
- */
- private float calculateChange(float current, float last) {
- float sub = current - last;
- return Math.signum(sub) * (float) Math.pow(Math.abs(sub), 1.3) / 100;
- }
- }
