I have this code which runs when an Async HTTP call is made on a HorizontalScrollView
//These 3 defined outside of the method
ObjectAnimator animatorR = null;
ObjectAnimator animatorL = null;
boolean loop = true;
Inside the method
if (animatorL != null) animatorL.cancel();
animatorL = null;
if (animatorR != null) animatorR.cancel();
animatorR = null;
HorizontalScrollView sv = (HorizontalScrollView) findViewById(R.id.scroll_view);
loop = true;
sv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
animatorL.cancel();
animatorR.cancel();
loop = false;
sv.setOnTouchListener(null);
return false;
}
});
animatorR = ObjectAnimator.ofInt(sv, "scrollX", sv.getMaxScrollAmount()).setDuration(4000);
animatorL = ObjectAnimator.ofInt(sv, "scrollX", 0).setDuration(4000);
animatorR.removeAllListeners();
animatorR.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d("AnimR", "Ended");
if (loop && animatorL != null) animatorL.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorR.start();
animatorL.removeAllListeners();
animatorL.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d("AnimL", "Ended");
if (loop && animatorR != null) animatorR.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
This is my first time using Animators and the code seems very clunky. Where could I make improvements to this code?
if
statement that is out of alignment \$\endgroup\$