1

我正在为 android 应用程序制作动画。该应用程序的目的是动画排序/搜索算法。我在使用 ObjectAnimator 时遇到问题,因为我无法让它交换两个元素。这是我的相关代码:

public void swapNodes(TextView node1, TextView node2, int animationSpeed) {
    //switching leftmost with rightmost
    if (node1.getX() > node2.getX()) 
    {
        TextView temp = node1;
        node1 = node2;
        node2 = temp;

    }
    Handler h = new Handler();
    h.postDelayed(moveUpRunnable(node1, 150, 0, 1000), ithRun*1000);
    ++ithRun;
    h.postDelayed(moveDownRunnable(node2, 150, 0, 1000), ithRun*1000);
    ++ithRun;
    float distance = node2.getX() - node1.getX();
    h.postDelayed(moveRightRunnable(node1, distance, 0, 1000), ithRun*1000);
    ++ithRun;
    h.postDelayed(moveLeftRunnable(node2, distance, 0, 1000), ithRun*1000);
    ++ithRun;
    h.postDelayed(moveDownRunnable(node1, 150, 0, 1000), ithRun*1000);
    ++ithRun;
    h.postDelayed(moveUpRunnable(node2, 150, 0, 1000), ithRun*1000);
    ++ithRun;

}
public void moveDown (final TextView n, float distance, int timeDelay, int duration) {
    ObjectAnimator down = ObjectAnimator.ofFloat(n, "TranslationY", n.getTranslationY() + distance);
    down.setStartDelay(timeDelay);
    down.setDuration(duration);

    down.start();
}
public Runnable moveRightRunnable(final TextView n, final float distance, final int timeDelay, final int duration) {
    Runnable r = new Runnable()
    {
        public void run()
        {
            moveRight(n,distance,timeDelay,duration);
        }
    };
    return r;
}
public void moveRight (final TextView n, float distance, int timeDelay, int duration) {
    ObjectAnimator right = ObjectAnimator.ofFloat(n, "TranslationX", n.getTranslationX() + distance);
    Log.d("moveRightX", Float.toString(n.getTranslationX()));
    Log.d("moveRightY", Float.toString(n.getTranslationY()));
    right.setStartDelay(timeDelay);
    right.setDuration(duration);

    right.start();
}
public Runnable moveLeftRunnable(final TextView n, final float distance, final int timeDelay, final int duration) {
    Runnable r = new Runnable()
    {
        public void run()
        {
            moveLeft(n,distance,timeDelay,duration);
        }
    };
    return r;
}
public void moveLeft (final TextView n, float distance, int timeDelay, int duration) {
    ObjectAnimator left = ObjectAnimator.ofFloat(n, "TranslationX", n.getTranslationX() - distance);
    Log.d("moveLeftX", Float.toString(n.getTranslationX()));
    Log.d("moveLeftY", Float.toString(n.getTranslationY()));
    left.setStartDelay(timeDelay);
    left.setDuration(duration);

    left.start();
}
public Runnable moveUpRunnable(final TextView n, final float distance, final int timeDelay, final int duration) {
    Runnable r = new Runnable()
    {
        public void run()
        {
            moveUp(n,distance,timeDelay,duration);
        }
    };
    return r;
}
public void moveUp (final TextView n, float distance, int timeDelay, int duration) {
    ObjectAnimator up = ObjectAnimator.ofFloat(n, "TranslationY", n.getTranslationY() - distance);
    up.setStartDelay(timeDelay);
    up.setDuration(duration);

    up.start();

}
4

0 回答 0