How to add animation before the opening of a new activity? - android

I would like to add an animation before the opening of a new Android Empty Activity. Something like a chroma-keyed video played on top of the current activity and at the end of it, the secondary activity opens up.

You create a splash activity that includes your animation, and you implement an AnimationListener. Inside the method onAnimationEnd() you create the intent that takes you to the second activity. Don't forget to set the splash activity as the Launcher activity on your manifest.
animationObject.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent (SplashActivity.this, MainActivity.class);
startActivity(intent); }
#Override
public void onAnimationRepeat(Animation animation) {
}
});
EDIT: if you want to play a video with media player instead you use a playback listener and run the same intent from onCompletion()

After your startActivity method use the overridePendingTranistion and put the animations in it
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(ActivityA.this, ActivityB.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});
The xml animations are as follows
enter.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
exit.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" />
</set>

Related

Animation not working properly in Activity while on start and destroy

i have 5 to 6 activities in my application I have added left to right and right to left animation on activity start and on finish activity but its not working properly when I click for new activity its working fine it start from right to left but when i press back button its also start from right to left. but when I press hardware back button its work from left to right. here is my code
right to left animation
leftin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>
leftout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"/>
rightin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"/>
rightout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"/>
here is code oncreate activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub__category);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
and here is code of my_back button
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
});
and here is code of my onbackpress override method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
here I am facing problem is that when I press hardware back button its work fine but when i press back button of my UI it not start from left to right but start new activity form right to left
Use this code:
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
Slide in right xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="800" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
Slide out left xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
write overridePendingTransition(R.anim.right_in, R.anim.right_out);
after finish(); method.
I don't think you need to invoke overridependingtransitions() method in onCreate().
hi here is how i achieved this animation. i know my thinking of achieving this is not professional way but finally i achieved it by my way.
here is i have activity A,B,C so on activity B i check that is it clicked from Activity A or C and according to that i used if condition and implement animation some way like below code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor = pref.edit();
String fromhome =pref.getString("from_home_list_adapter","");
if(fromhome.equals("yes"))
{
editor.putString("from_home_list_adapter","no");
editor.commit();
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
again i say this is not professional way to handle animation but its works

animation on pressing back button

I have added the following code to my activity and got the desired animation, but pressing the back button the animation is not the same I.e the activity just closes normally. How can I add animation when back button is pressed
public void notesAndCodeClick(View v){
Intent notesIntent = new Intent(MainActivity.this, NotesActivity.class);
ActivityOptions notesoptions = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
startActivity(notesIntent, notesoptions.toBundle());
}
Try this,
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
this.overridePendingTransition(R.anim.trans_right_in,
R.anim.trans_right_out);
}
Add both the files listed below to your anim folder
res --> anim
trans_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="-100%p"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0" />
</set>
trans_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toXDelta="100%p" />
</set>
You can set IN and OUT animation for Activity while pressing back button.
Left to Right animation :
Put this file in res/anim/left_to_right.xml :-
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
Right to Left animation :
Put this file in res/anim/right_to_left.xml :-
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
Now in onBackPressed() : -
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);
}
You have to just do one thing, call animation after finishing your activity like this.
finish();
overridePendingTransition(R.anim.nothing,R.anim.nothing);
Happy Coding....
To add an animation when the back button is pressed, you can use the onBackPressed () method of the Activity class.
Example
#Override
public void onBackPressed() {
super.onBackPressed();
// add your animation
}
Android documentation
public void onBackPressed ():
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

Android Animation Display prob

I wanna use an animation in my prog., but it is not seen on emulator. It opens MenuActivity class before animation resource.
Here is the code partition;
Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
ImageView girisLogo = (ImageView)findViewById(R.id.girisLogoImageView);
anim.reset();
girisLogo.clearAnimation();
girisLogo.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Intent intent = new Intent(GirisActivity.this,MenuActivity.class);
startActivity(intent);
GirisActivity.this.finish();
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {}
});
}
fade_in.xml:
<set xmlns:android="https://schemes.android.com/apk/res/android">
<alpha
android:fromAlpha = "0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="4000"
/>
</set>
I tried to use anim.serDuration(400); but result doesnt change.
Could you help me in this problem?
Change fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="4000" />
Also, if you want the new Activity to start when animation ends, place the code in onAnimationEnd()

Animate object out of the screen and keep it there

How can I move an object out of the screen?
This is how I move the TextView from under the screen into the screen.
slide_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="500" android:fillAfter="true"/>
</set>
This is how I move it out of the screen:
slide_down_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="100%" android:duration="500" android:fillAfter="true"/>
</set>
But the TextView appears again. Why?
That is because it renders it again after the animation and will actually place the TextView to its default position from the screen/layout.
solution:
You need to add a listener and set the visibility of your TextView to gone or invisible at the end of the listener.
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
You_text_view.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});

change image on translateanimation on android

I implement the translateAnimation to a imageview.
it Successfully animatated.
One translate is moving up and another one is moving down. i need to change image when start second translate.
My code is:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<translate
android:fromXDelta="0%" android:toXDelta="0%p"
android:fromYDelta="0%" android:toYDelta="20%"
android:drawable="#drawable/bs_bunny1"
android:duration="2000" android:startOffset="100"/>
<translate
android:fromXDelta="0%" android:toXDelta="0%p"
android:fromYDelta="0%p" android:toYDelta="-20%p"
android:duration="3000" android:startOffset="100"/></set>
I set the above animation to the imageview.
But i want to chage the image when start to load the second translate.. How to do it.
You need to break it up into two animations, and register a Animation.AnimationListener to the first translate animation. On Animation.AnimationListener's onAnimationEnd(Animation animation) callback, do the image change and then start the second animation, like thus:
translate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
view.setImageResource(resId);
view.startAnimation(translate2);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Categories

Resources