当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现ImageView图片双击放大及缩小

Android实现ImageView图片双击放大及缩小

2019年07月24日  | 移动技术网IT编程  | 我要评论

溺爱皇室宠公主,脊梁颂,劲舞世界官网

本文实例介绍了android实现imageview图片双击放大及缩小的相关技巧,分享给大家供大家参考,具体内容如下

public class doublescaleimageview extends imageview implements ontouchlistener, ongloballayoutlistener {
  private boolean isfirst = false;
  private float doublescale;// 双击放大的值
  private matrix mscalematrix;
  private float defaultscale;// 默认的缩放值
  private int mlastpintercount;// 记录上一次多点触控的数量
  private float mlastx;
  private float mlasty;
  private int mtouchslop;
  private boolean iscandrag;
  private boolean ischeckleft;
  private boolean ischecktop;
  private gesturedetector mgesturedetector;
  public doublescaleimageview(context context) {
    this(context, null);
  }
  public doublescaleimageview(context context, attributeset attrs) {
    this(context, attrs, 0);
  }
  @suppresslint("clickableviewaccessibility")
  public doublescaleimageview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    mscalematrix = new matrix();
    setscaletype(scaletype.matrix);
    setontouchlistener(this);
    // getscaledtouchslop是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件。如果小于这个距离就不触发移动控件
    mtouchslop = viewconfiguration.get(context).getscaledtouchslop();
    mgesturedetector = new gesturedetector(context, new gesturedetector.simpleongesturelistener() {
      @override
      public boolean ondoubletap(motionevent e) {
        float x = e.getx();
        float y = e.gety();
        if (getscale() < doublescale) {
          mscalematrix.postscale(doublescale / getscale(), doublescale / getscale(), x, y);// 放大
        }
        else {
          mscalematrix.postscale(defaultscale / getscale(), defaultscale / getscale(), x, y);// 缩小
        }
        setimagematrix(mscalematrix);
        return super.ondoubletap(e);
      }
    });
  }
  @override
  protected void onattachedtowindow() {// view附加到窗体上时调用该方法
    super.onattachedtowindow();
    getviewtreeobserver().addongloballayoutlistener(this);
  }
  @suppresswarnings("deprecation")
  @override
  protected void ondetachedfromwindow() {// 将视图从窗体上分离的时候调用该方法。
    super.ondetachedfromwindow();
    getviewtreeobserver().removeglobalonlayoutlistener(this);
  }
  @override
  public void ongloballayout() {// 在这个方法中获取imageview加载完成后的图片
    if (!isfirst) {
      // 获取控件的宽度和高度
      int width = getwidth();
      int height = getheight();
      // 得到我们的图片以及图片的宽度及高度
      drawable drawable = getdrawable();
      if (drawable == null) { return; }
      int imagewidth = drawable.getintrinsicwidth();// 图片的宽度
      int imageheight = drawable.getintrinsicheight();// 图片的高度
      float scale = 1.0f;
      // 如果图片宽度大于控件宽度,但是图片高度小于控件 高度,我们要缩小图片
      if (imagewidth > width && imageheight < height) {
        scale = width * 1.0f / imagewidth;
      }
      // 如果图片宽度小于控件宽度,但是图片高度大于控件 高度,我们要缩小图片
      if (imagewidth < width && imageheight > height) {
        scale = height * 1.0f / imageheight;
      }
      // 如果图片的宽度都 大于或小于控件宽度,我们则要对图片进行对应缩放,保证图片占满控件
      if ((imagewidth > width && imageheight > height) || (imagewidth < width && imageheight < height)) {
        scale = math.min(width * 1.0f / imagewidth, height * 1.0f / imageheight);
      }
      // 初始化对应的缩放值
      defaultscale = scale;
      doublescale = defaultscale * 2;
      // 图片缩放后,将图片要移动到控件中心
      int dx = width / 2 - imagewidth / 2;
      int dy = height / 2 - imageheight / 2;
      mscalematrix.posttranslate(dx, dy);
      mscalematrix.postscale(defaultscale, defaultscale, width / 2, height / 2);
      setimagematrix(mscalematrix);
      isfirst = true;
    }
  }
  @suppresslint("clickableviewaccessibility")
  @override
  public boolean ontouch(view v, motionevent event) {
    if (mgesturedetector.ontouchevent(event)) { return true; }
    float x = 0;
    float y = 0;
    int pointercount = event.getpointercount();// 获取放在屏幕上的手指数量
    for (int i = 0; i < pointercount; i++) {
      x += event.getx(i);
      y += event.gety(i);
    }
    x /= pointercount;
    y /= pointercount;
    if (mlastpintercount != pointercount) {
      iscandrag = false;
      mlastx = x;
      mlasty = y;

    }
    mlastpintercount = pointercount;
    switch (event.getaction()) {
      case motionevent.action_move:
        float dx = x - mlastx;
        float dy = y - mlasty;
        iscandrag = ismove(dx, dy);
        if (iscandrag) {
          rectf rectf = getmatrixrectf();
          if (null != getdrawable()) {
            ischeckleft = ischecktop = true;
            if (rectf.width() < getwidth()) {// 如果图片宽度小于控件宽度(屏幕宽度)不允许横向移动
              dx = 0;
              ischeckleft = false;
            }
            if (rectf.height() < getheight()) {// 如果图片高度小于控件高度(屏幕高度)不允许纵向移动
              dy = 0;
              ischecktop = false;
            }
            mscalematrix.posttranslate(dx, dy);
            checktranslatewithborder();
            setimagematrix(mscalematrix);
          }
        }
        mlastx = x;
        mlasty = y;
        break;
      case motionevent.action_up:
      case motionevent.action_cancel:
        mlastpintercount = 0;
        break;
    }
    return true;
  }
  /**
   * 移动图片时进行边界检查
   * @description:
   * @date 2016-1-8 下午4:02:24
   */
  private void checktranslatewithborder() {
    rectf rectf = getmatrixrectf();
    float delx = 0;
    float dely = 0;
    int width = getwidth();
    int height = getheight();
    if (rectf.top > 0 && ischecktop) {
      dely = -rectf.top;
    }
    if (rectf.bottom < height && ischecktop) {
      dely = height - rectf.bottom;
    }
    if (rectf.left > 0 && ischeckleft) {
      delx = -rectf.left;
    }
    if (rectf.right < width && ischeckleft) {
      delx = width - rectf.right;
    }
    mscalematrix.posttranslate(delx, dely);
  }
  // 判断是否有移动
  private boolean ismove(float x, float y) {
    return math.sqrt(x * x + y * y) > mtouchslop;
  }
  /**
   * 获取图片的位置
   * @description:
   * @date 2016-1-8 上午9:02:10
   */
  private rectf getmatrixrectf() {
    matrix matrix = mscalematrix;
    rectf recft = new rectf();
    if (getdrawable() != null) {
      recft.set(0, 0, getdrawable().getintrinsicwidth(), getdrawable().getintrinsicheight());
      matrix.maprect(recft);
    }
    return recft;
  }

  // 获取当前图片的缩放值
  private float getscale() {
    float values[] = new float[9];
    mscalematrix.getvalues(values);
    return values[matrix.mscale_x];
  }
}

以上就是安卓实现imageview图片双击放大及缩小的全部代码,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网