美文网首页
支持手势photoview源码分析记录

支持手势photoview源码分析记录

作者: 辣公公 | 来源:发表于2017-01-10 18:25 被阅读79次

    工程结构

    这里是做记录的,废话不多说先上项目图大体上分为四个部分

    • gestures包 与手势相关的操作
    • log包 日志相关
    • scrollerproxy包 与滚动相关
    • 剩下的就是核心代码 (图片的缩放)


      工程结构

    gestures包

    1. UML类图


      类图
    2. 接口
    • GestureDetector
     public interface GestureDetector {
      //处理Imageview的OnTouchListener,详情见PhotoViewAttacher 类
      boolean onTouchEvent(MotionEvent ev);
      //是否真正缩放
      boolean isScaling();
     //是否正在拖拽,也就是手指没有离开Imagview 
      boolean isDragging();
      //将一些手指状态返回,外部做处理
      void setOnGestureListener(OnGestureListener listener);
    }
    
    public interface OnGestureListener {
        //拖拽时调用  dx 水平方向发生的位置 dy 竖直方向发生的位移
        void onDrag(float dx, float dy);
        //快速滑动离开ImageView时调用,startX startY手指离开时的X,Y 坐标
        //velocityX 水平方向的速度,velocityY 竖直方向的速度
        void onFling(float startX, float startY, float velocityX,float velocityY);
      //缩放时调用 缩放比例 手指点击的地方的X Y 坐标
        void onScale(float scaleFactor, float focusX, float focusY);
    }
    
    • 工具类 VersionedGestureDetector
    public final class VersionedGestureDetector {
          //根据版本生成一个 GestureDetector 对象
        public static GestureDetector newInstance(Context context,
                                                  OnGestureListener listener) {
            final int sdkVersion = Build.VERSION.SDK_INT;
            GestureDetector detector;
            if (sdkVersion < Build.VERSION_CODES.ECLAIR) {
                detector = new CupcakeGestureDetector(context);
            } else if (sdkVersion < Build.VERSION_CODES.FROYO) {
                detector = new EclairGestureDetector(context);
            } else {
                detector = new FroyoGestureDetector(context);
            }
            detector.setOnGestureListener(listener);
            return detector;
        }
    }
    

    相关文章

      网友评论

          本文标题:支持手势photoview源码分析记录

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