Android app-image dragging and Scaling

Source: Internet
Author: User
Tags gety

Public class MainActivity extends Activity {private ImageView operImage; private PointF point = new PointF (); // record the position of the finger private PointF midPoint = new PointF (); // record the position of the midpoint between fingers private Matrix matrix = new Matrix (); // record the matrixprivate Matrix newMatrix = new Matrix () pressed by the finger before dragging and scaling (); // record the matrixprivate int type that is being dragged and scaled by fingers = 0; // operation type private float pointerDistance; // The distance between the two fingers is private static final int DRAG = 1; // drag private static final int SCALE = 2; // SCALE @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); operImage = (ImageView) findViewById (R. id. before); operImage. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {int action = event. getAction () & MotionEvent. ACTION_MASK; switch (action) {case MotionEvent. ACTION_DOWN: float startX = event. getX (); float startY = event. getY (); // record the point of the finger. set (startX, startY); // obtain the position matrix of the current image. set (operImage. getImageMatrix (); type = DRAG; // DRAG break; case MotionEvent. action_pointer_resize down: // records the image size before scaling. set (operImage. getImageMatrix (); // record the distance between two fingers pointerDistance = getDistance (event); // record the midPoint of the distance between the two fingers = getMidPoint (event); type = SCALE; // scale break; case MotionEvent. ACTION_MOVE: switch (type) {case DRAG: // DRAG float newX = event. getX (); float newY = event. getY (); newMatrix. set (matrix); float dx = newX-point. x; float dy = newY-point. y; newMatrix. postTranslate (dx, dy); break; case SCALE: // SCALE newMatrix. set (matrix); float nowDistance = getDistance (event ); // calculate the ratio of float scale = nowDistance/pointerDistance based on the distance between the two fingers at this time divided by the distance between the two fingers at the beginning; // scale newMatrix according to the center point. postScale (scale, scale, midPoint. x, midPoint. y); break; default: break;} break; case MotionEvent. ACTION_UP: type = 0; break; case MotionEvent. action_pointer_reply up: type = 0; break;} operImage. setImageMatrix (newMatrix); return true ;}});}/** calculate the midpoint of two fingers */protected PointF getMidPoint (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);}/** calculate the distance between two points */protected float getDistance (MotionEvent event) {float x = event. getX (0)-event. getX (1); float y = event. getY (0)-event. getY (1); return FloatMath. sqrt (x * x + y * y);} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.