Android, help to rotate the image when touched

I am trying to rotate one of the transparent PNGs in this image. The digital part is what I want to rotate. I can do it, but thatโ€™s not what I'm trying to achieve.

I want to rotate numbers, like on a real combination lock. Thus, the user will touch and move his finger in a circle. I looked at less accurate image rotation on touch / move events, and there werenโ€™t enough of them.

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 it is touched due to the matrix parameter. This is not the desired effect.

The user will largely need to walk in a circle with a finger.

+6
source share
3 answers

Enter the code below into the 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 the link below for more help.

Rotate dialing example

+7
source

Accepting @Dipak Keshariya's answer , I set myself the task of getting a corner that could help you identify the selected part of the 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)); 
+1
source

I assume you want to rotate the image at the point where the user touches the screen? If yes, extend SimpleOnGestureListener as follows:

 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 have the screen coordinates of the touch event, you can apply the rotation animation around the point - see here for more details: http://developer.android.com/guide/topics/graphics/2d-graphics.html

0
source

Source: https://habr.com/ru/post/920091/


All Articles