Bitmap旋转和缩放

MainActivity如下:

package cn.testtouchscreentomoveimage;

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;

/**

 * Demo描述:

 * 实现图片的拖动和缩放

 * 

 * 参考资料:

 * 1 http://blog.csdn.net/lfdfhl/article/details/8248848

 * 2 http://blog.csdn.net/leverage_1229/article/details/9009931

 * 

 * 注意事项:

 * 在该Demo中对于ImageView的设置

 * android:layout_width="match_parent"

 * android:layout_height="match_parent"

 * 是不太合理的,在具体项目中应调整

 */



public class MainActivity extends Activity {

    private ImageView mImageView;

    // 开始点

    private PointF mStartPoinF ;

    // 图片位置的变换矩阵

    private Matrix mMatrix;

    // 图片当前矩阵

    private Matrix mCurrentMatrix;

    // 模式参数

    private int current_mode = 0;

    // 拖拉模式

    private static final int drag_mode = 1;

    // 缩放模式

    private static final int zoom_mode = 2;

    // 开启缩放的阈值

    private static final float ZOOM_THRESHOLD = 10.0f;

    // 缩放前两指间的距离

    private float before_move_twoFingers_distance;

    // 两指移动后两者间的距离

    private float after_move_twoFingers_distance;

    // 两指间中心点

    private PointF mMiddlePointF;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        init();

    }



	private void init() {

		mStartPoinF = new PointF();

		mMatrix = new Matrix();

		mCurrentMatrix = new Matrix();

		mImageView = (ImageView) findViewById(R.id.imageView);

		mImageView.setOnTouchListener(new TouchListenerImpl());

	}



	private class TouchListenerImpl implements OnTouchListener {

		@Override

		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction() & MotionEvent.ACTION_MASK) {

			case MotionEvent.ACTION_DOWN:

				current_mode = drag_mode;

				// 记录图片当前matrix

				mCurrentMatrix.set(mImageView.getImageMatrix());

				// 记录开始坐标

				mStartPoinF.set(event.getX(), event.getY());

				break;



			// 当屏幕上已经有触点(手指),再有手指按下时触发该事件

			case MotionEvent.ACTION_POINTER_DOWN:

				current_mode = zoom_mode;

				before_move_twoFingers_distance = getTwoPointsDistance(event);

				if (before_move_twoFingers_distance > ZOOM_THRESHOLD) {

					// 计算两触点的中心点

					mMiddlePointF = getMiddlePoint(event);

					// 记录图片当前matrix

					mCurrentMatrix.set(mImageView.getImageMatrix());

				}

				break;



			case MotionEvent.ACTION_MOVE:

				//处理图片的拖动

				if (current_mode == drag_mode) {

					// 获取X轴移动距离

					float distanceX = event.getX() - mStartPoinF.x;

					// 获取Y轴移动距离

					float distanceY = event.getY() - mStartPoinF.y;

					// 在mCurrentMatrix的基础上平移图片

					mMatrix.set(mCurrentMatrix);

					mMatrix.postTranslate(distanceX, distanceY);

					

				} 

				//处理图片的缩放

				if (current_mode == zoom_mode) {

					after_move_twoFingers_distance = getTwoPointsDistance(event);

					if (after_move_twoFingers_distance > ZOOM_THRESHOLD) {

						// 计算缩放比例

						float scale = after_move_twoFingers_distance / before_move_twoFingers_distance;

						mMatrix.set(mCurrentMatrix);

						// 依据缩放比例和中心点进行缩放

						mMatrix.postScale(scale, scale, mMiddlePointF.x,mMiddlePointF.y);

					}

				}

				break;



			case MotionEvent.ACTION_UP:

			// 当手指离开屏幕,但屏幕上仍有其他触点(手指)时触发该事件

			case MotionEvent.ACTION_POINTER_UP:

				current_mode = 0;

				break;

			}

			// 为ImageView设置matrix

			mImageView.setImageMatrix(mMatrix);

			return true;

		}



	}

    

     //计算两点之间的距离

    public static float getTwoPointsDistance(MotionEvent event) {

        float disX = event.getX(1) - event.getX(0);

        float disY = event.getY(1) - event.getY(0);

        return FloatMath.sqrt(disX * disX + disY * disY);

    }

   

     //计算两点之间的中间点

    public static PointF getMiddlePoint(MotionEvent event) {

        float midX = (event.getX(0) + event.getX(1)) /2;

        float midY = (event.getY(0) + event.getY(1)) /2;

        return new PointF(midX, midY);

    }

}


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" >



    <ImageView

       android:id="@+id/imageView"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:src="@drawable/e"

       android:scaleType="matrix" 

    />



</LinearLayout>


 

 

你可能感兴趣的:(bitmap)