美文网首页
《Android开发艺术探索》摘录3

《Android开发艺术探索》摘录3

作者: 拿破轮 | 来源:发表于2016-10-19 23:55 被阅读20次

    第三章 View的事件体系

    3.1 View的基础知识

    1.View的位置主要由它的四个顶点来决定,分别对应于View的四个属性:
    top、left、right、bottom(都是坐标信息)。需要注意的是

    这些坐标都是相对于View的父容器来说的,因此它是一种相对坐标根据图1-1可以得出View的宽高和坐标的关系:

    width = right - left   
    height = bottom - top
    y = top + translationY //y是左上角y坐标,记住平移过程中top和left不会变的,改变的是x、y、translationX和translationY
    
    • 其中right值获取的方式即为 Right = getRight();
    1-1 View的位置坐标和父容器的关系

    2 通过MotionEvent对象我们可以得到点击事件发生的x和y坐标。为此系统提供了两组方法:getX/getY和getRawX/getRawY。区别如下:

    getX/getY返回的是相对与当前View左上角的x和y坐标,
    getRawX/getRawY返回的是相对于手机屏幕左上角的x和y坐标

    3.TouchSlop是系统所能识别出的被认为是滑动的最小距离,小于改值不算滑动(8dp),可以通过:ViewConfiguration.get(getContext()).getScaledTouchSlop()

    4 VelocityTracker //速度追踪,步骤如下

    • 首先,在View的onTouchEvent方法中追踪当前单击事件的速度:

      VelocityTracker velocityTracker = VelocityTracker.obtain();
      velocityTracker.addMovement(event);
      
    • 接着,当我们想知道当前的滑动速度时,这个时候可以采用如下方式获得当前的速度。

      velocityTracker.computeCurrentVelocity(1000);//在1s内
      int  xVelocity = (int) velocityTracker.getXVelocity();
      int  yVelocity = (int) velocityTracker.getYVelocity();  
      /** 不用的话需要回收  **/
      velocityTracker.clear();
      velocityTracker.recycle();
      

    5.GestureDetector //手势检测(用于检测用户的单击、滑动、长按、双击等行为),使用过程如下:

    • 首先,需要创建一个GestureDetector对象并实现OnGestureListener接口,根据需要还可以实现OnDoubleTapListener从而能够监听双击行为

      GestureDetector mGestureDetector = new GestureDetector(this);
      //解决长按屏幕后无法拖动的现象
      mGestureDetector.setIsLongpressEnabled(false);
      
    • 接着,接管目标View的onTouchEvent方法,在待监听View的onTouchEvent方法中添加如下实现:

      boolean consume = mGestureDetector.onTouchEvent(event);
      return  consume;
      
    • 有选择的实现OnGestureListenter和OnDoubleTapListener中的方法了。
      常用的有:onSingleTapUp(单击)、onFling(快速滑动)、onScroll(拖动)、onLongPress(长按)和onDoubleTap(双击)。
      建议:如果只是监听有关的,建议自己在onTouchEvent中实现,如果要监听双击这种行为的话,那么就要使用GestureDetector。
      6.Scroller //弹性滑动对象,用于实现View的弹性滑动。解释一下:当我们使用View的srollTo/srollBy方法来进行滑动时,过程是瞬间的,这个没有过渡过程体验很差,所以需要Scroller和View的computeScroll方法配合使用才能共同完成这个功能,典型代码如下:

      Scroller scroller = new Scroller(mContext);
      //缓慢滚动到指定位置
      private void smoothScrollTo(int destX,int destY) {
              int   scrollX = getSrollX();
              int   delta = destX - scrollX;
              //1000ms 内滑向destX,效果就是慢慢滑动
              mScroller.startScroll(scrollX,0,delta,0,1000);
              invalidate();
      }
      
      @Override
      public void computeScroll(){
             if(mScroller.computeScrollOffset())  {
                  scrollTo(mScroller.getCurrx(), mScroller.getCurrY());
                  postInvalidate();
             }
       }
      

    3.2 View的滑动

    三种方式

    • 使用scrollTo/scrollBy
      • 在滑动过程中,mScrollX的值总是等于View左边缘和View内容左边缘在水平方向的距离(同理mScrollY也是),如图(3-1)
      • scrollTo和scrollBy只能改变View内容的位置而不能改变View在布局中的位置
    • 通过动画给View施加平移效果实现滑动
    • 通过改变view的LayoutParams使得View重新布局从而实现滑动
      • 这个比较好理解,就是直接改变控件的属性,如下:
          MarginLayoutParams parmas = (MarginLayoutParams)mButton1.getLayoutParams();
          params.witdh += 100;
          params.leftMargin += 100;
          mButton1.requestLayout();//或者mButton1.setLayoutParams(params);
    
    3-1

    3.3 弹性滑动(需要配合代码理解,P136-P140)

    • 使用Scroller
    • 通过动画
    • 使用延时策略

    相关文章

      网友评论

          本文标题:《Android开发艺术探索》摘录3

          本文链接:https://www.haomeiwen.com/subject/puzxuttx.html