Code example for MotionEvent

Methods: getActiongetActionMaskedgetPointerCountgetXgetY

     * pointers and rotates the image accordingly.  As the user 
     * rotates their fingers, the image will follow. 
    private boolean doRotationEvent(MotionEvent event) {
        //Calculate the angle between the two fingers 
        float deltaX = event.getX(0) - event.getX(1);
        float deltaY = event.getY(0) - event.getY(1);
        double radians = Math.atan(deltaY / deltaX);
        //Convert to degrees 
        int degrees = (int)(radians * 180 / Math.PI);
         * Must use getActionMasked() for switching to pick up pointer events. 
         * These events have the pointer index encoded in them so the return 
         * from getAction() won't match the exact action constant. 
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
            case MotionEvent.ACTION_POINTER_UP:
                //Mark the initial angle 
                mLastAngle = degrees;
            case MotionEvent.ACTION_MOVE:
                // ATAN returns a converted value between -90deg and +90deg 
                // which creates a point when two fingers are vertical where the 
                // angle flips sign.  We handle this case by rotating a small amount 
                // (5 degrees) in the direction we were traveling 
                if ((degrees - mLastAngle) > 45) {
                    //Going CCW across the boundary 
                    mImageMatrix.postRotate(-5, mPivotX, mPivotY);
                } else if ((degrees - mLastAngle) < -45) {
                    //Going CW across the boundary 
                    mImageMatrix.postRotate(5, mPivotX, mPivotY);
                } else { 
                    //Normal rotation, rotate the difference 
                    mImageMatrix.postRotate(degrees - mLastAngle, mPivotX, mPivotY);
                //Post the rotation to the image 
                //Save the current angle 
                mLastAngle = degrees;
        return true; 
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // We don't care about this event directly, but we declare 
            // interest so we can get later multi-touch events. 
            return true; 
        switch (event.getPointerCount()) {
            case 3: 
                // With three fingers down, zoom the image 
                // using the ScaleGestureDetector 
                return mScaleDetector.onTouchEvent(event);
            case 2: 
Stop searching for code, let great code find you!  Add Codota to your java IDE