在 android 中基于 onTouchListener 旋转包含贴纸的布局
Rotate layout containing a sticker based on onTouchListener in android
我已经尝试了一个多星期了,但我无法根据触摸正确旋转布局(相对)。我需要设置旋转角度,以便当我在布局的左下角(有一个小图像)移动手指时布局旋转。基本上我有一个带有贴纸图像的相对布局。我可以通过触摸布局的相应角来拖动和缩放贴纸(图片),但我无法正确旋转它。谁能帮我设置一下旋转角度,这样整个布局就可以顺畅旋转了?
这是代码片段
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastTouchedScaleXPosition = event.getX();
lastTouchedScaleYPosition = event.getY();
if(previousXPosition == 0 && previousYPosition == 0){
int[] viewLocation = new int[2];
v.getLocationOnScreen(viewLocation);
previousXPosition= viewLocation[0];
previousYPosition= viewLocation[1];
}
break;
}
case MotionEvent.ACTION_MOVE: {
currentTouchedXPosition = event.getX() - lastTouchedScaleXPosition;
currentTouchedYPosition = event.getY() - lastTouchedScaleYPosition;
currentXPosition = previousXPosition + currentTouchedXPosition;
currentYPosition = previousYPosition + currentTouchedYPosition;
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.parent);
int w = mainLayout.getWidth();
int h = mainLayout.getHeight();
float center_x = 0;
float center_y = 0;
center_x += w/2;
center_y += h/2;
double tx = currentXPosition - center_x, ty = currentYPosition - center_y;
double t_length = Math.sqrt(tx*tx + ty*ty);
double a = Math.toDegrees(Math.acos(ty / t_length));
mainLayout.setRotation((float) a);
mainLayout.setTranslationX((w - h) / 2);
mainLayout.setTranslationY((h - w) / 2);
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mainLayout.getLayoutParams();
lp.height = w;
lp.width = h;
parent.setLayoutParams(lp);
previousXPosition = currentXPosition;
previousYPosition = currentYPosition;
break;
}
}
return true;
}
`
尝试了很多但没有得到想要的 result.This link 帮我解决了这个问题。
rotate and resize the image view with single finger in android
来源linkhttps://github.com/ryanch741/android-view-rotate-zoom-single-finger
我已经尝试了一个多星期了,但我无法根据触摸正确旋转布局(相对)。我需要设置旋转角度,以便当我在布局的左下角(有一个小图像)移动手指时布局旋转。基本上我有一个带有贴纸图像的相对布局。我可以通过触摸布局的相应角来拖动和缩放贴纸(图片),但我无法正确旋转它。谁能帮我设置一下旋转角度,这样整个布局就可以顺畅旋转了?
这是代码片段
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastTouchedScaleXPosition = event.getX();
lastTouchedScaleYPosition = event.getY();
if(previousXPosition == 0 && previousYPosition == 0){
int[] viewLocation = new int[2];
v.getLocationOnScreen(viewLocation);
previousXPosition= viewLocation[0];
previousYPosition= viewLocation[1];
}
break;
}
case MotionEvent.ACTION_MOVE: {
currentTouchedXPosition = event.getX() - lastTouchedScaleXPosition;
currentTouchedYPosition = event.getY() - lastTouchedScaleYPosition;
currentXPosition = previousXPosition + currentTouchedXPosition;
currentYPosition = previousYPosition + currentTouchedYPosition;
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.parent);
int w = mainLayout.getWidth();
int h = mainLayout.getHeight();
float center_x = 0;
float center_y = 0;
center_x += w/2;
center_y += h/2;
double tx = currentXPosition - center_x, ty = currentYPosition - center_y;
double t_length = Math.sqrt(tx*tx + ty*ty);
double a = Math.toDegrees(Math.acos(ty / t_length));
mainLayout.setRotation((float) a);
mainLayout.setTranslationX((w - h) / 2);
mainLayout.setTranslationY((h - w) / 2);
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mainLayout.getLayoutParams();
lp.height = w;
lp.width = h;
parent.setLayoutParams(lp);
previousXPosition = currentXPosition;
previousYPosition = currentYPosition;
break;
}
}
return true;
}
`
尝试了很多但没有得到想要的 result.This link 帮我解决了这个问题。 rotate and resize the image view with single finger in android
来源linkhttps://github.com/ryanch741/android-view-rotate-zoom-single-finger