Android アプリケーションのアニメーションに取り組んでいます。アプリの目的は、並べ替え/検索アルゴリズムをアニメーション化することです。2 つの要素を入れ替えることができないため、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();
}