图片的拖动与释放

原理图:

1)


2)

图片的拖动与释放_第1张图片


代码如下:

1、在drawable目录下导入要演示的图片


2、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:orientation="vertical" >
    
    <ImageView 
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/a"
        />

</LinearLayout>


3、MainActivity

package com.njupt.drag_scale1;

import android.app.Activity;
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.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	
	private ImageView iv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        iv = (ImageView) findViewById(R.id.iv);
        
        iv.setOnTouchListener(new MyOnTouchListener());
    }
    
    private final class MyOnTouchListener implements OnTouchListener{
    	
    	private PointF startPoint = new PointF();//初始点
    	private Matrix mCurrentMatrix = new Matrix();//图片初始的matrix值
    	private Matrix mMatrix = new Matrix();//来一个临时的matrix
    	
    	private PointF midPointF;//中心点
    	private float midDistance;//两点间的距离
    	
    	private int type = 0;
    	private final static int DRAG = 1;
    	private final static int SCALE = 2;
        
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			int action = event.getAction()&MotionEvent.ACTION_MASK;
			switch (action) {
			case MotionEvent.ACTION_DOWN://手指按下
				float x = event.getX();
				float y = event.getY();
				startPoint.set(x, y);
				mCurrentMatrix.set(iv.getImageMatrix());//图片没有移动的位置
				type = DRAG;
				break;
			case MotionEvent.ACTION_POINTER_1_DOWN://屏幕上有一根手指,再按下一根手指
				mCurrentMatrix.set(iv.getImageMatrix());//图片没有缩放的大小
				midPointF = getMidPointF(event);
				midDistance = getMidDistance(event);
				type = SCALE;
				break;
			case MotionEvent.ACTION_MOVE://手指移动
				if(type == DRAG){
					mMatrix.set(mCurrentMatrix);
					float dx = event.getX() - startPoint.x;
					float dy = event.getY() - startPoint.y;
					mMatrix.postTranslate(dx, dy);
				}else if(type == SCALE){
					mMatrix.set(mCurrentMatrix);
					float distance = getMidDistance(event);//得到当前两个手指间的距离
					float sx = distance/midDistance;//得到缩放的倍数
					mMatrix.postScale(sx, sx, midPointF.x, midPointF.y);
				}
				break;
			case MotionEvent.ACTION_UP://手指弹起
			case MotionEvent.ACTION_POINTER_1_UP://弹起一根手指,屏幕上还有一根手指
				type = 0;
				break;

			default:
				break;
			}
			
			iv.setImageMatrix(mMatrix);
			return true;
		}
		

    	
    }
    
    //得到中心点
	private PointF getMidPointF(MotionEvent event){
		float x = (event.getX(1) + event.getX(0))/2;
		float y = (event.getY(1) + event.getY(0))/2;
		return new PointF(x, y);
	}
	
	//计算两点间的距离
	private float getMidDistance(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);
	}
}


你可能感兴趣的:(图片的拖动与释放)