android系统中绕着两个点不工作 [英] Rotating around two points in android not working
问题描述
我有两个单独的(X,Y)点,我想用应用旋转视图。
第一点是固定的,我觉得它的价值很容易(例如200,200)。我的第二点是对触摸为present,所以我抢RawX和RawY点很容易为好。我喂这两点到这个方法,我在另一个堆栈溢出问题找到。
私人浮动findRotation(INT firstPointX,诠释firstPointY,诠释secondPointX,诠释secondPointY){
双delta_x =(firstPointX - secondPointX);
双delta_y =(firstPointY - secondPointY);
双弧度= Math.atan2(delta_y,delta_x);
回报(浮)Math.toDegrees(弧度);
}
和我使用的是返回设置视图的旋转。像这样 myView.setRotation(...)
。结果最终被一个疯狂的旋转视图,而我移动光标/手指在屏幕上。有任何想法吗?
两点我抓住似乎是正确的,留下我猜测,也许findRotation方法不正确。
我的活动:
公共类MainActivity延伸活动{
ImageView的ImageView的;
ImageView的dragHandle;
RelativeLayout的布局;
INT rememberAngle; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); ImageView的=(ImageView的)findViewById(R.id.imageView1);
dragHandle =(ImageView的)findViewById(R.id.imageView2);
布局=(RelativeLayout的)findViewById(R.id.relativeLayout2); 调整(dragHandle); }
公共无效调整大小(ImageView的resizeButton){
resizeButton.setOnTouchListener(新View.OnTouchListener(){ 公共布尔onTouch(视图V,MotionEvent motionEvent){ 如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
INT [] locationOfLayout =新INT [2];
INT [] locationOfDrag =新INT [2]; layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag); INT firstPointX = locationOfLayout [0];
INT firstPointY = locationOfLayout [1]; INT secondPointX = dragHandle.getWidth()/ 2 + locationOfDrag [0];
INT secondPointY = dragHandle.getHeight()/ 2 + locationOfDrag [1];
rememberAngle =(int)的findRotation(firstPointX,firstPointY,secondPointX,secondPointY)+ layout.getRotation();
}否则如果(motionEvent.getAction()== MotionEvent.ACTION_MOVE){
INT [] locationOfLayout =新INT [2];
INT [] locationOfDrag =新INT [2]; layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag); INT centerXOnLayout = layout.getWidth()/ 2 + locationOfLayout [0];
INT centerYOnLayout = layout.getHeight()/ 2 + locationOfLayout [1]; INT centerXOnDrag = dragHandle.getWidth()/ 2 + locationOfDrag [0];
INT centerYOnDrag = dragHandle.getHeight()/ 2 + locationOfDrag [1]; layout.setRotation(findRotation(centerXOnLayout,centerYOnLayout,centerXOnDrag,centerYOnDrag) - rememberAngle);
}否则如果(motionEvent.getAction()== MotionEvent.ACTION_UP){ }
返回true;
}
});
} 私人浮动findRotation(INT firstPointX,诠释firstPointY,诠释secondPointX,诠释secondPointY){
双delta_x =(secondPointX - firstPointX);
双delta_y =(secondPointY - firstPointY);
双弧度= Math.atan2(delta_y,delta_x);
回报(浮)Math.toDegrees(弧度);
}
}
我的XML:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < RelativeLayout的
机器人:ID =@ + ID / relativeLayout2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真正的> < ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:SRC =@绘制/ ic_launcher/> < ImageView的
机器人:ID =@ + ID / imageView2
机器人:layout_width =48dp
机器人:layout_height =48dp
机器人:layout_below =@ + ID / imageView1
机器人:layout_toRightOf =@ + ID / imageView1
机器人:SRC =@绘制/ meanicons/>
< / RelativeLayout的>< / RelativeLayout的>
公共无效调整大小(ImageView的resizeButton){
resizeButton.setOnTouchListener(新View.OnTouchListener(){ 浮startAngle开始;
浮zeroAngle;
INT firstPointX;
INT firstPointY; 公共布尔onTouch(视图V,MotionEvent motionEvent){ 如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
INT [] locationOfLayout =新INT [2];
INT [] locationOfDrag =新INT [2]; layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag); firstPointX = locationOfLayout [0];
firstPointY = locationOfLayout [1]; INT secondPointX = motionEvent.getRawX();
INT secondPointY = motionEvent.getRawY(); zeroAngle = findRotation(firstPointX,firstPointY,secondPointX,secondPointY)//记得零的角度
由startAngle = layout.getRotation(); //记住角度哪种布局在开始旋转
}否则如果(motionEvent.getAction()== MotionEvent.ACTION_MOVE){ layout.setRotation(findRotation(firstPointX,firstPointY,motionEvent.getRawX(),motionEvent.getRawY()) - zeroAngle + startAngle开始); //相对旋转就零角度
}否则如果(motionEvent.getAction()== MotionEvent.ACTION_UP){ }
返回true;
}
});
}私人浮动findRotation(INT firstPointX,诠释firstPointY,诠释secondPointX,诠释secondPointY){
双delta_x =(secondPointX - firstPointX);
双delta_y =(secondPointY - firstPointY);
双弧度= Math.atan2(delta_y,delta_x);
回报(浮)Math.toDegrees(弧度);
}
I have two seperate (x, y) points that I want to use to apply rotation to a view.
The first point is fixed, and I find the values of it fairly easily (for example 200,200). My second point is where a TOUCH is present, so I grab the RawX and RawY points easily as well. I feed these two points into this method that I found on another stack overflow question.
private float findRotation(int firstPointX, int firstPointY, int secondPointX, int secondPointY) {
double delta_x = (firstPointX - secondPointX);
double delta_y = (firstPointY - secondPointY);
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
and I use the return of that to set the rotation of a View. Like so myView.setRotation(...)
. The result ends up being a crazy spinning view while I move the cursor/finger on the screen. Any ideas?
The two points I'm grabbing seem to be correct, leaving me guessing that maybe the findRotation method is incorrect.
My activity:
public class MainActivity extends Activity {
ImageView imageView;
ImageView dragHandle;
RelativeLayout layout;
int rememberAngle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
dragHandle = (ImageView) findViewById(R.id.imageView2);
layout = (RelativeLayout) findViewById(R.id.relativeLayout2);
resize(dragHandle);
}
public void resize(ImageView resizeButton) {
resizeButton.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
int[] locationOfLayout = new int[2];
int[] locationOfDrag = new int[2];
layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag);
int firstPointX = locationOfLayout[0];
int firstPointY = locationOfLayout[1];
int secondPointX = dragHandle.getWidth() / 2 + locationOfDrag[0];
int secondPointY = dragHandle.getHeight() / 2 + locationOfDrag[1];
rememberAngle = (int) findRotation(firstPointX, firstPointY, secondPointX, secondPointY) + layout.getRotation();
} else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
int[] locationOfLayout = new int[2];
int[] locationOfDrag = new int[2];
layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag);
int centerXOnLayout = layout.getWidth() / 2 + locationOfLayout[0];
int centerYOnLayout = layout.getHeight() / 2 + locationOfLayout[1];
int centerXOnDrag = dragHandle.getWidth() / 2 + locationOfDrag[0];
int centerYOnDrag = dragHandle.getHeight() / 2 + locationOfDrag[1];
layout.setRotation(findRotation(centerXOnLayout, centerYOnLayout, centerXOnDrag, centerYOnDrag) - rememberAngle);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
});
}
private float findRotation(int firstPointX, int firstPointY, int secondPointX, int secondPointY) {
double delta_x = (secondPointX - firstPointX);
double delta_y = (secondPointY - firstPointY);
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
}
My XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_below="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/meanicons" />
</RelativeLayout>
</RelativeLayout>
public void resize(ImageView resizeButton) {
resizeButton.setOnTouchListener(new View.OnTouchListener() {
float startAngle;
float zeroAngle;
int firstPointX;
int firstPointY;
public boolean onTouch(View v, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
int[] locationOfLayout = new int[2];
int[] locationOfDrag = new int[2];
layout.getLocationOnScreen(locationOfLayout);
dragHandle.getLocationOnScreen(locationOfDrag);
firstPointX = locationOfLayout[0];
firstPointY = locationOfLayout[1];
int secondPointX = motionEvent.getRawX();
int secondPointY = motionEvent.getRawY();
zeroAngle = findRotation(firstPointX, firstPointY, secondPointX, secondPointY) // remember "zero" angle
startAngle = layout.getRotation(); // remember angle at which layout is rotated at the start
} else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
layout.setRotation(findRotation(firstPointX, firstPointY, motionEvent.getRawX(), motionEvent.getRawY()) - zeroAngle + startAngle); // rotate relative to start and zero angle
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
});
}
private float findRotation(int firstPointX, int firstPointY, int secondPointX, int secondPointY) {
double delta_x = (secondPointX - firstPointX);
double delta_y = (secondPointY - firstPointY);
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
这篇关于android系统中绕着两个点不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!