我什么我跟你的一样运行不了大神
package newer.com.imageviewkk;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
/**
* Created by Administrator on 2015-6-11.
*/
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 mScaleMatrix;
/**
* 捕获用户多点指控
*/
private ScaleGestureDetector mScaleGestureDetector;
public ZoomImageView(Context context) {
this(context, null);
}
public ZoomImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZoomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mScaleMatrix = new Matrix();
setScaleType(ScaleType.MATRIX);
mScaleGestureDetector = new ScaleGestureDetector(context, this);
setOnTouchListener(this);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
//注册
getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
//移除
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
/**
* 获取加载完成的图片
*/
@Override
public void onGlobalLayout() {
if (!mOnce) {
//得到控件的宽和高
int width = getWidth();
int height = getHeight();
//得到我们的图片,以及宽和高
Drawable d = getDrawable();
if (d == null) {
return;
}
int dw = d.getIntrinsicWidth();
int dh = d.getIntrinsicHeight();
//设置缩放值
float scale = 1.0f;
/**
* 如果图片的宽度大于控件宽度,但是高度高于小于控件宽度;我们将其缩小
*
*/
if (dw > width && dh < height) {
scale = width * 1.0f / dw;
}
/**
* 如果图片的宽度大于控件宽度,但是高度高于小于控件宽度;我们将其缩小
*
*/
if (dh > height && dh < height) {
scale = height * 1.0f / dh;
}
/**
* 如果图片的宽度大于控件宽度,但是高度高于小于控件宽度;我们将其缩小
*
*/
if ((dw > width && dh > height) || (dw < width && dh < height)) {
scale = Math.min(width * 1.0f / dw, height * 1.0f / dw);
}
/**
* 等到初始化的值比例
*/
mInitScale = scale;
mMaxScale = mInitScale * 4;
mMidScale = mInitScale * 2;
//将图片移动至控件的中心
int dx = getWidth() / 2 - dw / 2;
int dy = getHeight() / 2 - dh / 2;
mScaleMatrix.postTranslate(dx, dy);
//缩放图片
mScaleMatrix.postScale(mInitScale, mInitScale, width / 2, height / 2);
setImageMatrix(mScaleMatrix);
mOnce = true;
}
}
/**
* 获取当前图片的缩放值
*
* @return
*/
public float getScale() {
float[] values = new float[9];
mScaleMatrix.getValues(values);
return values[Matrix.MSCALE_X];
}
/**
* 缩放的区间
* initScale maxScale
* 缩放比例
*
* @param detector
* @return
*/
@Override
public boolean onScale(ScaleGestureDetector detector) {
//拿到缩放值
float scale = getScale();
float scaleFactor = detector.getScaleFactor();
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) {
scale = mMaxScale / scale;
}
//缩放
mScaleMatrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
checkBorderAndCenterWhenScale();
setImageMatrix(mScaleMatrix);
}
return true;
}
/**
* 获得图片放大缩小以后的宽和高,以及l,r,t,b
*
* @return
*/
private RectF getMatrixRectF() {
Matrix matrix = mScaleMatrix;
RectF rectF = new RectF();
Drawable d = getDrawable();
if (d != null) {
rectF.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
matrix.mapRect(rectF);
}
return rectF;
}
/**
* 在缩放的时候进行边界控件已及我们的位置控制
*/
private void checkBorderAndCenterWhenScale() {
RectF rect = getMatrixRectF();
float deltaX = 0;
float deltaY = 0;
int width = getWidth();
int height = getHeight();
//缩放时进行边界检测,防止出现白边
if (rect.width() >= width) {
if (rect.left >0){
deltaX = -rect.left;
}
if (rect.right <width){
deltaX = width - rect.right;
}
}
if (rect.height() >= height){
if (rect.top >0){
deltaY = -rect.top;
}
if (rect.bottom <height){
deltaY = height - rect.bottom;
}
}
//如果宽度或者高度小于控件的宽或者高;则让其居中
if (rect.width() <width){
deltaX = width /2f -rect.right + rect.width() /2f;
}
if (rect.height() < height){
deltaY = height /2f -rect.bottom +rect.height()/2f;
}
mScaleMatrix.postTranslate(deltaX,deltaY);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
//------------------------------------
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleGestureDetector.onTouchEvent(event);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<newer.com.imageviewkk.ZoomImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a"
android:scaleType="matrix"
></newer.com.imageviewkk.ZoomImageView>
</RelativeLayout>