Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make smooth frame animation in android?

I made a frame animation. But the transitition between images is bad looking. How can I apply a crossfade effect to it ?

When using TransitionDrawable i get the proper result but it stops after one execution.

Any one has an idea how to resolve it?

public void startAnimation() {
    if (logoAnimation != null) {
        if (logoAnimation.isRunning()) {
            logoAnimation.stop();
        }
        logoAnimation.start();
    }
}

private int setLogoAnimation(int animationID, int targetID) {
    imageView = (ImageView) window.findViewById(targetID);
    imageView.setImageResource(animationID);
    logoAnimation = (AnimationDrawable) imageView.getDrawable();

    if (imageView != null && logoAnimation != null) {
        return 1;
    } else {
        return 0;
    }
}

Than I simply run it by object.startAnimation(); I works, but the animation is ugly and I need it to be smooth.

like image 400
Olek Avatar asked May 04 '11 08:05

Olek


1 Answers

If you want to crossfade between two pictures, why not use an AlphaAnimation which will alter the transparency of two views and will create the effect that you require.

Create two animations:

res/anim/fadeout.xml

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

res/anim/fadein.xml

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

and then override the default transition between activities:

startActivity( new Intent( this, SecondActivity.class ) );
overridePendingTransition( R.anim.fadeout, R.anim.fadein );

or you can apply animations to specific widgets:

Animation animation = AnimationUtils.loadAnimation( this, R.anim.fadeout );
image1.startAnimation( animation );

I am currently in the middle of a series on animation on my blog which may give you some further information.

like image 130
Mark Allison Avatar answered Sep 17 '22 20:09

Mark Allison