There is such an animation:

android:duration="100" android:fillBefore="false" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:toXScale="0.9" android:interpolator="@android:anim/decelerate_interpolator" android:toYScale="0.9" /> 

How it works: the object is first reduced, and then returned to its original state. (ImageView object)

In the code I address so:

 anim= AnimationUtils.loadAnimation(getContext(),R.anim.clickbutton); 

and call it like this:

 public boolean onTouch(View v, MotionEvent event) { v.startAnimation(anim); switch (v.getId()) { case R.id.b1: { stopPlayerIfNeeded(); playSample(soundsRawResIds[0]); } break; 

Everything works like a clock, but I would like the animation to "work constantly" after clicking. I clicked on case R.id.b1 - the action was performed, but the animation continued to work until I chose another R.id.* and further

///////////////////////////////////////// how to stop the animation in this case? right now : chose R.id.b1: everything started and works, chose R.id.b2: the animation started but also on R.id.b1: the animation continues to run, but you need to stop

 for (int j = 0; j < arr_imageB.length; j++) { arr_imageB[j].setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.startAnimation(anim); switch (v.getId()) { case R.id.b1: { stopPlayerIfNeeded(); playSample(soundsRawResIds[0]); } break; case R.id.b2: { stopPlayerIfNeeded(); playSample(soundsRawResIds[1]); } break; case R.id.b3: { stopPlayerIfNeeded(); playSample(soundsRawResIds[2]); } break; case R.id.b4: { stopPlayerIfNeeded(); playSample(soundsRawResIds[3]); } break; case R.id.b5: { stopPlayerIfNeeded(); playSample(soundsRawResIds[4]); } break; case R.id.b6: { stopPlayerIfNeeded(); playSample(soundsRawResIds[5]); } break; case R.id.b7: { stopPlayerIfNeeded(); playSample(soundsRawResIds[6]); } break; } 

    3 answers 3

    If the animation is in <set> then the android:repeatCount="infinite" attribute should work

    • tell me, now it turns out that everything works fine in terms of animation, but if I chose case R.id.b1: and then case R.id.b2 then the animation will play. in both elements, I understand that I need to do a check and stop the animation? - upward
    • Yes, the animation in the view can be chopped off with view.clearAnimation (); - Android Android
    • That is, I assign view.clearAnimation () to each R.id.b; ? - upward
    • one
      You need to write a method in which the animations would be cut down when you press a button and run on the desired one on all views - Android Android
    • @Android Android and then call the method in kesah? - upward

    Hang up the listener of the end of the animation and play it again in it, again hanging up the listener:

     View viewToAnimate = ...; final int animResId = R.anim.anim_file; Animation anim = AnimationUtils.loadAnimation(ctx, animResId); anim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { Animation anim = AnimationUtils.loadAnimation(ctx, animResId); anim.setAnimationListener(this); viewToAnimate.startAnimation(anim); } @Override public void onAnimationRepeat(Animation arg0) { } @Override public void onAnimationStart(Animation arg0) { } }); viewToAnimate.startAnimation(anim); 

    So it will be repeated ~ infinitely.

    • Override public void onAnimationStart (Animation arg0) {anim = AnimationUtils.loadAnimation (getActivity (), R.anim.clickbutton); anim.setAnimationListener (this); rootViewB.startAnimation (anim); } and then how to call this method and stop the animation, ie R.id.b1: I clicked, the animation started, everything is OK, clicked R.id.b2: I need to stop b1, that is, to make a check, not to omg understand how to do it in your version - upward
    • @upward, try something like this: stackoverflow.com/a/30501784/3212712 - Yuriy SPb
    • I added the code to the header, I understand that I need to call clearAnimation (), only as in my case ... I tried everything - upward
    • @upward, well ... Like running around the array of views and calling the stop method above each ... - YuriySPb
    • one
      @upward, do not endlessly edit the question with an already accepted answer. Moreover, you have another question - not how to loop the animation, but how to stop. Ask a new question. - Yuriy SPb

    Infinite animation

     anim.setRepeatCount(-1); 

    Stop animation

     v.getAnimation().setRepeatCount(0);