ViewPager图片预览之图片的放大缩小,移动,切换(第四课)结束了

终极版

package com.zhy.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
/**
 *
 * @author zhy
 * 博客地址:http://blog.csdn.net/lmj623565791
 */
public class ZoomImageView extends ImageView implements OnScaleGestureListener,
		OnTouchListener, ViewTreeObserver.OnGlobalLayoutListener

{
	private static final String TAG = ZoomImageView.class.getSimpleName();
	public static final float SCALE_MAX = 4.0f;
	private static final float SCALE_MID = 2.0f;

	/**
	 * 初始化时的缩放比例,如果图片宽或高大于屏幕,此值将小于0
	 */
	private float initScale = 1.0f;
	private boolean once = true;

	/**
	 * 用于存放矩阵的9个值
	 */
	private final float[] matrixValues = new float[9];

	/**
	 * 缩放的手势检测
	 */
	private ScaleGestureDetector mScaleGestureDetector = null;
	private final Matrix mScaleMatrix = new Matrix();

	/**
	 * 用于双击检测
	 */
	private GestureDetector mGestureDetector;
	private boolean isAutoScale;

	private int mTouchSlop;

	private float mLastX;
	private float mLastY;

	private boolean isCanDrag;
	private int lastPointerCount;

	private boolean isCheckTopAndBottom = true;
	private boolean isCheckLeftAndRight = true;

	public ZoomImageView(Context context)
	{
		this(context, null);
	}

	public ZoomImageView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		super.setScaleType(ScaleType.MATRIX);
		mGestureDetector = new GestureDetector(context,
				new SimpleOnGestureListener()
				{
					@Override
					public boolean onDoubleTap(MotionEvent e)
					{
						if (isAutoScale == true)
							return true;

						float x = e.getX();
						float y = e.getY();
						Log.e("DoubleTap", getScale() + " , " + initScale);
						if (getScale() < SCALE_MID)
						{
							ZoomImageView.this.postDelayed(
									new AutoScaleRunnable(SCALE_MID, x, y), 16);
							isAutoScale = true;
						} else if (getScale() >= SCALE_MID
								&& getScale() < SCALE_MAX)
						{
							ZoomImageView.this.postDelayed(
									new AutoScaleRunnable(SCALE_MAX, x, y), 16);
							isAutoScale = true;
						} else
						{
							ZoomImageView.this.postDelayed(
									new AutoScaleRunnable(initScale, x, y), 16);
							isAutoScale = true;
						}

						return true;
					}
				});
		mScaleGestureDetector = new ScaleGestureDetector(context, this);
		this.setOnTouchListener(this);
	}

	/**
	 * 自动缩放的任务
	 *
	 * @author zhy
	 *
	 */
	private class AutoScaleRunnable implements Runnable
	{
		static final float BIGGER = 1.07f;
		static final float SMALLER = 0.93f;
		private float mTargetScale;
		private float tmpScale;

		/**
		 * 缩放的中心
		 */
		private float x;
		private float y;

		/**
		 * 传入目标缩放值,根据目标值与当前值,判断应该放大还是缩小
		 *
		 * @param targetScale
		 */
		public AutoScaleRunnable(float targetScale, float x, float y)
		{
			this.mTargetScale = targetScale;
			this.x = x;
			this.y = y;
			if (getScale() < mTargetScale)
			{
				tmpScale = BIGGER;
			} else
			{
				tmpScale = SMALLER;
			}

		}

		@Override
		public void run()
		{
			// 进行缩放
			mScaleMatrix.postScale(tmpScale, tmpScale, x, y);
			checkBorderAndCenterWhenScale();
			setImageMatrix(mScaleMatrix);

			final float currentScale = getScale();
			// 如果值在合法范围内,继续缩放
			if (((tmpScale > 1f) && (currentScale < mTargetScale))
					|| ((tmpScale < 1f) && (mTargetScale < currentScale)))
			{
				ZoomImageView.this.postDelayed(this, 16);
			} else
			// 设置为目标的缩放比例
			{
				final float deltaScale = mTargetScale / currentScale;
				mScaleMatrix.postScale(deltaScale, deltaScale, x, y);
				checkBorderAndCenterWhenScale();
				setImageMatrix(mScaleMatrix);
				isAutoScale = false;
			}

		}
	}

	@SuppressLint("NewApi")
	@Override
	public boolean onScale(ScaleGestureDetector detector)
	{
		float scale = getScale();
		float scaleFactor = detector.getScaleFactor();

		if (getDrawable() == null)
			return true;

		/**
		 * 缩放的范围控制
		 */
		if ((scale < SCALE_MAX && scaleFactor > 1.0f)
				|| (scale > initScale && scaleFactor < 1.0f))
		{
			/**
			 * 最大值最小值判断
			 */
			if (scaleFactor * scale < initScale)
			{
				scaleFactor = initScale / scale;
			}
			if (scaleFactor * scale > SCALE_MAX)
			{
				scaleFactor = SCALE_MAX / scale;
			}
			/**
			 * 设置缩放比例
			 */
			mScaleMatrix.postScale(scaleFactor, scaleFactor,
					detector.getFocusX(), detector.getFocusY());
			checkBorderAndCenterWhenScale();
			setImageMatrix(mScaleMatrix);
		}
		return true;

	}

	/**
	 * 在缩放时,进行图片显示范围的控制
	 */
	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 * 0.5f - rect.right + 0.5f * rect.width();
		}
		if (rect.height() < height)
		{
			deltaY = height * 0.5f - rect.bottom + 0.5f * rect.height();
		}
		Log.e(TAG, "deltaX = " + deltaX + " , deltaY = " + deltaY);

		mScaleMatrix.postTranslate(deltaX, deltaY);

	}

	/**
	 * 根据当前图片的Matrix获得图片的范围
	 *
	 * @return
	 */
	private RectF getMatrixRectF()
	{
		Matrix matrix = mScaleMatrix;
		RectF rect = new RectF();
		Drawable d = getDrawable();
		if (null != d)
		{
			rect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			matrix.mapRect(rect);
		}
		return rect;
	}

	@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, y = 0;
		// 拿到触摸点的个数
		final int pointerCount = event.getPointerCount();
		// 得到多个触摸点的x与y均值
		for (int i = 0; i < pointerCount; i++)
		{
			x += event.getX(i);
			y += event.getY(i);
		}
		x = x / pointerCount;
		y = y / pointerCount;

		/**
		 * 每当触摸点发生变化时,重置mLasX , mLastY
		 */
		if (pointerCount != lastPointerCount)
		{
			isCanDrag = false;
			mLastX = x;
			mLastY = y;
		}

		lastPointerCount = pointerCount;
		RectF rectF = getMatrixRectF();
		switch (event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			if (rectF.width() > getWidth() || rectF.height() > getHeight())
			{
				getParent().requestDisallowInterceptTouchEvent(true);
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (rectF.width() > getWidth() || rectF.height() > getHeight())
			{
				getParent().requestDisallowInterceptTouchEvent(true);
			}
			Log.e(TAG, "ACTION_MOVE");
			float dx = x - mLastX;
			float dy = y - mLastY;

			if (!isCanDrag)
			{
				isCanDrag = isCanDrag(dx, dy);
			}
			if (isCanDrag)
			{

				if (getDrawable() != null)
				{
					// if (getMatrixRectF().left == 0 && dx > 0)
					// {
					// getParent().requestDisallowInterceptTouchEvent(false);
					// }
					//
					// if (getMatrixRectF().right == getWidth() && dx < 0)
					// {
					// getParent().requestDisallowInterceptTouchEvent(false);
					// }
					isCheckLeftAndRight = isCheckTopAndBottom = true;
					// 如果宽度小于屏幕宽度,则禁止左右移动
					if (rectF.width() < getWidth())
					{
						dx = 0;
						isCheckLeftAndRight = false;
					}
					// 如果高度小雨屏幕高度,则禁止上下移动
					if (rectF.height() < getHeight())
					{
						dy = 0;
						isCheckTopAndBottom = false;
					}

					mScaleMatrix.postTranslate(dx, dy);
					checkMatrixBounds();
					setImageMatrix(mScaleMatrix);
				}
			}
			mLastX = x;
			mLastY = y;
			break;

		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			Log.e(TAG, "ACTION_UP");
			lastPointerCount = 0;
			break;
		}

		return true;
	}

	/**
	 * 获得当前的缩放比例
	 *
	 * @return
	 */
	public final float getScale()
	{
		mScaleMatrix.getValues(matrixValues);
		return matrixValues[Matrix.MSCALE_X];
	}

	@Override
	protected void onAttachedToWindow()
	{
		super.onAttachedToWindow();
		getViewTreeObserver().addOnGlobalLayoutListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onDetachedFromWindow()
	{
		super.onDetachedFromWindow();
		getViewTreeObserver().removeGlobalOnLayoutListener(this);
	}

	@Override
	public void onGlobalLayout()
	{
		if (once)
		{
			Drawable d = getDrawable();
			if (d == null)
				return;
			Log.e(TAG, d.getIntrinsicWidth() + " , " + d.getIntrinsicHeight());
			int width = getWidth();
			int height = getHeight();
			// 拿到图片的宽和高
			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 && dw <= width)
			{
				scale = height * 1.0f / dh;
			}
			// 如果宽和高都大于屏幕,则让其按按比例适应屏幕大小
			if (dw > width && dh > height)
			{
				scale = Math.min(width * 1.0f / dw, height * 1.0f / dh);
			}
			initScale = scale;

			Log.e(TAG, "initScale = " + initScale);
			mScaleMatrix.postTranslate((width - dw) / 2, (height - dh) / 2);
			mScaleMatrix.postScale(scale, scale, getWidth() / 2,
					getHeight() / 2);
			// 图片移动至屏幕中心
			setImageMatrix(mScaleMatrix);
			once = false;
		}

	}

	/**
	 * 移动时,进行边界判断,主要判断宽或高大于屏幕的
	 */
	private void checkMatrixBounds()
	{
		RectF rect = getMatrixRectF();

		float deltaX = 0, deltaY = 0;
		final float viewWidth = getWidth();
		final float viewHeight = getHeight();
		// 判断移动或缩放后,图片显示是否超出屏幕边界
		if (rect.top > 0 && isCheckTopAndBottom)
		{
			deltaY = -rect.top;
		}
		if (rect.bottom < viewHeight && isCheckTopAndBottom)
		{
			deltaY = viewHeight - rect.bottom;
		}
		if (rect.left > 0 && isCheckLeftAndRight)
		{
			deltaX = -rect.left;
		}
		if (rect.right < viewWidth && isCheckLeftAndRight)
		{
			deltaX = viewWidth - rect.right;
		}
		mScaleMatrix.postTranslate(deltaX, deltaY);
	}

	/**
	 * 是否是推动行为
	 *
	 * @param dx
	 * @param dy
	 * @return
	 */
	private boolean isCanDrag(float dx, float dy)
	{
		return Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
	}

}

