只需要自定义ImageView
package com.ringing.phone.weight;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
/**
* Created by houruixiang on 2017/8/22.
*/
@SuppressLint("AppCompatCustomView")
public class ZoomImageView extends ImageView implements ViewTreeObserver.OnGlobalLayoutListener
, View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private boolean mOnce;
private float minScale;
private float maxScale;
private float mTouchScale;
private Matrix mMatrix;
/**捕获多指触摸的手势*/
private ScaleGestureDetector mScaleGestureDetector;
private Object rectF;
/**------------------------------------------------
* 自由移动*/
private float mLastX;
private float mLastY;
private boolean isMoveX;
private boolean isMoveY;
private boolean isMove;
private int mPointCount;
private int mTouchslop;
/**------------------------------------------------
* 双击放大+缩小*/
private GestureDetector mGes;
private boolean isClick;
public ZoomImageView(Context context) {
this(context,null);
}
public ZoomImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public ZoomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mMatrix = new Matrix();
setScaleType(ScaleType.MATRIX);
mScaleGestureDetector = new ScaleGestureDetector(getContext(), this);
setOnTouchListener(this);
mTouchslop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mGes = new GestureDetector(getContext(),new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
float scale = getScale();
if (scale < mTouchScale){
Log.i("==currScale",String.valueOf(mTouchScale) + ":::" + String.valueOf(scale));
//放大的逻辑---注意要渐变
if (!isClick){
isClick = true;
postDelayed(new ChangeRunnable(mTouchScale,x,y),16);
}