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);
Related
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
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 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);
}
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) {
}
});
I have prepared a simple test project at GitHub for my question:
I am trying to show/hide a FloatingActionButton (aka FAB) every 5 seconds by the following code in MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFab = (FloatingActionButton) findViewById(R.id.fab);
mInAnimation = AnimationUtils.makeInAnimation(this, false);
mInAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
mFab.setVisibility(View.VISIBLE);
}
});
mOutAnimation = AnimationUtils.makeOutAnimation(this, true);
mOutAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mFab.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
run();
}
The Runnable output ("Toggle animation") appears every 5 seconds in ADB logs, but the FAB is always visible:
#Override
public void run() {
Log.d("MyCoordinator", "Toggle animation");
mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);
mHandler.postDelayed(this, 5000);
}
Does anybody please know, what is missing here?
Also I am curious, if it's possible to define the above animations in the activity_main.xml instead of Java code.
I would rather use mFab.startAnimation(mFab.isShown() ? mOutAnimation : mInAnimation) than mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);. With setAnimation you have to define the start time of your animation (which is probably what you are missing)