Animiation Drawable ending - android

I got this animation drawable running, and i want to continue only when it is finished. this code results in overlapping images.
public void onClick(View w) {
rangen = r.nextInt(4-1)+1;
frameAnimation.start();
if (rangen==1){
enemyj.setImageResource(R.drawable.paper);
restextj.setText("You Lose!");}
else if (rangen==2){
enemyj.setImageResource(R.drawable.rock);
restextj.setText("Draw");}
else if (rangen==3){
enemyj.setImageResource(R.drawable.sciss);
restextj.setText("You Win!");}}
}

add an animation listener to your frameAnimation before you start it
frameAnimation.setAnimationListener( new AnimationListener() {
#Override
public void onAnimationEnd (Animation animation) {
animation.setAnimationListener(null);
//do whatever work you need done
}
#Override
public void onAnimationRepeat (Animation animation) {
}
#Override
public void onAnimationStart (Animation animation) {
}
});

Related

animations unresponsive inside button click

I am trying to animate the button on click.It worked only once .The animation I used was Alpha.It works outside of button click.Can anyone find the reason for this strange behaviour?
Code:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(2000);
button.setAnimation(alphaAnimation);
alphaAnimation.start();
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Log.e("Alpha","a");}
});
Use startAnimation.
From official documentation:
void setAnimation (Animation animation)
Sets the next animation to play for this view. If you want the animation to play immediately, use startAnimation(android.view.animation.Animation) instead. This method provides allows fine-grained control over the start time and invalidation, but you must make sure that 1) the animation has a start time set, and 2) the view's parent (which controls animations on its children) will be invalidated when the animation is supposed to start.
Below is a sample snippet.
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(2000);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
button.startAnimation(alphaAnimation);

Applying sequence of animation on a view

is there a simple way to animate a view so that it does one animation after another.
i tried this:
button.animate().translationX(500).setDuration(300);
button.animate().translationX(0).setDuration(300).setStartDelay(2000);
Something like this:
button.animate().translationX(500).setDuration(300).withEndAction(new Runnable() {
#Override
public void run() {
button.animate().translationX(0).setDuration(300).start();
}
}).start();
Try setting a listener on the first animation so that when it ends, you can start the second animation:
button.animate().translationX(500).setDuration(300).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
button.animate().translationX(0).setDuration(300).start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});

How to start a function when animation has ended?

I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.
public Boolean functionFinished = false;
public void runFunction(){
firstFunction();
if(functionFinished = true){
secondFunction();
}
}
public void firstFunction(){
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
initialCount.startAnimation(out);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if(initialCount.getText().equals("3")){
initialCount.setText("2");
initialCount.startAnimation(out);
} else if(initialCount.getText().equals("2")){
initialCount.setText("1");
initialCount.startAnimation(out);
} else if (initialCount.getText().equals("1")){
initialCount.setText("START!");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
functionFinished = true;
}
The second function simply changes its own textView every second counting up from 0.
What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")
public void runFunction() {
firstFunction();
}
public void firstFunction() {
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
...
secondFunction();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
initialCount.startAnimation(out);
}
Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.
If your minimum sdk version is 14:-
textView1.animate()
.alpha(0f)
.setDuration(400)
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
// animate second textview here....
}
});
Because you assign functionFinished = true at the end of the firstFunction().
And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .
initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.

Play sound while animating imageview and should stop when imageview animation is stopped

Is there any solution that I can play sound while my imageView is animating.
I've tried this:
public void bounceInterpolar(View thumbnailView){
ImageView animatedImage = (ImageView) findViewById(R.id.img_animation);
Animation animation
= AnimationUtils.loadAnimation(this, R.anim.animation);
animatedImage.startAnimation(animation);
mp = MediaPlayer.create(this, R.raw.soho);
animatedImage = (ImageView)this.findViewById(R.id.img_animation);
animatedImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
}
});
Issue: By clicking on imageView, once the animation is over and then if I click on the imageView 2nd time, then the sound is played which is not my requirement. Need to play the sound parallel along with animation.
Please set animation listener to your animation object
animation.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
if (m_player != null && m_player.isPlaying())
{
m_player.stop();
m_player.release();
m_player = null;
}
}
});
You need to add listener to the animation.
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
mp.start();
}
#Override
public void onAnimationEnd(Animation animation) {
mp.stop();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

How to go to an anther activity after the end of a frame by frame android animation

How to start an activity after the animation has ended. I have added android:oneshot="true" in the xml but how to start a new activity after this animation has stopped.I have attached the entire code below. Please let me know how to start new activity.If I use this code without showing my animation it goes to the another acitivity.
public class MainActivity extends Activity {
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animation = (AnimationDrawable) iv.getBackground();
if(hasFocus) {
animation.start();
startActivity(new Intent(MainActivity.this,second.class));
} else {
animation.stop();
}
}
public void onStart() {
{
super.onStart();
iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.animator.animation);
}
}
}
One way for sure you can use onAnimationListener, and be really interested into implementing the onAnimationEnd method of the listener interface.
Something like this:
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(MainActivity.this,second.class));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Categories

Resources