Android, help rotating image on touch

烈酒焚心 提交于 2019-12-03 04:38:34

问题


I am trying to rotate one of the transparent PNGs on this image. The number part is what I want to rotate. I am able to do this, but it is not what I am trying to achieve

I want to rotate the numbers like on a real combination lock. So the user will touch and move their finger in a circle. I looked at less precise image rotation on touch/move events, and they were not sufficient.

this is currently my code

 public boolean onTouch(View v, MotionEvent event) {
    double r=Math.atan2(event.getX()-lockNumbers.getWidth(), lockNumbers.getHeight()-event.getY());
    int rotation=(int)Math.toDegrees(r);
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            x=event.getX();
            y=event.getY();
            updateRotation(rotation);
            break;
        case MotionEvent.ACTION_UP:
            break;
    }//switch       

    return true;

}//onTouch
private void updateRotation(double rot){
    float newRot=new Float(rot);
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.numbers);
    Matrix matrix=new Matrix();
    matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
    if(y>250){
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        lockNumbers.setImageBitmap(reDrawnBitmap);
    }
    else{
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        lockNumbers.setImageBitmap(reDrawnBitmap);
    }
}

it also resizes the bitmap when you touch it because of the matrix parameter. This is not the desired effect.

The user will pretty much be required to go in a circle with their finger.


回答1:


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




回答2:


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));



回答3:


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



来源:https://stackoverflow.com/questions/11405466/android-help-rotating-image-on-touch

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