How to automatically switch to another activity after animation ends - android

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.

Related

Fragment's layout blinks after launching activity and calling finish() on the old activity

I'm calling finish() on a fragment's activity after launching a new activity. the problem is, when finishing the old activity, the layout and its finishing's animation shows up when the method is called.
I've tried to make the finish first, but it was horrible as i was able to see it.
Code:
if(getActivity() != null){
Glide.with(getActivity()).load(R.mipmap.ic_launcher).into(mIcon);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_splash_icon_pulse_scale);
mIcon.startAnimation(myFadeInAnimation);
int mRandom = new Random().nextInt(mMemesArray.length);
mMemeText.setText(mMemesArray[mRandom]);
try{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getActivity(), ActivityMain.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getActivity().finish();
}
},1000 * 2);
}
},1000 * 3);
} catch (Exception e){
e.printStackTrace();
}
}
I've tried to remove the views, cancel the animated ImageView. Had the same result.
As far as I understand this is caused by activity transition overlap. To overcome this issue I have used the following values in the onCreate() methods of both activities:
getWindow().setAllowEnterTransitionOverlap(false);
getWindow().setAllowReturnTransitionOverlap(false);

imageview movement in relative layout

I am trying to make a splash screen which has got 2 images .1st one is at center and moves up, 2nd one comes in when 1st goes up completely.
I have done the animation for the 1st image but the second one is not working in the desired manner. Actually I am not quite sure do i use imageswitcher or any other function. Please let me know what shall i do.
I would share the codes here.
public class Splash_screen extends Activity { // SPLASH ACTIVITY
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
ImageView kfcpic = (ImageView) findViewById(R.id.kfclogo);
ImageView sogood= (ImageView)findViewById(R.id.sogood);
TranslateAnimation animation = new TranslateAnimation(0,0,0,-500); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(3000); // animation duration
//animation.setRepeatCount(0); // animation repeat count
animation.setRepeatMode(1);
animation.setFillAfter(true);
// repeat animation (left to right, right to left )
//animation.setFillAfter(true);
kfcpic.startAnimation(animation); // start animation
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(5000);
runOnUiThread(new Runnable() {
public void run() {
Intent mainIntent = new Intent(Splash_screen.this, MainActivity.class);
startActivity(mainIntent);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
}
Maybe you didn't set the visibility of sogood ImageView correctly. It would be better if you could provide the layout of this SplashActivity.

Allow an animation to finish before starting a new activity

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

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

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