[英]Android, help rotating image on touch
I am trying to rotate one of the transparent PNGs on this image. 我正在尝试旋转此图像上的一个透明PNG。 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. 用户几乎需要用手指进入一个圆圈。
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. 并使用以下链接以获取更多参考。
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. 通过接受@Dipak Keshariya的答案,我正在采取方法来获得角度旋转,这可以帮助您识别车轮的选定部分。
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));
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: 如果是这样,
SimpleOnGestureListener
像这个示例一样扩展SimpleOnGestureListener
:
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 获得触摸事件的屏幕坐标后,您可以应用关于该点的旋转动画 - 有关详细信息,请参阅此处: http : //developer.android.com/guide/topics/graphics/2d-graphics.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.