Android animation for 1 second - android

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

Related

Weird problem with button clicking in a layout that has a RecyclerView

I have run into a very strange problem. There is a layout I have with a RecyclerView and a button that switches on animation. The point is if I click the button right after the app executes - say 10 seconds, everything is fine. But if I leave the app running idle for some seconds, without touching anything, then when I click the button again, the animation simply doesn't start at all. And yet if after that I slide RecyclerView about the screen with my finger (in any place!) then the animation triggers all of a sudden. As if it was somehow frozen and "woke up" only after I interacted with the RecyclerView. As if RecyclerView after some seconds puts its animation-related objects to sleep somehow and they "wake-up" only after the RecyclerView is slided?.. Apparently the listener "goes to bed"... Is there a way to keep the animation-related objects "awake" at all times?
Here is the Animation function:
private void recyclerViewAnimate(final RecyclerView recyclerView, LayoutAnimationController controller) {
if(isAnimating){
return;
}
controller = AnimationUtils.loadLayoutAnimation(recyclerView.getContext(), R.anim.layout_recycleview_disappear);
recyclerView.setLayoutAnimation(controller);
recyclerView.setLayoutAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
isAnimating = true;
bt_add.animate().rotationXBy(180).setDuration(400).start();
addButtonLayout.animate().setDuration(400).alpha(0).withEndAction(new Runnable() {
#Override
public void run() {
addButtonLayout.setVisibility(View.INVISIBLE);
}
});
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
recyclerView.setVisibility(View.GONE);
layoutSettings.setVisibility(View.VISIBLE);
layoutSettings.animate().scaleX(0.95f).setDuration(0).withEndAction(new Runnable() {
#Override
public void run() {
layoutSettings.animate().alpha(1)
.scaleY(1.01f).scaleX(1f).setDuration(400).withEndAction(new Runnable() {
#Override
public void run() {
settingsText.animate().setDuration(300).alpha(1).start();
isAnimating = false;
recyclerView.setLayoutAnimationListener(null);
}
}).start();
}
}).start();
}
});
recyclerView.scheduleLayoutAnimation();
}
Instead of setting scheduleLayoutAnimation(); I needed to use startLayoutAnimation();

animations unresponsive inside button click

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

How to start a function when animation has ended?

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.

TextView keeps popping up

I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}
If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);
You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);
You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.

How to continue processing after animation ended?

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

Categories

Resources