android animation with no duration (only with set speed) -


i have linearlayout images inside (these images added dynamically width of layout change time time),

i want layout animate marquee left right (i can not use textview marquee because not support rtl ) have deal animation. the width of layout might change cant set static duration animation method setduration , when use example 10000 ms duration moves fast , slow (because of width change)

ok question: there custom method example setspeed animation class? how can achieve goal? want layout animate same speed size.

you can not set speed animate view directly, can manipulate achieve want:

you can set linearinterpolator animation let have same regular speed along whole animation, , implement listener want in onanimationend, and here comes tricky part:

the rule of speed is: speed = distance / time

lets apply in code logically. consider distance total width of images (or parent view linear layout). multiply width 1000 let duration in milliseconds, divide result want determine speed. animation speed should same regardless of total width of images.

similarly, can multiply total width of images constant number 10. same ratio.

here how done in code:

linearlayout linearlayout = (linearlayout) findviewbyid(r.id.linearlayout); int layoutwidth = linearlayout.getwidth(); int animationspeed = (layoutwidth * 1000) / 100;  linearlayout.animate().translationx(layoutwidth).setduration(animationspeed).setinterpolator(new linearinterpolator()).setlistener(new animatorlisteneradapter() {         @override         public void onanimationend(animator animation) {             super.onanimationend(animation);             linearlayout.setvisibility(view.gone);         }     }); 

if want make animation infinite, can this:

linearlayout linearlayout = (linearlayout) findviewbyid(r.id.linearlayout); int layoutwidth = linearlayout.getwidth(); int animationspeed = (layoutwidth * 1000) / 100;  final translateanimation translateanimation = new translateanimation(0, layoutwidth, 0, 0); translateanimation.setinterpolator(new linearinterpolator()); translateanimation.setduration(animationspeed); translateanimation.setanimationlistener(new animation.animationlistener() {     @override     public void onanimationstart(animation animation) {      }      @override     public void onanimationend(animation animation) {         linearlayout.startanimation(translateanimation);     }      @override     public void onanimationrepeat(animation animation) {      } }); linearlayout.startanimation(translateanimation); 

Comments