Zoom-in and zoom-out of android Images

Source: Internet
Author: User

They are all examples of the past, but they also learn from others. Share with you the frequently used features




All the code. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> public class MainActivity extends Activity {private ImageView imageView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. imageView); imageView. setOnTouchListener (new OnTouchListener () {// set the start point private PointF startPoint = new PointF (); // set the conversion Matrix private matrix Matrix = new Matrix () of the image position (); // set the transformation Matrix private Matrix currentMatrix = new Matrix () for the current position of the image; // The initialization mode parameter private int mode = 0; // non-mode private static final int NONE = 0; // DRAG mode private static final int DRAG = 1; // scaling mode private static final int ZOOM = 2; // enable the threshold for scaling effect: private static final float ZOOM_THRESHOLD = 10.0f; // start from private float startDistance; // center point private PointF middlePoint; @ Override public boolean onTouch (View v, motionEvent) {switch (event. getAction () & MotionEvent. ACTION_MASK) {case MotionEvent. ACTION_DOWN: mode = DRAG; // record the current position of the image currentMatrix. set (imageView. getImageMatrix (); // record the start coordinate startPoint. set (event. getX (), event. getY (); break; // This event case MotionEvent is triggered when there are contacts (fingers) on the screen and another finger is pressed. ACTION_POINTER_DOWN: mode = ZOOM; // get the distance between two points: startDistance = getDistance (event); // if it is greater than if (startDistance> ZOOM_THRESHOLD) {// obtain the middle point of the two points middlePoint = getMiddlePoint (event); // record the current scaling ratio of the image currentMatrix. set (imageView. getImageMatrix ();} break; case MotionEvent. ACTION_MOVE: if (mode = DRAG) {// obtain the moving distance from the X axis to float distanceX = event. getX ()-startPoint. x; // obtain the moving distance from the Y axis to float distanceY = event. getY ()-startPoint. y; // move the matrix based on the last stop position. set (currentMatrix); // moves the matrix to the image position. postTranslate (distanceX, distanceY);} else if (mode = ZOOM) {// The end distance from float endDistance = getDistance (event); if (endDistance> ZOOM_THRESHOLD) {// float scale = endDistance/startDistance; matrix. set (currentMatrix); matrix. postScale (scale, scale, middlePoint. x, middlePoint. y) ;}} break; case MotionEvent. ACTION_UP: // This event case MotionEvent is triggered when the finger leaves the screen but there are other contacts on the screen. ACTION_POINTER_UP: mode = 0; break;} imageView. setImageMatrix (matrix); return true ;}});}/*** calculate the distance between two points */public static float getDistance (MotionEvent event) {// The second vertex x and y minus the first vertex x and y coordinate float disX = event. getX (1)-event. getX (0); float disY = event. getY (1)-event. getY (0); return FloatMath. sqrt (disX * disX + disY * disY);}/*** calculate the intermediate point between two points */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 );}}

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.