java - 在android中绕两点旋转不起作用

标签 java android rotation

我有两个独立的 (x, y) 点,我想用它们对 View 应用旋转。

第一个点是固定的,我很容易找到它的值(例如 200,200)。我的第二点是存在 TOUCH 的地方,因此我也可以轻松捕获 RawX 和 RawY 点。我将这两点输入到我在另一个堆栈溢出问题中找到的这个方法中。

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);
       }

我使用它的返回来设置 View 的旋转。就像这样myView.setRotation(...)。当我在屏幕上移动光标/手指时,结果最终是一个疯狂旋转的 View 。有任何想法吗?

我捕获的两点似乎是正确的,让我猜测也许 findRotation 方法是不正确的。

我的 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);
    }
}

我的 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);
}

关于java - 在android中绕两点旋转不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000012/

相关文章:

安卓控制台 : authentication required

android - 尝试在我的手机上安装应用程序时出现解析错误

iphone - 将 iPhone 设备旋转至横向时隐藏 TabBar

javascript - 如何在 Canvas 对象保持在同一位置的同时旋转该对象

java - 在 Android 中旋转文本(不是图像)

java - 如何在 Java 中将 dropbox 文件夹与 Amazon S3 同步?

java - Jpanel点击创建事件

java - java中如何根据另一个变量动态调用一个变量

java - Jackson 可以仅使用注释反序列化为 Map<Long, String> 吗?

android - (Android)/Chrome getUserMedia() 约束应该如何格式化?