android 以弧形移动图像视图

android move image view in an arc

我们想使用 sin & cos 在两个弧形中移动图像视图,其中的形状就像 NFL 橄榄球的外周。我们不太优雅的代码如下。它有效,但是当我们尝试用 sin 和 cos 解决这个动画时,第一个问题是坐标系统认为点 0,0 是屏幕的左上角并移动 imageview 并开始将它移出屏幕。我们已尝试通过各种有效的方法 none 来克服这个问题。我们知道起点和终点 X 和 Y,但不知道圆的半径。 那么第一个问题是如何让 imageview 处于正确的位置? 问题二是如何将图像视图移动到顶部弧线,然后沿着底部弧线回到起始位置。 我们在问题中使用 Android 因为这不是数学或 JavaFX 问题 请理解,我们知道数学是关于在 Android 中用 sin 和 cos 实现它。 这个link接近

这是我们的工作代码

    public void startGAME(View view){

    earth.setVisibility(View.VISIBLE);
    earth.setX(60);
    earth.setY(520);
    // Create and Start the timer.
    timer = new Timer();
    handler = new Handler();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    orbitEARTH();
                }
            });
        }
    }, 0, 500);
}

public void orbitEARTH(){

    if(earth.getX() == 828){
        number = 2;
    }
    if(earth.getX() == 1884){
        number = 3;
    }
    if(earth.getX() == 914){
        number = 4;
    }
    if(earth.getX() == 41){
        earth.setX(60);
        earth.setY(520);
        number = 1;
    }

    switch (number) {
        case 1:
            xPos = (int) (earth.getX()+96);
            yPos = (int) (earth.getY()-30);
            earth.setX(xPos);
            earth.setY(yPos);
            System.out.println("##### 1 xPos"+xPos+" yPos "+yPos);
            break;
        case 2:
            xPos = (int) (earth.getX()+96);
            yPos = (int) (earth.getY()+30);
            earth.setX(xPos);
            earth.setY(yPos);
            System.out.println("##### 2 xPos"+xPos+" yPos "+yPos);
            break;
        case 3:
            xPos = (int) (earth.getX()-97);
            yPos = (int) (earth.getY()+30);
            earth.setX(xPos);
            earth.setY(yPos);
            System.out.println("##### 3 xPos"+xPos+" yPos "+yPos);
            break;

        case 4:
            xPos = (int) (earth.getX()-97);
            yPos = (int) (earth.getY()-30);
            earth.setX(xPos);
            earth.setY(yPos);
            System.out.println("##### 4 xPos"+xPos+" yPos "+yPos);
            break;
    }

}

实际上,我不确定您的代码是否那么优雅。使用 sin 和 cos 使地球在其轨道上移动需要更多的思考和工作。也就是说,如果有人尝试此代码,那么许多变量相互依赖,这并不理想,我将要 post。但它确实回答了这个问题 所以我可能会建议按照这个 link 来掌握主题

Math

    public void startGAME(View view){

    earth.setVisibility(View.VISIBLE);
    earth.setX(60);
    earth.setY(520);
    // Create and Start the timer.
    timer = new Timer();
    handler = new Handler();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    newORBIT();
                }
            });
        }
    }, 0, 500);
}

public void newORBIT(){
    newTheta = newTheta + 0.009;
    radius = 10;
    newDeltaX = radius*Math.cos(newTheta);
    newDeltaY = radius*Math.sin(newTheta);
    earth.setRotation(ANGLE += 7.5f);
    ANGLE = earth.getRotation();
    System.out.println("############# Angle "+ANGLE);
    if(ANGLE < 82.5) {
        X = (float) newDeltaX + 60;
        Y = (float) newDeltaY - 18.75f;
    }

    if(ANGLE > 82.5){
        X = (float) newDeltaX + 60;
        Y = (float) newDeltaY + 18.75f;
    }
    if(ANGLE > 196){
        X = (float) newDeltaX - 60;
        Y = (float) newDeltaY + 18.75f;
    }
    if(ANGLE > 330){
        X = (float) newDeltaX - 60;
        Y = (float) newDeltaY - 18.75f;
    }
    if(ANGLE > 457){
        ANGLE = 0;
        newTheta = 0;
        newDeltaX = 0.0f;
        newDeltaY = 0.0f;
        X = 0;
        Y = 0;
        earth.setRotation(0);
        earth.setX(60);
        earth.setY(520);
    }
    earth.setX(earth.getX()+X);
    earth.setY(earth.getY()+Y);

}