Android实现手势控制ImageView图片大小

本文实例实现的主要功能是在ImageView中识别手势用以控制图片放大或缩小,具有一定的参考价值,分享给大家。

public class MatrixImageView extends ImageView {
  private GestureDetector mGestureDetector;
  private Matrix mMatrix = new Matrix();
  private float mImageWidth;
  private float mImageHeight;
  private float mScale;
  private OnMovingListener mMoveListener;
  private OnSingleTapListener mSingleTapListener;
  public MatrixImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  public MatrixImageView(Context context) {
    super(context, null);
    init();
  }
  private void init() {
    MatrixTouchListener listener = new MatrixTouchListener();
    setOnTouchListener(listener);
    mGestureDetector = new GestureDetector(getContext(),
        new GestureListener(listener));
    setBackgroundColor(Color.BLACK);
    setScaleType(ScaleType.FIT_CENTER);
  }
  public void setOnMovingListener(OnMovingListener listener) {
    mMoveListener = listener;
  }
  public void setOnSingleTapListener(OnSingleTapListener onSingleTapListener) {
    this.mSingleTapListener = onSingleTapListener;
  }
  @Override
  public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    if (getWidth() == 0) {

      ViewTreeObserver vto = getViewTreeObserver();
      vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {

          initData();
          MatrixImageView.this.getViewTreeObserver()
              .removeOnPreDrawListener(this);
          return true;
        }

      });
    } else {
      initData();
    }
  }
  private void initData() {
    mMatrix.set(getImageMatrix());
    float[] values = new float[9];
    mMatrix.getValues(values);
    mImageWidth = getWidth() / values[Matrix.MSCALE_X];
    mImageHeight = (getHeight() - values[Matrix.MTRANS_Y] * 2)
        / values[Matrix.MSCALE_Y];
    mScale = values[Matrix.MSCALE_X];
  }
  public class MatrixTouchListener implements OnTouchListener {
    private static final int MODE_DRAG = 1;
    private static final int MODE_ZOOM = 2;
    private static final int MODE_UNABLE = 3;
    private static final float MAX_SCALE = 6;
    private static final float DOUBLE_CLICK_SACLE = 2;
    private int mMode = 0;
    private float mStartDis;
    private Matrix mCurrentMatrix = new Matrix();
    private boolean mLeftDragable;
    private boolean mRightDragable;
    private boolean mFirstMove = false;
    private PointF mStartPoint = new PointF();
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        mMode = MODE_DRAG;
        mStartPoint.set(event.getX(), event.getY());
        isMatrixEnable();
        startDrag();
        checkDragable();
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        reSetMatrix();
        stopDrag();
        break;
      case MotionEvent.ACTION_MOVE:
        if (mMode == MODE_ZOOM) {
          setZoomMatrix(event);
        } else if (mMode == MODE_DRAG) {
          setDragMatrix(event);
        } else {
          stopDrag();
        }
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        if (mMode == MODE_UNABLE)
          return true;
        mMode = MODE_ZOOM;
        mStartDis = distance(event);
        break;
      case MotionEvent.ACTION_POINTER_UP:
        break;
      default:
        break;
      }

      return mGestureDetector.onTouchEvent(event);

    }

    private void startDrag() {

      if (mMoveListener != null)

        mMoveListener.startDrag();

    }

    private void stopDrag() {

      if (mMoveListener != null)

        mMoveListener.stopDrag();

    }

    private void checkDragable() {

      mLeftDragable = true;

      mRightDragable = true;

      mFirstMove = true;

      float[] values = new float[9];

      getImageMatrix().getValues(values);

      if (values[Matrix.MTRANS_X] >= 0)

        mRightDragable = false;

      if ((mImageWidth) * values[Matrix.MSCALE_X]

          + values[Matrix.MTRANS_X] <= getWidth()) {

        mLeftDragable = false;

      }

    }

    public void setDragMatrix(MotionEvent event) {

      if (isZoomChanged()) {

        float dx = event.getX() - mStartPoint.x;

        float dy = event.getY() - mStartPoint.y;

        if (Math.sqrt(dx * dx + dy * dy) > 10f) {

          mStartPoint.set(event.getX(), event.getY());

          mCurrentMatrix.set(getImageMatrix());

          float[] values = new float[9];

          mCurrentMatrix.getValues(values);

          dy = checkDyBound(values, dy);

          dx = checkDxBound(values, dx, dy);

          mCurrentMatrix.postTranslate(dx, dy);

          setImageMatrix(mCurrentMatrix);

        }
      } else {
        stopDrag();
      }
    }
    private boolean isZoomChanged() {
      float[] values = new float[9];
      getImageMatrix().getValues(values);
      float scale = values[Matrix.MSCALE_X];
      return scale != mScale;
    }
    private float checkDyBound(float[] values, float dy) {
      float height = getHeight();
      if (mImageHeight * values[Matrix.MSCALE_Y] < height)
        return 0;
      if (values[Matrix.MTRANS_Y] + dy > 0)

        dy = -values[Matrix.MTRANS_Y];

      else if (values[Matrix.MTRANS_Y] + dy < -(mImageHeight
          * values[Matrix.MSCALE_Y] - height))

        dy = -(mImageHeight * values[Matrix.MSCALE_Y] - height)

            - values[Matrix.MTRANS_Y];

      return dy;

    }

    private float checkDxBound(float[] values, float dx, float dy) {

      float width = getWidth();

      if (!mLeftDragable && dx < 0) {

        if (Math.abs(dx) * 0.4f > Math.abs(dy) && mFirstMove) {

          stopDrag();

        }

        return 0;

      }
      if (!mRightDragable && dx > 0) {
        if (Math.abs(dx) * 0.4f > Math.abs(dy) && mFirstMove) {
          stopDrag();
        }
        return 0;
      }
      mLeftDragable = true;

      mRightDragable = true;

      if (mFirstMove)

        mFirstMove = false;

      if (mImageWidth * values[Matrix.MSCALE_X] < width) {

        return 0;

      }

      if (values[Matrix.MTRANS_X] + dx > 0) {

        dx = -values[Matrix.MTRANS_X];

      } else if (values[Matrix.MTRANS_X] + dx < -(mImageWidth

          * values[Matrix.MSCALE_X] - width)) {

        dx = -(mImageWidth * values[Matrix.MSCALE_X] - width)

            - values[Matrix.MTRANS_X];

      }

      return dx;

    }

    private void setZoomMatrix(MotionEvent event) {

      if (event.getPointerCount() < 2)
        return;

      float endDis = distance(event);
      if (endDis > 10f) {
        float scale = endDis / mStartDis;
        mStartDis = endDis;
        mCurrentMatrix.set(getImageMatrix());
        float[] values = new float[9];
        mCurrentMatrix.getValues(values);
        scale = checkMaxScale(scale, values);
        PointF centerF = getCenter(scale, values);
        mCurrentMatrix.postScale(scale, scale, centerF.x, centerF.y);
        setImageMatrix(mCurrentMatrix);
      }
    }
    private PointF getCenter(float scale, float[] values) {
      if (scale * values[Matrix.MSCALE_X] < mScale || scale >= 1) {
        return new PointF(getWidth() / 2, getHeight() / 2);
      }
      float cx = getWidth() / 2;
      float cy = getHeight() / 2;
      if ((getWidth() / 2 - values[Matrix.MTRANS_X]) * scale < getWidth() / 2)
        cx = 0;
      if ((mImageWidth * values[Matrix.MSCALE_X] + values[Matrix.MTRANS_X])
          * scale < getWidth())

        cx = getWidth();

      return new PointF(cx, cy);

    }
    private float checkMaxScale(float scale, float[] values) {

      if (scale * values[Matrix.MSCALE_X] > MAX_SCALE)
        scale = MAX_SCALE / values[Matrix.MSCALE_X];
      return scale;
    }
    private void reSetMatrix() {
      if (checkRest()) {
        mCurrentMatrix.set(mMatrix);
        setImageMatrix(mCurrentMatrix);
      } else {
        float[] values = new float[9];
        getImageMatrix().getValues(values);
        float height = mImageHeight * values[Matrix.MSCALE_Y];
        if (height < getHeight()) {
          float topMargin = (getHeight() - height) / 2;
          if (topMargin != values[Matrix.MTRANS_Y]) {
            mCurrentMatrix.set(getImageMatrix());
            mCurrentMatrix.postTranslate(0, topMargin
                - values[Matrix.MTRANS_Y]);
            setImageMatrix(mCurrentMatrix);
          }
        }
      }
    }
    private boolean checkRest() {
      float[] values = new float[9];
      getImageMatrix().getValues(values);
      float scale = values[Matrix.MSCALE_X];
      return scale < mScale;
    }
    private void isMatrixEnable() {
      if (getScaleType() != ScaleType.CENTER) {

        setScaleType(ScaleType.MATRIX);

      } else {
        mMode = MODE_UNABLE;
      }

    }
    private float distance(MotionEvent event) {

      float dx = event.getX(1) - event.getX(0);

      float dy = event.getY(1) - event.getY(0);

      return (float) Math.sqrt(dx * dx + dy * dy);
    }
    public void onDoubleClick() {
      float scale = isZoomChanged() ? 1 : DOUBLE_CLICK_SACLE;
      mCurrentMatrix.set(mMatrix);
      mCurrentMatrix.postScale(scale, scale, getWidth() / 2,
          getHeight() / 2);
      setImageMatrix(mCurrentMatrix);
    }
  }
  private class GestureListener extends SimpleOnGestureListener {
    private final MatrixTouchListener mTouchListener;
    public GestureListener(MatrixTouchListener listener) {
      this.mTouchListener = listener;
    }
    @Override
    public boolean onDown(MotionEvent e) {
      return true;
    }
    @Override
    public boolean onDoubleTap(MotionEvent e) {

      mTouchListener.onDoubleClick();
      return true;
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
      return super.onSingleTapUp(e);
    }
    @Override
    public void onLongPress(MotionEvent e) {
      super.onLongPress(e);
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,

        float distanceX, float distanceY) {
      return super.onScroll(e1, e2, distanceX, distanceY);

    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
      return super.onFling(e1, e2, velocityX, velocityY);
    }
    @Override
    public void onShowPress(MotionEvent e) {
      super.onShowPress(e);
    }
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
      return super.onDoubleTapEvent(e);
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
      if (mSingleTapListener != null)
        mSingleTapListener.onSingleTap(e);
      return super.onSingleTapConfirmed(e);
    }
  }
  public interface OnMovingListener {
    public void startDrag();

    public void stopDrag();
  }
  public interface OnSingleTapListener {
    public void onSingleTap(MotionEvent e);
  }
}

