Delay while restart of animation - android

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);
}
}

Related

Continue the program after the end of the animation android studio

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

Blink animation on TextView with text changing simultaneously

I have an array of Strings and want to display one String at a time with blink animation (after a blink another String in textView). I did the animation part with the code below:
On OnCreate:
TextView subTitle=findViewById(R.id.subTitle);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(2500);
anim.setStartOffset(0);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
subTitle.startAnimation(anim);
and changing the textview text with this:
Handler h = new Handler();
int delay = 2500;
Runnable runnable;
#Override
protected void onResume() {
h.postDelayed(runnable = new Runnable() {
public void run() {
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
h.postDelayed(runnable, delay);
}
}, delay);
super.onResume();
}
But the text of textView does not change simultaneously with blink. Is there a single way I can do this?
You can do like below...
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//if set animation count to once
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
anim.start();
}
#Override
public void onAnimationRepeat(Animation animation) {
// Or if set repeat count to infinite
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
}
});
Modify as you want...

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);
}
}

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) {
}
});

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

Categories

Resources