Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change background image with fade in/out animation

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.

like image 695
BekaKK Avatar asked Jul 24 '14 16:07

BekaKK


People also ask

Which animation property can be used to help create a fade effect in Android?

startAnimation(aniFade); If you observe above code snippet, we are adding an animation to the image using loadAnimation() method. The second parameter in loadAnimation() method is the name of our animation xml file.


4 Answers

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) {
      }
});
like image 186
kimkevin Avatar answered Nov 08 '22 09:11

kimkevin


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.

like image 28
akshay7692 Avatar answered Nov 08 '22 09:11

akshay7692


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.

like image 26
Ziwei Zeng Avatar answered Nov 08 '22 08:11

Ziwei Zeng


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.

like image 23
Cabezas Avatar answered Nov 08 '22 10:11

Cabezas