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) {
}
});
Related
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);
I am animating a loader on start up. How can I work around it so that instead of using ontouch listener to stop the timer, I use a time of say like 3 seconds? Here is my code:
private void showdiag() {
loader = findViewById(R.id.loader);
logo= loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this,R.anim.rotate));
loader.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loader.setVisibility(View.GONE);
}
});
}
You can use View.postDelayed() to run some action after a set delay:
private void showdiag() {
loader = findViewById(R.id.loader);
logo = loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}
logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.setVisibility(View.GONE);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});
Please try following code.
logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});
I made nice looking splash screen and I want to block onclicklistener for a few seconds cause i've got few animations etc and I want the user see it all.
I've got:
ostatni.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
starttap.start();
Intent i = new Intent(Start.this,ActivityMainWallet.class);
startActivity(i);
}
});
EDIT
I've got:
ostatni.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
starttap.start();
ostatni.setEnabled(false);
}
});
wlot.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent i = new Intent(Start.this,ActivityMainWallet.class);
startActivity(i);
ostatni.setEnabled(true);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
And it doesn't work. Please help me, thanks!
You can pause the thread it is running on, the main thread (UI Thread):
Thread.sleep(4000); // freeze the thread for 4 seconds
Source: Java, Pausing Execution
SystemClock.sleep(2000)will be better.
sleep 2ms, and this method will not block the thread.
You can also use android animation utils to do this
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_in);
<your imageviews or any other view>.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//start your main activity
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
add this ?
public onCreate(....)
{
......
......
ostatni.setOnClickListener(........);
ostatni.setEnabled(false);
}
#Override
public void onAnimationEnd(Animation animation) {
ostatni.setEnabled(true);
}
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);
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);
}
}