Continue the program after the end of the animation android studio - android

I made an animation (ObjectAnimator)for a text view it work correctly but i have a question.
I wanna know is there any method that i can use immediately after animation duration finished ?
(I mean a method like onFinish when we use CountDownTimer)
I want the rest of methods and codes I have in app run when animation finished.Is there a solution for that?
ObjectAnimator deltaXAnimation = ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
ObjectAnimator deltaYAnimation = ObjectAnimator.ofFloat(winMassage,"scaleY",1f,1.5f);
deltaYAnimation.setDuration(300);
deltaYAnimation.start();

HI there are proper callback methods in which you get Animation Start and end Listeners
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
deltaXAnimation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});

Below code might be understand well, creating a listener for ObjectAnimator
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation a) {
//
}
public void onAnimationRepeat(Animation a) {
}
public void onAnimationEnd(Animation a) {
}
});
We can go also to your ObjectAnimator
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//stop animation after 5 seconds
deltaXAnimation.cancel();
}
}, 5000); //5000 equals to 5seconds
}
Hope this might help you

Related

How to run code in each time in android

I want show animation in my application, and i want show this animation each 3000m/s.
I write below code, but in this code show just once.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
YoYo.with(Techniques.Bounce)
.duration(1500)
.playOn(arrowHelpImage);
}
}, 3000);
How can i edit my code and show each 3000m/s ?
U could use something like this. change the animations used below. this animations will run infinite. provide animation on end Listner. Change animation duration to 3000ms and use this in handler.it will work
YoYo.with(Techniques.Bounce)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {
YoYo.with(Techniques.Bounce)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(this).playOn(arrowHelpImage);
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
}).playOn(arrowHelpImage);
Put below code in your class.
private Handler handler;
private Runnable runnable;
below code in you oncreate
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
YoYo.with(Techniques.Bounce)
.duration(1500)
.playOn(arrowHelpImage);
handler.postDelayed(runnable, Constants.SPLASH_DURATION);
}
};
handler.postDelayed(runnable, Constants.SPLASH_DURATION);
#Override
protected void onDestroy() {
super.onDestroy();
//Close the handler and the process of splash screen.
if (handler != null && runnable != null) {
handler.removeCallbacks(runnable);
}
}

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.

How to repeat YoYo Animation Techniques infinite times?

Hi everyone..
I'm using very nice animation techniques from Github. This guy provide us very nice text effect and i like to use some of them for infinite times not only if User pressed that particular button and then play that effect.
here is my code:
private YoYo.YoYoString rope;
rope = YoYo.with(Techniques.RollOut)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
}).playOn(mTarget);
So here is my problem when I'm going to add this code at onAnimationEnd method:
animation.setRepeatCount(Animation.INFINITE);
there will be error and it said: "The method setRepeatCount(int) is undefined for the type Animator".
If you want to proceed please go to Github address I've provided.
So, again in short, i want to repeat an animation infinite times.
I use repeat method like this.
YoYo.with(Techniques.Tada)
.duration(700)
.repeat(Animation.INFINITE)
.playOn(...);
This trick works for me. Try
YoYo.with(Techniques.RollOut)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {
YoYo.with(Techniques.RollOut)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(this).playOn(mTarget);
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
}).playOn(mTarget);
The best way with countDownTimer:
private void countDownForCustomAnimation(final int time){
new CountDownTimer(time, time){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
if(!isMainClickedYet){
YoYo.with(Techniques.RollOut)
.duration(timeforDuration)
.playOn(view);
countDownForCustomAnimation(time);
}
}
}.start();
}
I use the code below to repeat animation :
YoYo.with(Techniques.Wobble)
.repeat(YoYo.INFINITE)
.duration(5000)
.playOn(imgCycleRoad);

Animation doesn't get called inside postDelayed run()

I have a linearlayout that I want it to be animated after 3 seconds delay using Handler.
After 3 seconds have passed, it doesn't even execute the animation, nor did it enter the AnimationListener's methods.
Here is how I do it:
loginBox.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Animation animTranslate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate);
animTranslate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
loginBox.setVisibility(View.VISIBLE);
Animation animFade = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
loginBox.startAnimation(animFade);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
btnContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
});
}
}, 3000);
The run() method works fine when I click btnContinue.
How can I make it work?
you forgot to call
loginBox.startAnimation(animTranslate)
and probably you want loginBox.setVisibility(View.VISIBLE); before starting the TranslateAnimation

Delay while restart of animation

I'm using value animator to animate a image view. I want to add a delay in Animation listener, i.e in "onAnimationRepeat()". How to accomplish this?
Maybe try something like this:
public class Test {
private int repeatNumber == 0;
public void startAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {Log.i("start", "start");}
#Override
public void onAnimationEnd(Animation animation) {Log.i("end", "end");}
#Override
public void onAnimationRepeat(Animation animation) {
Test.this.repeatNumber++;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Test.this.startAnimation();
}, 30000 );
}
textView.startAnimation(animation);
}
}

Categories

Resources