Animations appear, rotate and disappear - java

I want to do a multiple animations on my image (appear -> rotate -> disappear). I've got this code:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="0"
android:toAlpha="100" />
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="100"
android:toAlpha="0" />
</set>
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<rotate
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
</set>
Also in my java code:
animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
animRotate.setDuration((long) duration);
fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
s.addAnimation(animRotate);
s.addAnimation(fade_out);
image.startAnimation(s);
but unfortunately it doesn't work correctly...

you have severals errors in your animation xml files:
the duration property is in milliseconds, so 1ms is way too short for a noticeable fade in/fade out animation
the alpha property is a float between 0 et 1, 100 is way too much.
you don't need a set in your xml files if there is only one animation : just add the alpha or rotate tag as a root
So, you should have now these files:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0" />
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
Then, in your code, you need a add an offset between each animation. Otherwise, all the animations will be triggered at the same time. Moreover, the fillAfter flag must be set on the root animation object (here, your AnimationSet)
Animation animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
Animation fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
Animation fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
animRotate.setDuration((long) duration);
animRotate.setStartOffset(fade_in.getDuration());
s.addAnimation(animRotate);
fade_out.setStartOffset(fade_in.getDuration() + animRotate.getDuration());
s.addAnimation(fade_out);
s.setFillAfter(true);
image.startAnimation(s);

Related

Zoom in animation between activities

I am creating an Android application. I want to open the new activity with zoom in transition. I have some buttons in my current activity. When I click one button new activity will open from the button's position. I try to use the following code. But no transition will appear. I am using the following xml and java codes.
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
and zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="0.5"
android:toYScale="0.5" >
</scale>
</set>
And in my java code I call it like
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
With these code no effect will appear. Could someone please help me to finds out what is the issue?
Try to add the attribute: android:fillAfter="true"
Like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
place both animation xml files zoom_in.xml and zoom_out.xml in res/anim
directory and start animation like this :
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);

Animate View and keep visible after animation

I have two indicators, each need to be invisible to start. I want them to be visible after animating in, how do i do this? Right now they animate in then disappear, here is the code:
indicator.setVisibility(View.INVISIBLE);
indicator2.setVisibility(View.INVISIBLE);
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_view);
indicator.startAnimation(fadeInAnimation);
indicator2.startAnimation(fadeInAnimation);
My xml is:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="1500"
android:repeatCount="0" />
</set>
Try doing this: fadeInAnimation.setFillAfter(true)
or
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="1500"
android:repeatCount="0"
android:fillAfter="true"/>
</set>

Animate a TextView to slide off the screen

I'm animating a TextView with this code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:toYDelta="100%"
android:fromYDelta="0%" />
</set>
and doing this:
textView.startAnimation(slideOutAnim);
But this code only makes the TextView animate about 1/4 of the screen length. What should I set the toYDelta to so that it ensures that the TextView is out of the screen? I know I could just set it to a really high number but is there a more efficient/effective way to get this done? Thanks
change your animation to this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:toYDelta="100%p"<!--relative to parent-->
android:fromYDelta="0%" />
</set>
note that this work if it's parent have full screen height, actually it animate to 100% of height of it's parent

Center view during simple scale animation

I'm trying to do a scale in/out animation for activity/framgent transition
here are the two animations :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<scale
android:fromXScale="80%"
android:fromYScale="80%"
android:toXScale="80%"
android:toYScale="80%" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5" />
</set>
-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:fromXDelta="100%"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0" />
</set>
Use it like that :
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_to_behind);
The problem is that the activity doesn't scale down in the center of the screen, it stick to the top/left of the screen
Any idea what wm i missing ? Or doing wrong ?
Add a pivot to your scale animation. Add this on your xml:
android:pivotX="50%"
android:pivotY="50%"
Your scale should look like:
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="80%"
android:toYScale="80%" />

java animation android duration

This animation isn't animating it just instantly disappears no matter what duration I set
here is the java code and xml
Java Code:
Animation shrink =AnimationUtils.loadAnimation(Page.this, R.anim.shrink);
deleteMe.startAnimation(shrink);
XML File:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="true"
android:interpolator="#android:anim/linear_interpolator"
android:duration="10000">
<scale
android:duration="10000"
android:fillAfter="true"
android:fromXScale="1.0"
android:toXScale="0" >
</scale>
<alpha
android:duration="10000"
android:fromAlpha="1.0"
android:toAlpha="0">
</alpha>
</set>
Specify android:interpolator attribute of set tag with the value you desired.
This interpolator just moves the object from the start point to the end point (or rotation) at a steady rate.
for ex::
android:interpolator="#android:anim/linear_interpolator"
In one of my app i write this code to move a block from left to right and its working well
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" >
<rotate android:interpolator="#android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1500"
android:startOffset="1500"/>
<translate android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="3000"/>
<translate android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toXDelta="100%"
android:duration="1500"
android:startOffset="1500" />
</set>

Categories