左右滑動瀏覽圖片,選中圖片放大,浮在左右圖片之上,圖片的放大縮小拖動,多指觸控


實現效果圖如下:





開始用viewpager 做,考慮到一屏顯示三個圖片,並且中間Item 要有選中效果和放大效果。

發現Gallery 更好做一點。

目前也有些問題

一。爲了實現選中Item 浮在上面,用了vGallery.setSpacing(-50);   設置Item 間距。用的負值。所以,拖動時,邊上過度的兩個Item 會看到重合疊加的情況。

如果一屏顯示四個,或者更多個Item ,就無法實現效果了。

二。選中Item 放大效果是通過  iv.setLayoutParams(new Gallery.LayoutParams(640,360));   實現,左右滑動時,Item 的放大縮小太突兀,沒有過場動畫。


代碼如下:


activity_main.xml   主佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >


    <Gallery
        android:id="@+id/gallery_user_manuals"
        android:layout_width="match_parent"
        android:layout_height="370px"
        android:layout_marginLeft="30px"
        android:layout_marginRight="30px" 
        android:layout_marginTop="30px"/>

    <TextView
        android:id="@+id/tv_browse_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10px"
        android:textColor="#ccffffff"
        android:textSize="24px" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30px"
        android:layout_marginRight="30px"
        android:layout_marginTop="10px"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_reduce_progress"
            android:layout_width="46px"
            android:layout_height="46px"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:background="@drawable/back"/>

        <com.tan.gesture.detector.view.CustomSeekBar
            android:id="@+id/sb_progress"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10px"
            android:layout_marginRight="10px"
            android:layout_weight="1"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:maxHeight="2px"
            android:progress="0"
            android:progressDrawable="@drawable/seekbar_po_sound"
            android:thumb="@drawable/slide_selected"
            android:thumbOffset="16px" />

        <Button
            android:id="@+id/btn_add_progress"
            android:layout_width="46px"
            android:layout_height="46px"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:background="@drawable/next"/>
    </LinearLayout>

</LinearLayout>

Gallery  的高度儘量寫死,如果寫wrapcontent  , 在切換Item 時,Gallery 的高度會有變化,正常和放大後的Gallery Params 不一樣,會出現短暫的,下面的拖動條控件,上移下移。

關於SeekBar  的樣式請參考自定義豎向SeekBar ,橫向SeekBar 樣式


MainActivity.java

package com.tan.gesture.detector;

