Here is the situation. My Activity is running, and I call a function to start an animation. Once this animation finishes, I need to wait for another 2 seconds, then continue the process normally. What do you suggest I do?
The OnAnimationEnd does not help my case by the time I reach the end of iteration, my process has already returned from the calling function and continued processing.
What do you think I do? Do thread.sleep (duration of animation + 2 sec)?
Thanks
EDIT:
OK I thought maybe I should explain what I am doing in code
I have the following code
Public class myclassActivity extends Activity{
..
.
.
.
public void runGame(){
player1.startAnimating();
player2.start Animating();
player3.startAnimating();
updateStatus();
runGame();
}
}
So as you can see my activity keeps calling startAnimating for each player. In which some work and then animation for 1 second is done.
I don't want player2.startAnimation to start untill the player1.startAnimation is completed and so on for the rest of the code.
Currently what happens is that the code executes fully and rerun again while the animation is still running. Pleasee any help on this?
observe this code it is help full you.
public class PlayersActivity extends Activity {
ImageView I1,I2;
Button B;
Animation T1,T2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
I1=(ImageView)findViewById(R.id.i1);
I2=(ImageView)findViewById(R.id.i2);
B=(Button)findViewById(R.id.b1);
B.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
T1=new TranslateAnimation(0,100,0,0);
T1.setDuration(1000);
I1.startAnimation(T1);
T1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T2=new TranslateAnimation(0,100,0,0);
T2.setDuration(1000);
T2.setStartOffset(2000);
I2.startAnimation(T2);
T2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T1.setStartOffset(2000);
I1.startAnimation(T1);
}
});
}
});
}
});
}
}
Suppose you will start this two methods after completed player3, you are placing player3 onAnimationEnd(){ updateStatus(); runGame();}
Use postDelayed function to run something after exact delay. For example (2 sec delay):
LinearLayout.postDelayed(new Runnable() {
public void run() {
//Do something
}
}, 2000);
how about passing caller instance by
player1.startAnimating(this)
and then call method like
myclassActivity#animationFinished(this)
on finishing animation of player1/2? then
void animationFinished(Player p) {
if (p==player1) {
player2.start();
} else if (p==player2) {
player3.start();
}
}
or something
Related
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);
}
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 start an activity after the animation has ended. I have added android:oneshot="true" in the xml but how to start a new activity after this animation has stopped.I have attached the entire code below. Please let me know how to start new activity.If I use this code without showing my animation it goes to the another acitivity.
public class MainActivity extends Activity {
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animation = (AnimationDrawable) iv.getBackground();
if(hasFocus) {
animation.start();
startActivity(new Intent(MainActivity.this,second.class));
} else {
animation.stop();
}
}
public void onStart() {
{
super.onStart();
iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.animator.animation);
}
}
}
One way for sure you can use onAnimationListener, and be really interested into implementing the onAnimationEnd method of the listener interface.
Something like this:
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(MainActivity.this,second.class));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I am trying to make a page where when the user leaves the page, there will be an animation (eg.: button will slide out) in the page and after that, the user will be sendt to a different activity.
No issue with the animation, but as the code for starting the new activity is written just after the animation code, the animation is not completing for 1 second (as I have set).
I want to execute the animation for 1 second first and then move to another activity.
Please help me.
Use AnimationListener.
private Animation.AnimationListener animListener = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
// write code to start new activity.
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
};
Assign above listener to your animation
animation.setAnimationListener(animListener);
//Startanimation
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// call Activity
// End animation
}
}, 1000);
I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?
You can add Animation listener to your animation object like
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
}
});
Functionally the same as the accepted answer but in a much more concise way:
// Add/Remove any animation parameter
theView.animate()
.alpha(0)
.setDuration(2000)
.withEndAction(new Runnable() {
#Override
public void run() {
theView.setVisibility(View.GONE);
}
});
Enjoy :)
Simply take your animation object and add animation listener to it.
Here is the example code :
rotateAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
}
});
You can also achieve this using Animation.setFillAfter
Example for Kotlin
var fadeOutImage = findViewById<ImageView>(R.id.fade_out_Image)
val fadeOutAnimation = R.anim.fade_out_animation
val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
fadeOutImage.startAnimation(animation)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(p0: Animation?) {
// not implemented
}
override fun onAnimationRepeat(p0: Animation?) {
// not implemented
}
override fun onAnimationEnd(p0: Animation?) {
fadeOutImage.visibility = View.INVISIBLE
}
})