In Android applications, image dragging, scaling, and rotating are used in many scenarios, what we need to do today is to implement a TouchView that can be dragged, scaled, or rotated Based on the ImageView.
I. Implementation Principle
OnTouch event Capture + Matrix transformation
Ii. Core Methods
Drag: Matrix. postTranslate (DeltalX, DeltalY );
Scaling: Matrix. postScale (mScale, mScale, mPoint. x, mPoint. y );
Rotation: Matrix. postRotate (Angle, mPoint. x, mPoint. y );
Iii. Implementation
Package com. android. touchView;/** Android multi-touch technical exercise * @ Author: Robin * @ Date: July 15, December 29, 2013 * @ boundary processing I don't know how to write it yet * The current problems are: * gesture recognition is not smooth, and it often fails to be scaled down. * The program may crash due to no boundary judgment */import android. annotation. suppressLint; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. graphics. paintFlagsDrawFilter; import android. graphics. pointF; import android. util. displayMetrics; import android. util. floatMath; import android. view. motionEvent; import android. widget. imageView; @ SuppressLint ({"ViewConstructor", "FloatMath"}) public class MultiTouchView extends ImageView {// local image resource private int mDrawable; // image Bitmap private Bitmap mBitmap; // screen width private int ScreenWidth; // screen height private int ScreenHeight; // original image Matrix private Matrix mMatrix = new Matrix (); // process image Matrix private Matrix mSavedMatrix = new Matrix (); // result image Matrix private Matrix mResultMatrix = new Matrix (); // defines three modes: none, Drag, Zoom public static final int Mode_None = 0; public static final int Mode_Drag = 1; public static final int Mode_Zoom = 2; // The current operation mode is private int mMode = Mode_None; // The current coordinates are private float mDownX and mDownY; // The distance between the two points is private float mDistance = 0f; // The storage Rotation Angle @ SuppressWarnings ("unused") private float mAngle = 0f; // store the midpoint private PointF mPoint; // maximum scaling ratio // private float MaxScale = 3f; // minimum scaling ratio // private float MinScale = 0.5f; public MultiTouchView (Activity mActivity, int Drawable) {super (mActivity); // sets the current image resource this. mDrawable = Drawable; // obtain BitmapmBitmap = BitmapFactory. decodeResource (getResources (), mDrawable); DisplayMetrics dm = new DisplayMetrics (); mActivity. getWindowManager (). getdefadisplay display (). getMetrics (dm); // obtain the screen width and height ScreenWidth = dm. widthPixels; ScreenHeight = dm. heightPixels; mMatrix = new Matrix () ;}@ SuppressLint ("DrawAllocation") @ Overrideprotected void onDraw (Canvas canvas) {// eliminate image sawtooth canvas. setDrawFilter (new PaintFlagsDrawFilter (0, Paint. ANTI_ALIAS_FLAG | Paint. FILTER_BITMAP_FLAG); canvas. save (); // draw the image canvas. drawBitmap (mBitmap, mMatrix, null); canvas. restore () ;}@ Overridepublic boolean onTouchEvent (MotionEvent Event) {switch (Event. getAction () {// single-touch processing case MotionEvent. ACTION_DOWN: // set the current operation mode to Drag mMode = Mode_Drag; // obtain the current coordinate mDownX = Event. getX (); mDownY = Event. getY (); mSavedMatrix. set (mMatrix); break; // multi-touch processing case MotionEvent. ACTION_POINTER_DOWN: mMode = Mode_Zoom; // obtain the distance between two points. mDistance = getDistance (Event); // obtain the Rotation Angle mAngle = getAngle (Event ); // obtain the mPoint = getMidPoint (Event); mSavedMatrix. set (mMatrix); break; case MotionEvent. ACTION_MOVE: // if (mMode = Mode_Zoom) {mResultMatrix. set (mSavedMatrix); // get the scaling ratio float mScale = getDistance (Event)/mDistance; // get the rotation Angle. You do not need to use // float Angle = getAngle (Event)-mAngle here; // The mResultMatrix is scaled at the center of the vertex. postScale (mScale, mScale, mPoint. x, mPoint. y); // rotate with the center of the midpoint. You do not need to use the // mResultMatrix. postRotate (Angle, mPoint. x, mPoint. y); mMatrix. set (mResultMatrix); invalidate ();} else if (mMode = Mode_Drag) // translation processing {mResultMatrix. set (mSavedMatrix); // calculates the translation volume float DeltalX = Event. getX ()-mDownX; float DeltalY = Event. getY ()-mDownY; // translate mResultMatrix. postTranslate (DeltalX, DeltalY); mMatrix. set (mResultMatrix); invalidate ();} break; case MotionEvent. ACTION_UP: // do you want to handle it here? What should I do if necessary? case MotionEvent. ACTION_POINTER_UP: mMode = Mode_None; break;} return true;} // returns the public float getDistance (MotionEvent Event) between two points {// calculates the float DeltalX = Event. getX (0)-Event. getX (1); // calculate the variation of Y float DeltalY = Event. getY (0)-Event. getY (1); // calculates the return FloatMath distance. sqrt (DeltalX * DeltalX + DeltalY * DeltalY);} // returns the midpoint of two points @ SuppressLint ("FloatMath") public PointF getMidPoint (MotionEvent Event) {float X = Event. getX (0) + Event. getX (1); float Y = Event. getY (0) + Event. getY (1); return new PointF (X/2, Y/2);} // obtain the Rotation Angle public float getAngle (MotionEvent Event) {double DeltalX = Event. getX (0)-Event. getX (1); double DeltalY = Event. getY (0)-Event. getY (1); return (float) Math. atan2 (DeltalX, DeltalY) ;}// boundary processing. Currently, no better public boolean CheckBounary () {return false ;}// store the current image public Bitmap SaveImage () {Bitmap mBitmap = Bitmap. createBitmap (ScreenWidth, ScreenHeight, Config. ARGB_8888); Canvas mCanvas = new Canvas (mBitmap); mCanvas. drawBitmap (mBitmap, mMatrix, null); mCanvas. save (Canvas. ALL_SAVE_FLAG); mCanvas. restore (); return mBitmap ;}}
Iv. Running Effect