Android change background image with fade in/out animation

前端 未结 5 1578
心在旅途
心在旅途 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:09

    You can use relativeLayout, and add one layer of background view which set both height and width match_parent. All other UI element should put on top of this view. In your code. Define fadeOut and fadeIn animation. Find that background view by id, then do this:

     xxxBackground.startAnimation(fadeOut);
     xxxBackground.setBackgroundResource(R.drawable.your_random_pic);
     xxxBackground.startAnimation(fadeIn);
    

    You can use some interpolator to tune the performance.

    0 讨论(0)
  • 2020-12-29 09:11

    Answer by kimkevin animates a View (be it an ImageView, TextView etc.) and not a background of a View. Which means if you want to just highlight any View, you'll have to add another View behind it (lets call it backgroundView) and animate that backgroundView.

    Use the following code for animating background of a view

    val startColor = view.solidColor
    val endColor = ContextCompat.getColor(context, R.color.your_color)
    
    val colorAnim = ObjectAnimator.ofInt(view, "backgroundColor", startColor, endColor, startColor)
    colorAnim.duration = 2000
    colorAnim.setEvaluator(ArgbEvaluator())
    colorAnim.start()
    
    0 讨论(0)
  • 2020-12-29 09:20

    You need to call these codes.

    First, you have to make two xml files for fade in & out animation like this.

    fade_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
            android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:fillAfter="true"
            android:duration="2000"
            />
    </set>
    

    fade_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
            android:fromAlpha="1.0"
            android:toAlpha="0.0"
            android:fillAfter="true"
            android:duration="2000"
            />
    </set>
    

    Second, you have to run animation of imageView like below and also you have to set AnimationListener to change fadeout when fadein finish.

    Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
    imageView.startAnimation(fadeOut);
    
    fadeOut.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
          }
          @Override
          public void onAnimationEnd(Animation animation) {
              Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
              imageView.startAnimation(fadeIn);
          }
          @Override
          public void onAnimationRepeat(Animation animation) {
          }
    });
    
    0 讨论(0)
  • 2020-12-29 09:22

    You need AnimationDrawable with animation.

    First step AnimationDrawable

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

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false">
        <item android:drawable="@drawable/android_1"
              android:duration="100"/>
        <item android:drawable="@drawable/android_2"
              android:duration="100"/>
        <item android:drawable="@drawable/android_3"
              android:duration="100"/>
        <item android:drawable="@drawable/android_4"
              android:duration="100"/>
        <item android:drawable="@drawable/android_5"
              android:duration="100"/>
        <item android:drawable="@drawable/android_6"
              android:duration="100"/>
        <item android:drawable="@drawable/android_7"
              android:duration="100"/>
    </animation-list>
    

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

    <ImageView
        android:id="@+id/myanimation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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.

    0 讨论(0)
  • 2020-12-29 09:23

    For fade in animation , create new folder named 'anim' in your res folder and inside it, create 'fade_in.xml' with following code

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:toAlpha="1.0" />
    
    </set>
    

    now to run this animation on your imageview, use following code in your activity

    Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
    imageView.setAnimation(anim);
    anim.start();
    

    for fadeout animation, just swap values of android:fromAlpha and android:toAlpha

    Hope this helps.

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