布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.zhy.view.ZoomImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix"
        android:src="@drawable/tbug" />

</RelativeLayout>

vp.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

在activity中使用:

package com.zhy.zhy_scalegesturedetector02;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zhy.view.ZoomImageView;

public class MainActivity extends Activity
{
	private ViewPager mViewPager;
	private int[] mImgs = new int[] { R.drawable.tbug, R.drawable.a,
			R.drawable.xx };
	private ImageView[] mImageViews = new ImageView[mImgs.length];

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vp);

		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
		mViewPager.setAdapter(new PagerAdapter()
		{

			@Override
			public Object instantiateItem(ViewGroup container, int position)
			{
				ZoomImageView imageView = new ZoomImageView(
						getApplicationContext());
				imageView.setImageResource(mImgs[position]);
				container.addView(imageView);
				mImageViews[position] = imageView;
				return imageView;
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object)
			{
				container.removeView(mImageViews[position]);
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1)
			{
				return arg0 == arg1;
			}

			@Override
			public int getCount()
			{
				return mImgs.length;
			}
		});

	}
}
时间: 2024-10-10 20:27:38

ViewPager图片预览之图片的放大缩小,移动,切换(第四课)结束了的相关文章

ViewPager图片预览之图片的放大缩小,移动,切换(第三课)连载

