Android change background image with fade in/out animation

前端 未结 5 1602
心在旅途
心在旅途 2020-12-29 08:47

I wrote code which can change background image random every 5 second.now i want to use fade in/out animation to change background image,but I do not know how I can use this

5条回答
  •  青春惊慌失措
    2020-12-29 09:22

    You need AnimationDrawable with animation.

    First step AnimationDrawable

    -Create a file /res/anim/anim_android.xml

    
    
        
        
        
        
        
        
        
    
    

    -Add a ImageView with android:src="@anim/anim_android".

    
    

    Second step

    -In your activity create AnimationDrawable and Animation

    AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
                animationDrawable.setOneShot(true);
                animationDrawable.start();
    
        Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in);
    
        imageView.setAnimation(animation);
            animation.start();
            animation.setAnimationListener(new Animation.AnimationListener() {
              @Override
              public void onAnimationStart(Animation animation) {
              }
              @Override
              public void onAnimationEnd(Animation animation) {
                  Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out);
                  imageView.startAnimation(fadeOut);
              }
              @Override
              public void onAnimationRepeat(Animation animation) {
              }
    });
    

    you don't need Handler.

提交回复
热议问题