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.
Related
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);
I am making an application on Android which generates one of the two available images on clicking a button and after a duration of 1 second, that image is supposed to fade away, giving the user a choice to click the button again.
The problem is that the animation works smoothly on the first button press (i.e. generates an image and then fades away), however on the second button press, the image just sits there and nothing happens. I can't figure out why.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
final Button clickMe = (Button)findViewById(R.id.button);
final TextView image_description = (TextView)findViewById(R.id.textView);
image_description.setText("");
final Animation fadeout = new AlphaAnimation(1,0);
fadeout.setStartOffset(1000);
fadeout.setDuration(1000);
secondimage.setAnimation(fadeout);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
fadeout.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
System.out.println("Animation block");
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
clickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("Click block");
Random r = new Random();
int i = r.nextInt(2);
clickMe.setVisibility(View.GONE);
if(i == 0) {
secondimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a CAT");
image_description.setVisibility(View.VISIBLE);
secondimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
} else {
firstimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a DOG");
image_description.setVisibility(View.VISIBLE);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
}
}
});
}
The Logs look something like this:
Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block
Any idea why the code is not reaching the Animation block on 2nd click onwards?
Alright. I was able to solve my own query.
I replaced
secondimage.setAnimation(fadeout);
with
secondimage.startAnimation(fadeout);
On doing so, the code was able to reach the onAnimationEnd function.
The easiest thing to do is create/inflate a new instance of the Animation type object for each view that needs animation. As you have it now, it's trying to reuse the same object.
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.
I have the following code in my activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isPreviouslyLoggedIn()) {
setContentView(R.layout.splash);
final TextView revolution=(TextView) findViewById(R.id.textView1);
final Button login=(Button) findViewById(R.id.loginButton);
final Button signUp=(Button) findViewById(R.id.signUpButton);
login.setOnClickListener(loginListener);
signUp.setOnClickListener(signUpListener);
}
else {
setContentView(R.layout.home);
Intent intent = new Intent(getApplicationContext(), PickUpActivity.class);
startActivity(intent);
}
}
When the if statements are executed, the layout is set to splash. However, in cases when the else statement is executed the layout is not set to home and the intent is directly started. Why is this happening and how can I prevent this?
Try this way..
protected boolean active = true;
protected int time = 5000;
Thread thread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (active && (waited < time)) {
sleep(100);
if (active) {
waited += 100;
}
}
} catch (InterruptedException e) {
} finally {
finish();
Intent Go_Activity = new Intent(Screen.this, otherActivity.class);
startActivity(Go_Activity);
stop();
}
}
};
thread.start();
It help you.
Thanks.
Set Any Time or Thread to wait some time to go next activity. When else statement executed then layout set but for few time. for this reason you do not able to show it.
Thanks
You know what are you trying to ask ?? After setting content view straightly you are starting the another activity so it will show the new(started)activity layout... what is wrong then ?
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.