第三课(第三步):支持以手指触控的任意点为中心开始缩放 关键部分是在缩放的时候不断进行边界检测,防止放大后缩小后出现白边: /** * 在缩放的时候进行边界控制范围位置控制 */ private void checkBorderAndCenterWhenScale() { // TODO Auto-generated method stub RectF rect = getMatrixRectF(); float deltaX = 0; float deltaY = 0; float width

jquery图片预览使图片在屏幕正中间。

<ul> <li><img src='image/u1.jpg'></li> <li><img src='image/u2.jpg'></li> <li><img src='image/u3.jpg'></li></ul><div class="bg"> <img class="bgImg" src="&qu

ViewPager图片预览之图片的放大缩小,移动,切换(第一课)连载

1,自由的放大和缩小 2.双击放大与缩小 3.放大以后可以进行自由的移动 4.处理与ViewPager之间的的事件冲突 需要用到的知识点 1.Matrix (图片放大,缩小需要用到矩阵) 2.ScaleGestureDetector(检测用户多指触控时缩放的手势) 3.GestureDetector:检测用户双击时需要做的一些处理 4.事件分发机制(当我们图片放大时,我们的图片是可以左右移动的,在ViewPager左右切换图片,两者会有冲突). -------------------------

ViewPager图片预览之图片的放大缩小,移动,切换(第二课)连载

第二步:给自定义控件添加支持手指触控缩放的功能:(支持手指触控放大) 因为涉及到手势触摸事件所以要实现OnScaleGestureListener,OnTouchListener这两个接口. 声明成员变量: private ScaleGestureDetector mScaleGestureDetector;//捕获用户多指触控缩放的比例 在构造函数中初始化: mScaleGestureDetector = new ScaleGestureDetector(context, this); set

js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > .katex-html { display: block; } .katex-display > .katex > .katex-html > .tag { position: absolute; right: 0px; } .katex { font-style: normal; font

本地与在线图片转Base64及图片预览

查看效果:http://sandbox.runjs.cn/show/tgvbo9nq 本地图片转Base64(从而可以预览图片): function localImgLoad() { var src = this.files[0]; var self = $(this); var read = new FileReader(); read.onload = function(e) { var html = "<img src=" + e.target.result + "

微信企业号办公系统-图片预览放大功能-previewImage

在微信里看过文章的应该知道,文章里的图片点击后可以放大.分享和保存. 然而自己在微信里开发的网页,里面的图片点击后没办法实现这个效果,然后就去看了下微信JS文档,里面有个previewImage可以调用. previewImage是微信客户端给内置浏览器增加的一个Javascript Interface,通过调用这个API,可以调起微信客户端提供的大图片查看组件. 官方说明和例子: wx.previewImage({ current: '', // 当前显示图片的http链接 urls: []

一款基于jQuery可放大预览的图片滑块插件

今天给大家分享一款基于jQuery可放大预览的图片滑块插件.这款jQuery焦点图插件的特点是可以横向左右滑动图片,并且点击图片可以进行放大预览,唯一的缺陷是这款焦点图并不能循环切换,如果你有较好的JavaScript功底,可以自己修改代码来实现这一功能.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id="slider"> <div class="spic"> <img src="images

简单的移动端图片预览 包含放大缩小以及对各种手势的判定

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/> <title>title&