Android change background image with fade in/out animation
Asked Answered
C

5

16

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 animation.

This is a my source:

void handlechange() {

    Handler hand = new Handler();
    hand.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            // change image here
            change();

        }

        private void change() {
            // TODO Auto-generated method stub

            Random rand = new Random();

            int index = rand.nextInt(image_rundow.length);

            mapimg.setBackgroundResource(image_rundow[index]);

            handlechange();
        }
    }, 4000);

}

At the moment everything is OK. I can change background image random,but I don't know how can I use animation fade in/out.

If anyone knows solution please help me, thanks.

Cauline answered 24/7, 2014 at 16:36 Comment(1)
take a look at this: #2615045Terminus
C
25

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) {
      }
});
Cheryl answered 24/7, 2014 at 17:48 Comment(4)
worked like a charm. applied both fade in and fade out to my LinearLayout View. Had to change to 500 ms. 2000 is far too long.Austerity
is it suppose to be imageView.startAnimation(fadeIn );Kinghorn
This is not answering the real question. This is animation on ImageView (basically View) and not the background of a View.Dozer
This YouTube video is the real answerDozer
C
6

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.

Curie answered 24/7, 2014 at 16:43 Comment(0)
D
2

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.

Drummer answered 29/12, 2014 at 21:30 Comment(0)
B
1

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.

Bowker answered 13/1, 2016 at 10:0 Comment(0)
D
1

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()
Dozer answered 24/1, 2019 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.