Allow an animation to finish before starting a new activity - android

I would like to perform an animation on an image before switching to a new activity. I also want to make sure that the animation is fully completed before the new activity starts. Below is the code that I am using to accomplish the task.
public boolean onTouch(View view, MotionEvent event) {
Animation shake = AnimationUtils.loadAnimation(ViewReadingActivity.this, R.anim.shake);
imgAries.startAnimation(shake);
Intent intent = new Intent(ViewReadingActivity.this, ShowHoroActivity.class);
startActivity(intent);
return true;
}
The problem with this is that the animation does not run to completion before the new activity is presented and the animation will continue to run from where it was left. Is there a better way to accomplish this?

use like this here you can have animation listner
shake.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
Intent intent = new Intent(ViewReadingActivity.this, ShowHoroActivity.class);
startActivity(intent);
}
});

do this:
1. animation.setAnimationListener(), implement the methods
2. onanimationend method , write in the code to start a new activity
m late :(

Animation Listener Already Contains the Method called as onAnimationEnd #override it on your
imgAries Animation Listener
onAnimationEnd(Animation anim){
//Start you Activity here using Intent
Intent i=new Intent(this,//Your next Activity);
startActivity(i);
}
That it...Now you can go to Next Activity on Animation End...Thks

Related

How to automatically switch to another activity after animation ends

I am trying to do a splash screen in Android Studio. I have an image that I want to fade out (animation). Then, after the animation ends, I want the app to automatically switch to the Main Activity. With my current code, the Main Activity is displaying directly, without going through the animation first. And I don't understand why. I have updated the Android Manifest to specify that I want my Splash Activity to be launched. Still not working:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
fade();
}
public void fade() {
ImageView logo = (ImageView)findViewById(R.id.logo);
logo.animate().alpha(0f).setDuration(1700);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
However, if I remove the last 2 lines (about the Intent), then my animation displays. So it's as if the Intent makes Android bypass my animation altogether.
When you start an animation, that does not cause your code to stop until the animation completes. Instead, each frame of the animation are scheduled over time.
animate() returns a ViewPropertyAnimator, which you should use to register an AnimatorListener using setListener(). When the listener triggers onAnimationEnd(), then call startActivity to proceed.
logo.animate().alpha(0f).setDuration(1700).setListener(new AnimatorListener() {
// implement all the method with empty bodies, but this one is important:
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
};
It looks like you can just set an animation listener like this:
public void fade() {
ImageView logo = (ImageView)findViewById(R.id.logo);
ViewPropertyAnimator anim = logo.animate();
anim.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
anim.alpha(0f).setDuration(1700).start();
}
However, note that it's frowned upon to make dedicated Splash activities like this.
For the "correct" way to do it, see here....
ObjectAnimator fade = ObjectAnimator.ofFloat(logo, View.ALPHA, 0);
fade.setDuration(1700);
fade.addListener(new AnimatorListener() {
...
#Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
...
});
You can try this in your SplashActivity onCreate() method :
ImageView logo = (ImageView)findViewById(R.id.logo);
logo.animate().alpha(0f).setDuration(1700);
new Handler(Looper.getMainLooper()).postDelayed(new Runnable()
{ #Override
public void run()
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
},2000) ;
This will show your animation and after 2 seconds, will navigate to the MainActivity.
Also, since the introduction of MaterialTheme, you can use the Branded Launch Screen instead of creating the SplashScreen. It is very simple to implement and you may refer to this article by Antonio Leiva.

how to clear aftereffects of animation

I have 2 views that are being animated by sliding in and out of the screen simultaniously
this is the code for it: How to animate a slide in notification view that pushes the content view down
However, when the animation slides in, i must clear the animation, in order for the click events to work properly, otherwise they are also translated (which is not what i want)
What do i need to put in onAnimationEnd in the listener in order to clear the animation and have the view remain the way it looks after it has been animated
this is my current code:
greenViewSlideIn(1500, 0, new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
greenView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(blueView.getWidth(), blueView.getHeight());
layoutParams.setMargins(blueView.getLeft(), blueView.getTop() - greenViewHeight, blueView.getRight(), blueView.getBottom());
blueView.setLayoutParams(layoutParams);
greenView.clearAnimation();
// blueView.clearAnimation();
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
Two things you can do:
Make sure the setFillAfter flag on the animation is set to false. Then there's no need to an AnimationListener
Once the animation ends, call view.setAnimation(null);
Hope this helps :)

Android RotateAnimation bug

I have a RotateAnimation attached to an ImageButton which upon click rotates it and using OnAnimationEnd starts a new Activity.
Problem is its not working. After I close my application and come back, I am inside the new Activity(..) and when I go back, then the animation executes. I want the animation to happen and then start the new Activity.
For some reason, it was working absolutely fine before using the same code but I don't know, some trivial change may have affected it.
Here's the code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu);
ImageButton amazingPicsButton = (ImageButton) findViewById(R.id.amazingPics),
setViewOnClick(amazingPicsButton, new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));
}
/**
* Generic OnClick setter method for giving various View objects a click listener
* #param b
* #param intent
*/
private <B> void setViewOnClick(B b, final Intent intent){
((View) b).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
amazingPicsSound = createRandButSound();
amazingPicsSound.start();
rotateAndNewActivity(v, intent);
}
});
}
/** function that produces rotation animation on the View v.
* Could be applied to button, ImageView, ImageButton, etc.
*/
private void rotateAndNewActivity(View v, final Intent intent){
// Create an animation instance
Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);
an.setDuration(50); // duration in ms
an.setRepeatCount(3); // -1 = infinite repeate
/*we override the Animation an object to include the start of an new Activity
at the end of animation */
an.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
}
//start the activity onAnimationEnd
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
startActivity(intent);
}
});
// Set the animation's parameters
v.setAnimation(an);
}
setAnimation only sets the next animation to play on the View. To start an animation immediately, use startAnimation
In your case, use v.startAnimation(an);

