天天看點

android圖檔縮放、放大demo

示例

原圖:

android圖檔縮放、放大demo

拖動檢視其他部分:

android圖檔縮放、放大demo
android圖檔縮放、放大demo

放大仔細檢視

android圖檔縮放、放大demo

代碼示例,注釋比較詳細,其他不多講了

package com.study.drascale;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity {

	ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageView = (ImageView) findViewById(R.id.img);
        imageView.setOnTouchListener(new TounchListener());
    }

    private class TounchListener implements OnTouchListener{
    	
    	private PointF startPoint = new PointF();
    	private Matrix matrix = new Matrix();
    	private Matrix currentMaritx = new Matrix();
    	
    	private int mode = 0;//用于标記模式
    	private static final int DRAG = 1;//拖動
    	private static final int ZOOM = 2;//放大
    	private float startDis = 0;
    	private PointF midPoint;//中心點

		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				mode = DRAG;
				currentMaritx.set(imageView.getImageMatrix());//記錄ImageView當期的移動位置
				startPoint.set(event.getX(),event.getY());//開始點
				break;

			case MotionEvent.ACTION_MOVE://移動事件
				if (mode == DRAG) {//圖檔拖動事件
					float dx = event.getX() - startPoint.x;//x軸移動距離
					float dy = event.getY() - startPoint.y;
					matrix.set(currentMaritx);//在目前的位置基礎上移動
					matrix.postTranslate(dx, dy);
					
				} else if(mode == ZOOM){//圖檔放大事件
					float endDis = distance(event);//結束距離
					if(endDis > 10f){
						float scale = endDis / startDis;//放大倍數
						matrix.set(currentMaritx);
						matrix.postScale(scale, scale, midPoint.x, midPoint.y);
					}
					
					
				}

				break;
				
			case MotionEvent.ACTION_UP:
				mode = 0;
				break;
			//有手指離開螢幕,但螢幕還有觸點(手指)
			case MotionEvent.ACTION_POINTER_UP:
				mode = 0;
				break;
			//當螢幕上已經有觸點(手指),再有一個手指壓下螢幕
			case MotionEvent.ACTION_POINTER_DOWN:
				mode = ZOOM;
				startDis = distance(event);
				
				if(startDis > 10f){//避免手指上有兩個繭
					midPoint = mid(event);
					currentMaritx.set(imageView.getImageMatrix());//記錄目前的縮放倍數
				}
				
				break;


			}
			imageView.setImageMatrix(matrix);
			return true;
		}
    	
    }

    /**
     * 兩點之間的距離
     * @param event
     * @return
     */
    private static float distance(MotionEvent event){
    	//兩根線的距離
    	float dx = event.getX(1) - event.getX(0);
    	float dy = event.getY(1) - event.getY(0);
    	return FloatMath.sqrt(dx*dx + dy*dy);
    }
    /**
     * 計算兩點之間中心點的距離
     * @param event
     * @return
     */
    private static PointF mid(MotionEvent event){
    	float midx = event.getX(1) + event.getX(0);
    	float midy = event.getY(1) - event.getY(0);
    	
    	return new PointF(midx/2, midy/2);
    }
}
           

示例下載下傳:

http://download.csdn.net/detail/sdefzhpk/4542576