我已经尝试了一个多星期了,但我无法根据触摸正确旋转布局(相对)。我需要设置旋转角度,以便当我在布局的左下角(有一个小图像)移动手指时布局旋转。基本上我有一个带有贴纸图像的相对布局。我可以通过触摸布局的相应角来拖动和缩放贴纸(图片),但我无法正确旋转它。有人可以帮我设置旋转角度,使整个布局可以平滑旋转吗?
这是代码 fragment
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;
}
`
最佳答案
尝试了很多东西但没有得到想要的结果。这个链接帮助我解决了这个问题。 rotate and resize the image view with single finger in android
来源链接 https://github.com/ryanch741/android-view-rotate-zoom-single-finger
关于android - 旋转包含基于 android 中的 onTouchListener 的贴纸的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610463/