Should an animation be placed in a runnable?

In my application , i m applying an animation on layout. This animation put the layout off the screen and put it in from the other side :
private void slideAnimation(final int sens)
{
Animation animOut = null;
if(sens == -1) {
animOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_left);
} else {
animOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_right);
}
animOut.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) {
Animation animIn = null;
if(sens == -1) {
animIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right);
} else {
animIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
}
camLayout.startAnimation(animIn);
}
});
camLayout.startAnimation(animOut);
}
When needed i simply call slideAnimation().
It's working fine , but sometime we can see animation in a runnable. Should i consider using an other solution to slide out and in my layout or my code is OK ?
Thanks
seems ok to me! I gather you are just trying to schedule one animation to play after the another. And this code will do the job. I am not sure but i suppose the animation end listener does already listen from a different thread. So i don't think a new thread is required.
IF this code is working as per your expectation then it's well and good. The problem with runnable is it will create on more process separately parallel to main thread and it will eat up main thread's memory and it will make your UI performance weak.

android temporarily use setEnabled(false)

I am animating an ImageView so that when you click the image the animation occurs (then later it resets) but my problem is that if you click again where the image sat originally - the animation starts from the beginning right away with out finishing (it just resets and starts again.
so I tried using
setEnabled(false)
which works great, the animation continues on its path up perturbed by any random clicking, now the only problem is getting the ImageView enabled again - at about the same time as the animation stops
here's what i have
stopImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mpButtonClick.start();
stopImage.setEnabled(false);
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
stopImage.startAnimation(anim);
now is there an easy way i can call setEnabled(true) after a some time has passed?
You could try using an AnimationListener and then calling setEnabled(true) from onAnimationEnd().
Something like this:
stopImage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
mpButtonClick.start();
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
anim.setAnimationListener(new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
stopImage.setEnabled(false);
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
stopImage.setEnabled(true);
}
});
stopImage.startAnimation(anim);
}
}
Here's the documentation for AnimationListeners.

Categories

Resources