Android animation does not repeat

后端 未结 23 1357
囚心锁ツ
囚心锁ツ 2020-11-27 14:01

I\'m trying to make simple animation that would repeat several times (or infinitely).
It seems that android:repeatCount does not work!
Here is my anim

相关标签:
23条回答
  • 2020-11-27 14:14

    I do most of my stuff programmatically and I may be late or inefficient on this one but this but I completed the repeat animationset goal (I a even have 2 alternating animation sets). All this code does is simply fade in one image,pause, then fade out, fade in another image, pause, fade out, and bring back the first one (rinse and repeat). I first defined my Imageviews:

        final ImageView purple = (ImageView)findViewById(R.id.purp);
        final ImageView yellow = (ImageView)findViewById(R.id.yell);
        purple.setVisibility(View.INVISIBLE);
        yellow.setVisibility(View.INVISIBLE);
    

    Then I made two timers, task timers and handlers to deal with when to start and stop each animation:

        Timer p = new Timer();
        TimerTask pu = new TimerTask() {
            public void run() {
                    handler1.post(new Runnable() {
                            public void run() 
                            {
                               fadein(purple);
                            }
                   });
            }};
            p.schedule(pu, 6000, 12000);
    
        final Handler handler2 = new Handler();
    
        Timer y = new Timer();
        TimerTask ye = new TimerTask() {
            public void run() {
                    handler2.post(new Runnable() {
                            public void run() 
                            {
                               fadein(yellow);
                            }
                   });
            }};
    
            y.schedule(ye, 0, 12000);
    

    Finally, rather than creating animation sets by adding animations, I just animations listeners to to determine when to start each animation:

    public void fadein (final ImageView image)
    {
        Animation anim = new AlphaAnimation(0, 1);
    
        anim.setDuration(2000);
    
        image.startAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) 
            {
                image.clearAnimation();
                image.invalidate();
                pause(image);
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
    
            }
        });
    }    
    public void pause (final ImageView image)
    {
        Animation anim = new AlphaAnimation(1, 1);
    
        anim.setDuration(2000);
    
        image.startAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) 
            {
                image.clearAnimation();
                image.invalidate();
                fadeout(image);
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
    
            }
        });
    }     
    public void fadeout (final ImageView image)
    {
        Animation anim = new AlphaAnimation(1,0);
    
        anim.setDuration(2000);
    
        image.startAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) 
            {
                image.clearAnimation();
                image.invalidate();
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
    
            }
        });
    }    
    

    The clearanimation and invalidate where just previous attempts and getting this thing to work right. I don't know if they are required or not.

    Hope this helps someone.


    Ryan

    0 讨论(0)
  • 2020-11-27 14:15

    Little tweak to @Danufr answer to save resources from loading again.

        operator = (ImageView) findViewById(R.id.operator_loading);
      final  Animation ani = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.finding_operator);
    
    
        ani.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
    
                operator.startAnimation(ani);
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
    
            }
        });
    
        operator.setAnimation(ani);
    
    0 讨论(0)
  • 2020-11-27 14:16

    it's working fine

     GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
        gifDrawable.setLoopCount(0);
    
    0 讨论(0)
  • 2020-11-27 14:18

    Try to add the code to a looping thread or a while/for statement

    0 讨论(0)
  • 2020-11-27 14:20

    I've faced the same problem, but didn't want to do any timing things in Java because of the point that the UI thread may be very busy sometimes. The INFINITE flag doesn't work for the set tag. So I resolved the issue with a little piece of code:

    mAnimation = (AnimationSet) AnimationUtils.loadAnimation(myContext, R.anim.blink);
    mIcon.startAnimation(mAnimation);
    mAnimation.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation animation) {}
        public void onAnimationRepeat(Animation animation) {}
        public void onAnimationEnd(Animation animation) {
            mIcon.startAnimation(mAnimation);
        }
    });
    

    with the following XML:

    <alpha
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    
    <alpha
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromAlpha="0.9"
        android:startOffset="1000"
        android:toAlpha="0.0" />
    

    Where mIcon is an ImageView from my layout.

    0 讨论(0)
  • 2020-11-27 14:22

    You should include the attribute

    android:repeatCount="infinite"
    

    But in your "scale" animation not in "set"

    0 讨论(0)
提交回复
热议问题