Android, help rotating image on touch

[亡魂溺海] 提交于 2019-12-02 18:55:38

Write below code into your touch event.

switch (event.getAction()) {    
    case MotionEvent.ACTION_DOWN:
        // reset the touched quadrants
        for (int i = 0; i < quadrantTouched.length; i++) {
            quadrantTouched[i] = false;
        }
        allowRotating = false;
        startAngle = getAngle(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_MOVE:
        double currentAngle = getAngle(event.getX(), event.getY());
        rotateDialer((float) (startAngle - currentAngle));
        startAngle = currentAngle;
        break;
    case MotionEvent.ACTION_UP:
        allowRotating = true;
        break;
}

// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2), dialerHeight - event.getY() - (dialerHeight / 2))] = true;
detector.onTouchEvent(event);
return true;

And use below link for more reference.

Rotate Dialer Example

dharam

By accepting @Dipak Keshariya's answer i am putting way to to get the angle rotation which can help you identify selected part of wheel.

private float copy[] = new float[9];
    matrix.getValues(copy);
float wheelAngle = Math.round(Math.atan2(copy[Matrix.MSKEW_X],
                copy[Matrix.MSCALE_X]) * (180 / Math.PI));
Anand M Joseph

I presume you want to rotate an image at the point where a user touches the screen? If so, extend the SimpleOnGestureListener like this example:

public class MyGestureDetector extends SimpleOnGestureListener
{  
     @Override
     public void onLongPress(MotionEvent event)
     {
         int X = (int)event.getX();          
         int Y = (int)event.getY();

         ...Rotate the image
     }
}

Once you've got the screen coordinates of the touch event, you could apply a Rotation Animation about the point - see here for more details: http://developer.android.com/guide/topics/graphics/2d-graphics.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!