Android实现手势滑动多点触摸放大缩小图片效果

2019-12-10 19:07:12刘景俊

onTouchMove:移动的处理.

/** 移动的处理 **/ 
void onTouchMove(MotionEvent event) { 
  int left = 0, top = 0, right = 0, bottom = 0; 
  /** 处理拖动 **/ 
  if (mode == MODE.DRAG) { 
 
    /** 在这里要进行判断处理,防止在drag时候越界 **/ 
 
    /** 获取相应的l,t,r ,b **/ 
    left = current_x - start_x; 
    right = current_x + this.getWidth() - start_x; 
    top = current_y - start_y; 
    bottom = current_y - start_y + this.getHeight(); 
 
    /** 水平进行判断 **/ 
    if (isControl_H) { 
      if (left >= 0) { 
        left = 0; 
        right = this.getWidth(); 
      } 
      if (right <= screen_W) { 
        left = screen_W - this.getWidth(); 
        right = screen_W; 
      } 
    } else { 
      left = this.getLeft(); 
      right = this.getRight(); 
    } 
    /** 垂直判断 **/ 
    if (isControl_V) { 
      if (top >= 0) { 
        top = 0; 
        bottom = this.getHeight(); 
      } 
 
      if (bottom <= screen_H) { 
        top = screen_H - this.getHeight(); 
        bottom = screen_H; 
      } 
    } else { 
      top = this.getTop(); 
      bottom = this.getBottom(); 
    } 
    if (isControl_H || isControl_V) 
      this.setPosition(left, top, right, bottom); 
 
    current_x = (int) event.getRawX(); 
    current_y = (int) event.getRawY(); 
 
  } 
  /** 处理缩放 **/ 
  else if (mode == MODE.ZOOM) { 
 
    afterLenght = getDistance(event);// 获取两点的距离 
 
    float gapLenght = afterLenght - beforeLenght;// 变化的长度 
 
    if (Math.abs(gapLenght) > 5f) { 
      scale_temp = afterLenght / beforeLenght;// 求的缩放的比例 
 
      this.setScale(scale_temp); 
 
      beforeLenght = afterLenght; 
    } 
  } 
 
} 

处理的逻辑比较繁多,但上诉代码大部分都已注释,我相信大家都看得懂,大家可以掌握原理后可以进行自己的逻辑处理.

下面我们看下缩放处理,因为考虑到越界与否.
setScale方法:

/** 处理缩放 **/ 
  void setScale(float scale) { 
    int disX = (int) (this.getWidth() * Math.abs(1 - scale)) / 4;// 获取缩放水平距离 
    int disY = (int) (this.getHeight() * Math.abs(1 - scale)) / 4;// 获取缩放垂直距离 
 
    // 放大 
    if (scale > 1 && this.getWidth() <= MAX_W) { 
      current_Left = this.getLeft() - disX; 
      current_Top = this.getTop() - disY; 
      current_Right = this.getRight() + disX; 
      current_Bottom = this.getBottom() + disY; 
 
      this.setFrame(current_Left, current_Top, current_Right, 
          current_Bottom); 
      /*** 
       * 此时因为考虑到对称,所以只做一遍判断就可以了。 
       */ 
      if (current_Top <= 0 && current_Bottom >= screen_H) { 
        Log.e("jj", "屏幕高度=" + this.getHeight()); 
        isControl_V = true;// 开启垂直监控 
      } else { 
        isControl_V = false; 
      } 
      if (current_Left <= 0 && current_Right >= screen_W) { 
        isControl_H = true;// 开启水平监控 
      } else { 
        isControl_H = false; 
      } 
 
    } 
    // 缩小 
    else if (scale < 1 && this.getWidth() >= MIN_W) { 
      current_Left = this.getLeft() + disX; 
      current_Top = this.getTop() + disY; 
      current_Right = this.getRight() - disX; 
      current_Bottom = this.getBottom() - disY; 
      /*** 
       * 在这里要进行缩放处理 
       */ 
      // 上边越界 
      if (isControl_V && current_Top > 0) { 
        current_Top = 0; 
        current_Bottom = this.getBottom() - 2 * disY; 
        if (current_Bottom < screen_H) { 
          current_Bottom = screen_H; 
          isControl_V = false;// 关闭垂直监听 
        } 
      } 
      // 下边越界 
      if (isControl_V && current_Bottom < screen_H) { 
        current_Bottom = screen_H; 
        current_Top = this.getTop() + 2 * disY; 
        if (current_Top > 0) { 
          current_Top = 0; 
          isControl_V = false;// 关闭垂直监听 
        } 
      } 
 
      // 左边越界 
      if (isControl_H && current_Left >= 0) { 
        current_Left = 0; 
        current_Right = this.getRight() - 2 * disX; 
        if (current_Right <= screen_W) { 
          current_Right = screen_W; 
          isControl_H = false;// 关闭 
        } 
      } 
      // 右边越界 
      if (isControl_H && current_Right <= screen_W) { 
        current_Right = screen_W; 
        current_Left = this.getLeft() + 2 * disX; 
        if (current_Left >= 0) { 
          current_Left = 0; 
          isControl_H = false;// 关闭 
        } 
      } 
 
      if (isControl_H || isControl_V) { 
        this.setFrame(current_Left, current_Top, current_Right, 
            current_Bottom); 
      } else { 
        this.setFrame(current_Left, current_Top, current_Right, 
            current_Bottom); 
        isScaleAnim = true;// 开启缩放动画 
      } 
 
    } 
 
  }