glassraven glassraven - 11 days ago 8
Android Question

Android: Animations - how to determine the end of an xml file animation and start an AlphaAnim in java?

I have an animation(xml) file that sets an image view to appear like the credits of a movie:

<?xml version="1.0" encoding="utf-8"?>

<set

xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >

<translate
android:duration="10000"
android:fromYDelta="100%p"
android:toYDelta="0%p" />

</set>


As you can see, the image stops "moving" when it gets full screen. This is ok.
My question is: how can I set a textView (hidden) visible when this IV reaches de end of the animation, so the text can be over the image?

The java code:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

mozart=(ImageView) findViewById(R.id.mozart);

Picasso.with(this).load(R.drawable.amadeus).fit().centerCrop().into(mozart);

read=(TextView) findViewById(R.id.read);
restart=(Button) findViewById(R.id.restart);
restart.setOnClickListener(this);

Animation moz= AnimationUtils.loadAnimation(this, R.anim.animation);
mozart.startAnimation(moz);

read.setVisibility(View.INVISIBLE);
read.setText("Text to appear with other alphaAnimation");

}


@Override
public void onClick(View view) {
if (view == restart) {
allaTurca.stop();

Intent a = new Intent(this, MainActivity.class);
startActivity(a);
this.finish();
}

}

}


Is it possible? I've tried several aproaches, but I cant't figure out how to tell the program when the image is ready to "receive" the text. Thaks for the help

Answer

You have to add an animation listener to the animation like this

moz.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
   //Do Something on start
}           
@Override
public void onAnimationRepeat(Animation arg0) {
}           
@Override
public void onAnimationEnd(Animation arg0) {
}
});

When the animation starts it will first pass through the onStart( this is where u can initialize variables for animation) ...

then if you have repetition in your animation onAnimationRepeat will be called.

When the Animation is finished the onAnimationEnd will be called. ( This is the part you are interested in)

Comments