import com.tan.gesture.detector.R;
import com.tan.gesture.detector.view.CustomSeekBar;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.content.Intent;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	private Gallery vGallery;
	private GalleryAdapter mAdapter;
	private int[] mImgs = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
			R.drawable.a, R.drawable.b, R.drawable.c,
			R.drawable.a, R.drawable.b, R.drawable.c, 
			R.drawable.a, R.drawable.b, R.drawable.c };

	private TextView vProgressText;
	private Button vReduceProgressBtn;
	private Button vAddProgressBtn;
	private CustomSeekBar vProgressSB;
	private int mCurPostion;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//隱藏標題欄
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		
		initView();
		setListener();
		
	}
	
	private void initView() {
		
		vGallery = (Gallery) findViewById(R.id.gallery_user_manuals);

		vProgressText = (TextView) findViewById(R.id.tv_browse_progress);
		vReduceProgressBtn = (Button) findViewById(R.id.btn_reduce_progress);
		vAddProgressBtn = (Button) findViewById(R.id.btn_add_progress);
		vProgressSB = (CustomSeekBar) findViewById(R.id.sb_progress);
		
		//初始默認值
		vProgressText.setText("1/" + mImgs.length);
		vProgressSB.setProgress(0);
		vProgressSB.setMax(mImgs.length - 1);
		
		mAdapter = new GalleryAdapter(this);
		vGallery.setAdapter(mAdapter);
		// 設置未選中項的透明度,顯示半透明。
		vGallery.setUnselectedAlpha(0.5f); 
		// 設置兩個Item 間的距離,這裏是負值。
		//爲了實現選中項浮在其他左右項的上面。覆蓋效果
		//但是這麼做,在拖動時,能看到右側即將顯示的兩個Item 是重疊的。效果不好。
		vGallery.setSpacing(-50);  
		//選中Item 監聽
		vGallery.setOnItemSelectedListener(galleryListener);
	}
	private void setListener() {

		vGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(MainActivity.this, DetailActivity.class);
				intent.putExtra("imageId", mImgs[arg2]);
				startActivity(intent);
			}
		});
		
		vReduceProgressBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				//注意邊界
				if (mCurPostion > 0) {
					mCurPostion--;
					//更新Gallery,Text,progressBar 進度
					updateProgress();
				}
				
			}
		});
		
		vAddProgressBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//注意邊界
				if (mCurPostion < (mImgs.length-1)) {
					mCurPostion++;
					//更新Gallery,Text,progressBar 進度
					updateProgress();
				}
				
			}
		});
		
		vProgressSB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				//這個方法很重要,用來區別,是用戶自己拖動進度條還是用setProgress(); 更新進度條。
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				//這個方法很重要,用來區別,是用戶自己拖動進度條還是用setProgress(); 更新進度條。
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				mCurPostion = arg1;
				//更新Gallery,Text,progressBar 進度
				updateProgress();
			}
		});
	}
	
	private class GalleryAdapter extends BaseAdapter {
		private Context context;
		private int selectItem;

		public GalleryAdapter(Context context) {
			this.context = context;
		}

		//供外部調用,動態設置當前選中項,並放大選中項。
		public void setSelectItem(int selectItem) {

			if (this.selectItem != selectItem) {
				this.selectItem = selectItem;
				notifyDataSetChanged();
			}
		}

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

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = null;
			if (convertView == null)
				iv = new ImageView(context);
			else
				iv = (ImageView) convertView;

			iv.setImageResource(mImgs[position % mImgs.length]);
			iv.setAdjustViewBounds(true);
			iv.setBackgroundColor(Color.alpha(1));

			
			//實現選中Item 放大的方法
			//設置iv.setLayoutParams(new Gallery.LayoutParams(640,360));  
			// 通過調整Params大小,實現正好一屏顯示三個。且看不到ITem ,setSpacing 重疊區
			if (selectItem == position) {
//				Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_scale_action); // 實現動畫效果
				iv.setLayoutParams(new Gallery.LayoutParams(640,360));  
//				iv.startAnimation(animation); // 選中時,這是設置的比較大
			} else {
				iv.setLayoutParams(new Gallery.LayoutParams(400,225));  
				// 未選中
			}

			return iv;
			
			
		}
	}

	private OnItemSelectedListener galleryListener = new OnItemSelectedListener() {
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
			//未了動態設置選中項並且放大
			mAdapter.setSelectItem(arg2);
			mCurPostion = arg2;
			//更新Gallery,Text,progressBar 進度
			updateProgress();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
	};

	//更新控件Progress
	private void updateProgress(){
		vGallery.setSelection(mCurPostion);
		vProgressText.setText((mCurPostion +1)+ "/" + mImgs.length); 
		vProgressSB.setProgress(mCurPostion);
	}
}


activity_detail.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:background="@drawable/bg">

    <com.tan.gesture.detector.view.ZoomImageView
        android:id="@+id/iv_iamge"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix" />

</RelativeLayout>

DetailActivity
package com.tan.gesture.detector;

import com.tan.gesture.detector.R;
import com.tan.gesture.detector.view.ZoomImageView;
import com.tan.gesture.detector.view.ZoomImageView.OnSingleClickCloseListener;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;

public class DetailActivity  extends Activity{

	private ZoomImageView vImage;
	private int mCurrentImage  = R.drawable.a; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		mCurrentImage = getIntent().getIntExtra("imageId", mCurrentImage);
		
		vImage = (ZoomImageView) findViewById(R.id.iv_iamge);
		vImage.setImageResource(mCurrentImage);
		
		//設置接口監聽
		vImage.setOnSingleClickCloseListener(new OnSingleClickCloseListener() {
			
			@Override
			public void close() {
				DetailActivity.this.finish();
			}
		});
	}
}

這個地方存在一個業務需求,單擊圖片時,關閉DetailActivity。 所以註冊了一個接口,並在ZoomImageView 的OnTouch 事件中調用。


ZoomImageView.java

package com.tan.gesture.detector.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;

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;

	//業務需求,單擊照片關閉Activity , 故定義接口,並在OnTouch 事件中調用。
	private OnSingleClickCloseListener onSingleClickCloseListener;
	
	public interface OnSingleClickCloseListener{
		void close();
	}
	
	public void  setOnSingleClickCloseListener( OnSingleClickCloseListener listener){
		onSingleClickCloseListener = listener;
	}
	
	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 onSingleTapUp(MotionEvent e) {
//						onSingleClickCloseListener.close();
//						Log.d("View","single click");
//						return false;
//					}
					//不用onSingleTapUp,因爲雙擊也會觸發此監聽。
					//爲了區別單擊和雙擊用onSingleTapConfirmed
					
					@Override
					public boolean onSingleTapConfirmed(MotionEvent e) {
						
						onSingleClickCloseListener.close();
						return true;
					}
					
					@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;
	}

}

圖片的放大縮小拖動,不懂的看鴻洋大神的文章

源碼


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章