Android:ViewFlipper animation - android

I have add a ViewFlipper in which has 2 linearlayout,and I have made an animation xml:
left_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="3000"/>
</set>
right_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="3000"/>
</set>
left_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="3000"/>
</set>
right_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="3000"/>
</set>
the "Next" button in one linearlayout which shows when first load the app:
mNext.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
//mViewFlipper.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));
mViewFlipper.showNext();
}
});
and the "Prev" button:
mPrev.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));
mViewFlipper.showPrevious();
}
});
The "Next" Button goes well, But the "Prev" Button goes strange: when I click the "prev",it first change into the previous view and then start the animation ,and at last it changes into the Previous view again! How to solve it??
Thanks in advance!!

You want to use setOutAnimation() and setInAnimation().

Well this a very old post. but still the fix is here:
you need to call viewFlipper.setOutAnimation(null) and viewFlipper.setInAnimation(null) to reset the animation .
#Override
public void onClick(View v)
{
if (v.equals(mNext))
{
mViewFlipper.setOutAnimation(null);
mViewFlipper.setInAnimation(null);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));
mViewFlipper.showNext();
}
else if (v.equals(mPrev))
{
mViewFlipper.setOutAnimation(null);
mViewFlipper.setInAnimation(null);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));
mViewFlipper.showPrevious();
}
}

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

Android - How to open an acitivity with the scale and fade effect

I have an app and I want to open an activity using this transition
(Google Maps does this too if you open the settings).
How can I achieve this?
write your code in oncreate method of next activity
Create XML file for fade-in animation, /res/anim/fadein.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"
/>
</set>
Create XML file for fade-out animation, /res/anim/fadeout.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"
/>
</set>
In your Activity
final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
buttonToNextActivity.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeIn);
}});
Same as it is find write in the SecondActivity and call the animation onBackPressed(){...}
Try adding the following line after starting the activity:
overridePendingTransition(R.anim.youranimation, R.anim.default_anim);
For example:
Intent intent = new Intent (context, YourActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.youranimation, R.anim.default_anim);
default_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_shortAnimTime"
android:fromYDelta="0%p"
android:toYDelta="0%p" />
Change values.

set a animation for several view and start one after another

I want to set a animation for several View.and start One after another(such below imag)
I create a translation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-6%p" android:toXDelta="50%p"
android:duration="2000"/>
and set for Views.
imag1=(ImageView) findViewById(R.id.imag_icon1);
imag2=(ImageView) findViewById(R.id.imag_icon2);
imag3=(ImageView) findViewById(R.id.imag_icon3)
anim1=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.several_anim);
int count=0;
ImageView [] arr_imag={imag1,imag2,imag3};
arr_imag[count].startAnimation(anim1);
anim1.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 anim) {
count++;
if(count<3)
arr_imag[count].startAnimation(anim1);
}
});
but when run app .
start Animation with together.
I'm really confused.
What is the problem
Try This.
anim1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
anim2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:startOffset="500"
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
anim3.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:startOffset="1000"
android:duration="3000"
android:fromXDelta="-6%p"
android:toXDelta="100%p" />
</set>
then use this
Animation animanim1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim1);
Animation animanim2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim2);
Animation animanim3 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim3);
imag1.startAnimation(animanim1);
imag2.startAnimation(animanim2);
imag3.startAnimation(animanim3);

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.

slide out animation not working on back press button?

I am using animations when entering or exiting the activity,entering to an activity animation works fine but exit animation does not work well. I start exit animation when i press back button.What happens is it first start enter animation for current activity then show the last activity what i want a simple exit animation on back button press.
Slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%p"
android:toXDelta="-100%p" >
</translate>
Slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0%p" >
</translate>
On Action bar back button pressed
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.home:
finish();
overridePendingTransition(R.anim.slide_out, R.anim.slide_in);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Firstly create one more animation i.e nothing.xml in your anim folder
nothing.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%"
android:toXDelta="0%" >
</translate>
here is your slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
and slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%"
android:toXDelta="100%" >
</translate>
Now call your NewActivity like this
startActivity(new Intent(CurrentActivity.this, NewActivity.class));
overridePendingTransition(R.anim.slide_in, R.anim.nothing);
and then on your back button click do this
finish();
overridePendingTransition(R.anim.nothing, R.anim.slide_out);
I used
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%"
android:toXDelta="-100%" >
</translate>
slide_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
slide_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%"
android:toXDelta="100%" >
</translate>
Code
Intent i=new Intent(Authentication.this,Login.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
To previous
finish();
overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
Add animation in onBackPressed, it will show the animation while clicking the back button.
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
I solved it by overriding the back button behavior.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
}
I've done something similiar and it works fine, you can change animate() with startAnimation(your_xml), you will also have to make activity background transparent:
<item name="android:windowBackground">#android:color/transparent</item>
This activity will slide down, and the MainActivity will be visible at the moment of sliding because of transparent background.
EDIT - with toolbar back button:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainlinear.animate()
.translationY(ScUtils.getScreenHeight(getApplicationContext()))
.setDuration(210)
.setInterpolator(new AccelerateInterpolator())
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
ThemeActivity.super.onBackPressed();
overridePendingTransition(0, 0);
}
}).start();
}
});
For the Activity enter animation use overridePendingTransition(R.anim.slide_out, R.anim.slide_in); in the onCreate(...) function.
For the exit animation place the same call in onPause(...).
I had a back/home button on my action bar which was not picking up the slide animation by overriding onBackPressed or finish. So I had to add this snippet from here. If using the same slide_enter and slide_exit as above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
// back button
case android.R.id.home:
finish();
overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
return true;
}
return super.onOptionsItemSelected(item);
}
In case somebody finds it useful.
When you navigate to or from an Activity, pop animations are not applied automatically.
Once the animation is added to the navigation XML, it must be added to the target activity an override of Activity.finish() with applyPopAnimationsToPendingTransition to have the pop exit animation.
override fun finish() {
super.finish()
ActivityNavigator.applyPopAnimationsToPendingTransition(this)
}

Categories

Resources