我对其中定义OnSingleTapListener接口的方法稍作了一些修改,为onSingleTap回调方法增加了MotionEvent类型的参数,来方便我们根据用户具体的事件内容作出对应的控制。

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

(0)

相关推荐

  • Android自定义GestureDetector实现手势ImageView

    不说废话了,进入我们今天的主题吧. 先贴上前面内容的地址: Android手势ImageView三部曲(一) Android手势ImageView三部曲(二) Android手势ImageView三部曲(三) 前面我们讲到了ScaleGestureDetector这个工具类,我在疑惑,为什么搞出一个ScaleGestureDetector,不顺带把什么旋转.移动.做了呢? 好吧-! 谷歌肯定还是想给开发者留一点自己的空间哈. 仿照ScaleGestureDetector,我们来定义一个叫Move

  • Android ImageView随手势变化动态缩放图片

    在Android实际项目开发中,经常要用到ImageView这个组件,如果纯粹只是用来显示某一张图片,这个原生的组件是很方便的.但有时候为了效果更炫一点,比如很多软件都有的对图片的处理,随着手势的变化来拉大或缩小图片,这时就需要对原生的ImageView做一些处理来达到我们想要的效果. 原理就是Matrix 和PointF的运用,其实明白后一点都不难.我们需要写一个类继承ImageView来实现我们想要的功能,我这里演示的是类名是:ScaleImageView ,主要是重写onTouchEven

  • Android通过手势实现的缩放处理实例代码

    网络上传言HTC的HERO-ROM支持多点触摸的论证大多源于浏览网页和图片时,能像IPhone一样通过手势来控制页面的大小.下面的例子是利用现有的API实现HERO浏览图片和网页的缩放功能. 主要原理是onTouchEvent事件中的参数MotionEvent,它有一个getSize()方法.在一个点的时候,该方法永远返回0,而在两个触电的时候,该方法则根据两点相对位置变化而返回不同的值.我们只需计算出两点之间的距离变化,距离的大小表明我们希望目标变化的趋势.而getX()和getY()方法则永

  • Android手势ImageView三部曲 第三部

    接着上一节 Android手势ImageView三部曲(二)的往下走,我们讲到了github上的GestureDetector框架, 先附上github链接: https://github.com/Almeros/android-gesture-detectors 其实把这个框架的主体思想也是参考的Android自带的ScaleGestureDetector工具类,ScaleGestureDetector估计是参考的GestureDetector工具类,不管谁参考谁的,既然被我们遇到了,我们就要

  • Android使用ImageView实现支持手势缩放效果

    TouchImageView继承自ImageView具有ImageView的所有功能:除此之外,还有缩放.拖拽.双击放大等功能,支持viewpager和scaletype,并伴有动画效果. sharedConstructing private void sharedConstructing(Context context) { super.setClickable(true); this.context = context; mScaleDetector = new ScaleGestureDe

  • android使用gesturedetector手势识别示例分享

    复制代码 代码如下: public class MyGestureLintener extends SimpleOnGestureListener {private Context context;public MyGestureLintener(Context context) {    super();    this.context = context;} // 单击,触摸屏按下时立刻触发/*@Overridepublic boolean onDown(MotionEvent e) {  

  • Android手势ImageView三部曲 第一部

    这几天一直在研究github上的PhotoView跟GestureImageView,发现写的都很牛,看了很久的代码,于是打算把自己所看的一些东西总结一下,内容还是很多的,但是很有含金量哈-- 先附上两个开源项目的链接: GestureImageView: https://github.com/jasonpolites/gesture-imageview PhotoView:https://github.com/chrisbanes/PhotoView 这样说有点乏味哈,先看看我们今天要实现的效

  • Android手势ImageView三部曲 第二部

    废话不多说了,还记得上一节Android手势ImageView三部曲(一)最后我们提及的那个框架么?这一节我们重点了掌握一下GestureDetector这个类相关的属性方法. 一.那么GestureDetector是干嘛的呢? 顾名思义,字面意思就是"手势检测器"的意思,还记得我们上一节中实现的GestureImageView么?我们在onTouchEvent中检测到了各种个样的手势(手指按下.抬起.什么时候属于拖拽.什么时候属于缩放)都是通过我们的计算得到的,但是有了Gesture

  • android开发之为activity增加左右手势识别示例

    android开发中为activity增加左右手势识别,如右滑关闭当前页面. 复制代码 代码如下: /* *  for左右手势 *  1.复制下面的内容到目标Activity *  2.目标Activity的onCreate()调用initGesture() *  3.目标Activity需implements OnTouchListener, OnGestureListener */   private GestureDetector mGestureDetector;   private i

  • Android手势滑动实现ImageView缩放图片大小

    本文推出了两种Android手势实现ImageView缩放图片大小的方法,分享给大家供大家参考,具体内容如下 方法一: 将以下代码写到MulitPointTouchListener.java中,然后对你相应的图片进行OnTouchListener. 例如:imageView.setOnTouchListener(new MulitPointTouchListener ()); 在xml中要将ImageView的缩放格式改成Matrix 例如:android:scaleType="matrix&q

随机推荐