不多说上代码
package com.sdp.panda.myviewapp.view;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
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 80926 on 2016/11/23.
*/
public class ZoomImageView extends ImageView implements ViewTreeObserver.OnGlobalLayoutListener,
ScaleGestureDetector.OnScaleGestureListener, View.OnTouchListener {
private boolean mOnce;
private float mInitScale;
private float mMidScale;
private float mMaxScale;
private Matrix mMatrix;
private ScaleGestureDetector mScaleGestureDetector;
private int mLastPointerCount;
private float mLastX;
private float mLastY;
private int mTouchSlop;
private boolean isCanDrag;
private boolean isMoveTopAndBottom;
private boolean isMoveLeftAndRight;
private GestureDetector mGestureDetector;
private boolean isAutoScale;
public ZoomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public ZoomImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZoomImageView(Context context) {
this(context, null);
}
private void init(Context context) {
mMatrix = new Matrix();
mScaleGestureDetector = new ScaleGestureDetector(context, this);
setOnTouchListener(this);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mGestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onDoubleTap(MotionEvent e) {
if (isAutoScale){
return true;
}
float x = e.getX();
float y = e.getY();
if (getScale()<mMidScale){
postDelayed(new AutoScaleRunnable(mMidScale,x,y),15);
isAutoScale = true;
}else {
postDelayed(new AutoScaleRunnable(mInitScale,x,y),15);
isAutoScale = true;
}
return true;
}
});
}
@Override
public void onGlobalLayout() {
if (!mOnce) {
int width = getWidth();
int height = getHeight();
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
float scaleSize = 0;
int dWidth = drawable.getIntrinsicWidth();
int dHeight = drawable.getIntrinsicHeight();
if (dWidth > width && dHeight < height) {
scaleSize = width * 1.0f / dWidth;
}
if (dHeight > height && dWidth < width) {
scaleSize = height * 1.0f / dHeight;
}
if (dWidth > width && dHeight > height || dWidth < width && dHeight < height) {
scaleSize = Math.min(height * 1.0f / dHeight, width * 1.0f / dWidth);
}
mInitScale = scaleSize;
mMidScale = mInitScale * 2;
mMaxScale = mInitScale * 4;
int centerX = (width - dWidth) / 2;
int centerY = (height - dHeight) / 2;
mMatrix.postTranslate(centerX, centerY);
mMatrix.postScale(mInitScale, mInitScale, width / 2, height / 2);
setImageMatrix(mMatrix);
mOnce = true;
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
private float getScale() {
float[] values = new float[9];
mMatrix.getValues(values);
return values[Matrix.MSCALE_X];
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = detector.getScaleFactor();
float scale = getScale();
if (getDrawable() == null) {
return true;
}
if (scale < mMaxScale && scaleFactor > 1.0f ||
scale > mInitScale && scaleFactor < 1.0f) {
if (scale * scaleFactor < mInitScale) {
scaleFactor = mInitScale / scale;
}
if (scale * scaleFactor > mMaxScale) {
scaleFactor = mMaxScale / scale;
}
mMatrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
setImageViewInCenterWhenCheck();
setImageMatrix(mMatrix);
}
return true;
}
private RectF getMatrixRectF() {
Matrix mMatrix = this.mMatrix;
RectF rectF = new RectF();
Drawable d = getDrawable();
if (d != null) {
rectF.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
mMatrix.mapRect(rectF);
}
return rectF;
}
private void setImageViewInCenterWhenCheck() {
RectF matrixRectF = getMatrixRectF();
float postX = 0;
float postY = 0;
int width = getWidth();
int height = getHeight();
if (matrixRectF.width() >= width) {
if (matrixRectF.left > 0) {
postX = -matrixRectF.left;
}
if (matrixRectF.right < width) {
postX = width - matrixRectF.right;
}
}
if (matrixRectF.height() >= height) {
if (matrixRectF.top > 0) {
postY = -matrixRectF.top;
}
if (matrixRectF.bottom < height) {
postY = height - matrixRectF.bottom;
}
}
if (matrixRectF.width() < width) {
postX = width / 2 - matrixRectF.right + matrixRectF.width() / 2;
}
if (matrixRectF.height() < height) {
postY = height / 2 - matrixRectF.bottom + matrixRectF.height() / 2;
}
mMatrix.postTranslate(postX, postY);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)){
return true;
}
mScaleGestureDetector.onTouchEvent(event);
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 (mLastPointerCount != pointerCount) {
isCanDrag = false;
mLastX = x;
mLastY = y;
}
mLastPointerCount = pointerCount;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (getMatrixRectF().width()>getWidth()+0.01||getMatrixRectF().height()>getHeight()+0.01){
getParent().requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_MOVE:
if (getMatrixRectF().width()>getWidth()+0.01||getMatrixRectF().height()>getHeight()+0.01){
getParent().requestDisallowInterceptTouchEvent(true);
}
float dx = x - mLastX;
float dy = y - mLastY;
if (!isCanDrag) {
isCanDrag = isCanTouchMove(dx, dy);
}
if (isCanDrag) {
RectF matrixRectF = getMatrixRectF();
if (getDrawable() != null) {
isMoveLeftAndRight = isMoveTopAndBottom = true;
if (matrixRectF.width() < getWidth()) {
isMoveLeftAndRight = false;
dx = 0;
}
if (matrixRectF.height() < getHeight()) {
isMoveTopAndBottom = false;
dy = 0;
}
mMatrix.postTranslate(dx, dy);
WhenMoveCheck();
setImageMatrix(mMatrix);
}
}
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mLastPointerCount = 0;
break;
}
return true;
}
private void WhenMoveCheck() {
RectF matrixRectF = getMatrixRectF();
float dx = 0;
float dy = 0;
int width = getWidth();
int height = getHeight();
if (matrixRectF.top>0&&isMoveTopAndBottom){
dy = - matrixRectF.top;
}
if (matrixRectF.bottom<height&&isMoveTopAndBottom){
dy = height-matrixRectF.bottom;
}
if (matrixRectF.left>0&&isMoveLeftAndRight){
dx = -matrixRectF.left;
}
if (matrixRectF.right<width&& isMoveLeftAndRight){
dx = width-matrixRectF.right;
}
mMatrix.postTranslate(dx,dy);
setImageMatrix(mMatrix);
}
private boolean isCanTouchMove(float dx, float dy) {
return Math.sqrt(dx * dx + dy * dy) > mTouchSlop;
}
private class AutoScaleRunnable implements Runnable{
private float mTargetScale;
private float x;
private float y;
private final float BIGGER = 1.05F;
private final float SMALL = 0.95F;
private float tmpScale;
public AutoScaleRunnable(float targetScale,float x,float y){
this.mTargetScale = targetScale;
this.x = x;
this.y = y;
if (getScale()<mTargetScale){
tmpScale = BIGGER;
}
if (getScale()>mTargetScale){
tmpScale = SMALL;
}
}
@Override
public void run() {
mMatrix.postScale(tmpScale,tmpScale,x,y);
setImageViewInCenterWhenCheck();
setImageMatrix(mMatrix);
float currentScale = getScale();
if (tmpScale>1.0f&¤tScale <mTargetScale || tmpScale<1.0f && currentScale>mTargetScale){
postDelayed(this,15);
}else {
float scale = mTargetScale/currentScale;
mMatrix.postScale(scale,scale,x,y);
setImageViewInCenterWhenCheck();
setImageMatrix(mMatrix);
isAutoScale = false;
}
}
}
}
因为在viewPager中如不处理就无法进行对放大的图片进行点击移动的效果。。。
上述代码中:
if (getMatrixRectF().width()>getWidth()+0.01
||getMatrixRectF().height()>getHeight()+0.01){
getParent().requestDisallowInterceptTouchEvent(true);
}
正是处理这个冲突事件,只是先处理ImageView的放大缩小事件,最后可以进行滑动