Android 图片拖动和缩放

今天下载传智播客黎活明老师的Android视频看了图片拖动和缩放,放上去以后学习用。

package com.example.test_pictrue_move_drag;

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

public class MainActivity extends Activity {

	private ImageView imageView ;
	
	private PointF startPoint = new PointF();
	private Matrix matrix = new Matrix() ;
	private Matrix saveCurrentMatrix = new Matrix();
	
	// 初始状态  
	private  final static int NONE = 0;
	// 拖动  
	private  final static int DRAG = 1;
	// 缩放  
	private  final static int ZOOM = 2;
	// 记录当前模式
	private int MODE = NONE ;
	
	// 计算两点之间的距离
	private float startDistance ;
	// 计算两点之间的中心点
	private PointF midPoint ;
	
	
	// 
	private Bitmap bitmap ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		imageView = (ImageView) findViewById(R.id.imageView);
		
		imageView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction() & MotionEvent.ACTION_MASK) {
				case MotionEvent.ACTION_DOWN: // 手指按下屏幕
					
					saveCurrentMatrix.set(imageView.getImageMatrix()); // 记录imageView的当前移动位置
					startPoint.set(event.getX(), event.getY()); // 记录开始按下的点坐标
					MODE = DRAG ;
					
					break;
					
				case MotionEvent.ACTION_POINTER_DOWN: // 屏幕上已经有一个触点,再有一个手指按下屏幕
					MODE = ZOOM ;
					startDistance = distance(event);
					if(startDistance > 10f){
						midPoint = mid(event);
						saveCurrentMatrix.set(imageView.getImageMatrix());//记录ImageView当前的缩放倍数
					}
					break ;

				case MotionEvent.ACTION_MOVE:
					if(MODE == DRAG ){
						// 移动距离
						float dx = event.getX() - startPoint.x;
						float dy = event.getY() - startPoint.y;
						
						matrix.set(saveCurrentMatrix); //在上一次移动位置的基础上进行移动
						// 进行拖动
						matrix.postTranslate(dx, dy);
						
					} else if (MODE == ZOOM){
						float endDis = distance(event);//结束距离
						if(endDis > 10f){
							float scale = endDis / startDistance;//得到缩放倍数
							matrix.set(saveCurrentMatrix);//在上一次缩放位置的基础上进行缩放
							matrix.postScale(scale, scale, midPoint.x, midPoint.y);
						}
					}
					
					break ;
					
				case MotionEvent.ACTION_UP:// 手指离开屏幕
				case MotionEvent.ACTION_POINTER_UP: // 有手指离开屏幕,但还屏幕还有触点
					MODE = NONE ;
					
					break;
					
				}
				
				imageView.setImageMatrix(matrix);
				return true;
			}
		});
		
//		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_20150119100930);
		
	}

	/**
     * 计算两点之间的距离
     * @param event
     * @return
     */
	public 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
	 */
	public static PointF mid(MotionEvent event){
		float midX = (event.getX(1) + event.getX(0)) / 2;
		float midY = (event.getY(1) + event.getY(0)) / 2;
		return new PointF(midX, midY);
	}
	
//	/** 
//     * 横向、纵向居中 
//     */  
//    protected void center(boolean horizontal, boolean vertical) { 
//        Matrix m = new Matrix();  
//        m.set(matrix);  
//        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());  
//        m.mapRect(rect);  
//        float height = rect.height();  
//        float width = rect.width();  
//        float deltaX = 0, deltaY = 0;  
//        if (vertical) {  
//            // 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下放留空则往下移  
//            int screenHeight = getResources().getDisplayMetrics().heightPixels;  
//            if (height < screenHeight) {  
//                deltaY = (screenHeight - height) / 2 - rect.top;  
//            } else if (rect.top > 0) {  
//                deltaY = -rect.top;  
//            } else if (rect.bottom < screenHeight) {  
//                deltaY = imageView.getHeight() - rect.bottom;  
//            }  
//        }  
//        if (horizontal) {  
//            int screenWidth = getResources().getDisplayMetrics().widthPixels;  
//            if (width < screenWidth) {  
//                deltaX = (screenWidth - width) / 2 - rect.left;  
//            } else if (rect.left > 0) {  
//                deltaX = -rect.left;  
//            } else if (rect.right < screenWidth) {  
//                deltaX = screenWidth - rect.right;  
//            }  
//        }  
//        matrix.postTranslate(deltaX, deltaY);  
//    }  
}

 

    xml



      


 

   附件还有一个另一种拖动的实现方式

 

你可能感兴趣的:(Android,图片拖动缩放,Android,图片拖动,缩放)