Today, we are going to learn how we make animation in Android through XML.
Animation Resources
An animation resource can define one of two types of animations:
Property Animation
Creates an animation by modifying an object’s property values over a set period of time with an Animator.
View Animation
There are two types of animations that you can do with the view animation framework:
Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation.
Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable.
Property Animation
An animation defined in XML that modifies properties of the target object, such as background color or alpha value, over a set amount of time.
- FILE LOCATION:
res/animator/filename.xml
The filename will be used as the resource ID.COMPILED RESOURCE DATATYPE:
Resource pointer to a ValueAnimator, ObjectAnimator, or AnimatorSet.Let us Understand with Syantax :-
-
<set android:ordering=["together" | "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </set> </set>
The file must have a single root element: either <set>
, <objectAnimator>
, or <valueAnimator>
. You can group animation elements together inside the <set>
element, including other <set>
elements.
ELEMENTS:
<objectAnimator>
Animates a specific property of an object over a specific amount of time. Represents an ObjectAnimator.
attributes:
android:propertyName
- String. Required. The object’s property to animate, referenced by its name. For example you can specify
"alpha"
or"backgroundColor"
for a View object. TheobjectAnimator
element does not expose atarget
attribute, however, so you cannot set the object to animate in the XML declaration. You have to inflate your animation XML resource by calling loadAnimator( ) and call setTarget( ) to set the target object that contains this property. android:valueTo
- float, int, or color. Required. The value where the animated property ends. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:valueFrom
- float, int, or color. The value where the animated property starts. If not specified, the animation starts at the value obtained by the property’s get method. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:duration
- int. The time in milliseconds of the animation. 300 milliseconds is the default.
android:startOffset
- int. The amount of milliseconds the animation delays after start() is called.
android:repeatCount
- int. How many times to repeat an animation. Set to
"-1"
to infinitely repeat or to a positive integer. For example, a value of"1"
means that the animation is repeated once after the initial run of the animation, so the animation plays a total of two times. The default value is"0"
, which means no repetition. android:repeatMode
- int. How an animation behaves when it reaches the end of the animation.
android:repeatCount
must be set to a positive integer or"-1"
for this attribute to have an effect. Set to"reverse"
to have the animation reverse direction with each iteration or"repeat"
to have the animation loop from the beginning each time. android:valueType
- Keyword. Do not specify this attribute if the value is a color. The animation framework automatically handles color values
Value Description intType
Specifies that the animated values are integers floatType
(default)Specifies that the animated values are floats
<animator>
Performs an animation over a specified amount of time. Represents a ValueAnimator.
attributes:
android:valueTo
- float, int, or color. Required. The value where the animation ends. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:valueFrom
- float, int, or color. Required. The value where the animation starts. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:duration
- int. The time in milliseconds of the animation. 300ms is the default.
android:startOffset
- int. The amount of milliseconds the animation delays after start() is called.
android:repeatCount
- int. How many times to repeat an animation. Set to
"-1"
to infinitely repeat or to a positive integer. For example, a value of"1"
means that the animation is repeated once after the initial run of the animation, so the animation plays a total of two times. The default value is"0"
, which means no repetition. android:repeatMode
- int. How an animation behaves when it reaches the end of the animation.
android:repeatCount
must be set to a positive integer or"-1"
for this attribute to have an effect. Set to"reverse"
to have the animation reverse direction with each iteration or"repeat"
to have the animation loop from the beginning each time.
Here is an Example of Property Animation:-
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="alpha" android:valueFrom="1.0" android:valueTo="0.0" android:duration="0" /> <objectAnimator android:propertyName="rotationY" android:valueFrom="-180" android:valueTo="0" android:interpolator="@android:interpolator/overshoot" android:duration="10000"/> <objectAnimator android:propertyName="alpha" android:valueFrom="0.0" android:valueTo="1.0" android:startOffset="5000" android:duration="1"/> </set>
In java part don’t forget to add this :
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator); set.setTarget(myObject); set.start();
Now, let move to Tween Animation
View Animation
The view animation framework supports both tween and frame by frame animations, which can both be declared in XML. The following sections describe how to use both methods.
Tween animation
An animation defined in XML that performs transitions such as rotating, fading, moving, and stretching on a graphic.
- FILE LOCATION:
res/anim/filename.xml
The filename will be used as the resource ID.- COMPILED RESOURCE DATATYPE:
- Resource pointer to an Animation.
SYNTAX:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <alpha android:fromAlpha="float" android:toAlpha="float" /> <scale android:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="float" android:toYDelta="float" /> <rotate android:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> <set> ... </set> </set>
ELEMENTS:
SYNTAX:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" /> </animation-list>
ELEMENTS:
XML file saved at res/anim/rocket.xml
:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>
This application code will set the animation as the background for a View, then play the animation:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); rocketImage.setBackgroundResoucrce(R.drawable.rocket_thrust); rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); rocketAnimation.start();
Hope through these images all your qureies about animation will be slove.
Tranlation Animation :
Scale Animation :-
Alpha Animation :
Rotation Animation:
0 Comments