How to run code in each time in android - 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);
}
}

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

How to time an animation in android

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

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

DialogFragment animation of layout and "Attempting to destroy the window while drawing!" error

I have a DialogFragment which has some animations of some of the layouts inside its view.
When I dismiss the dialog, I want to perform an animation and when the animation has ended, perform the dismiss operation.
Step1:
Call the fragment from my activity:
myDialog.show(getSupportFragmentManager(), "");
Step 2:
After the user had completed the job with the dialog, he presses a button. That button calls an animation and after that I want the dialog to disappear:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.my_layout, null);
layMain = (LinearLayout) layout.findViewById(R.id.layMain);
TextView btnCancel = (TextView) layout.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.translate_to_bottom);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
dismiss();
}
});
layMain.startAnimation(anim);
}
});
.....
When the animations ends, the dialog gets dismissed but I get this error on logcat
E/ViewRootImpl(25507): Attempting to destroy the window while drawing!
E/ViewRootImpl(25507): window=android.view.ViewRootImpl#427348e0,
title=com.mypackage/com.mypackage.MyActivity
How can I prevent this ?
Later edit: if I am using it without any animation, everything works fine and no error is shown on logcat. So I suppose it has to do something with the animation.
The animation I am using:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="80%p" />
</set>
The only solution that works so far, without throwing an error is this:
#Override
public void onAnimationEnd(Animation animation) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
dismiss();
}
}, 10);
}
I am open to any other sugestions
You have pass fragment context as a getSupportFragmentManager().
So you have to change it:
myDialog.show(getFragmentManager(), "");
I do not see any such issues:
public class MyDIalog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Button b = new Button(getActivity());
final Dialog d = super.onCreateDialog(savedInstanceState);
d.setContentView(b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
b.animate().rotationYBy(180).setDuration(500).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
dismiss();
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
}
});
return d;
}
}
works fine with:
new MyDIalog().show(getFragmentManager(),"dialog");
Any ways, you can schedule the dismiss() call a bit later by using:
#Override
public void onAnimationEnd(Animator animator) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
dismiss();
}
});
}
You can use like this.
#Override
public void onAnimationEnd(Animation animation) {
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
dismiss();
}
});
}
here, the advantage is, you don't define your own time, this way it may help to keep everything else simple, as a user can surprise you even within a short duration if dialog kept without dismissing after the animation ends.
I am not sure if it's too late to answer your question but the only way to get rid of this error is to post the dialogue removal code in a view's post method in your onAnimationEnd().
for example:
view.post(new Runnable() {
#Override
public void run() {
windowManager.removeView(layout);
}
});

Categories

Resources