Animation can also be stored in an XML file, which improves the maintainability of the program. To write an animation in XML, follow these steps:
1. Create a folder named anim under the res folder
2. Create an xml file and add the set tag first. Modify the tag as follows:
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
</Set>
3. Add the rotate, alpha, scale, or translate tag to the tag.
4. Use AnimationUtils in the code to load the xml file and generate the Animation object.
Alpha Animation
[Java]
<SPAN style = "FONT-SIZE: 18px"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Alpha
Android: fromalphi = "1.0"
Android: toAlpha = "0.0"
Android: startOffset = "500"
Android: duration= "2000"
/>
</Set> </SPAN>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Alpha
Android: fromalphi = "1.0"
Android: toAlpha = "0.0"
Android: startOffset = "500"
Android: duration= "2000"
/>
</Set> Animation a = AnimationUtils. loadAnimation (this, R. anim. alpha );
Iv. startAnimation ();
Scale Animation
[Html]
<SPAN style = "FONT-SIZE: 18px"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Scale
Android: fromXScale = "1.0"
Android: toXScale = "0.0"
Android: fromYScale = "1.0"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "2000"
/>
</Set> </SPAN>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Scale
Android: fromXScale = "1.0"
Android: toXScale = "0.0"
Android: fromYScale = "1.0"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "2000"
/>
</Set>
Rotate Animation
[Html]
<SPAN style = "FONT-SIZE: 18px"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Rotate
Android: fromDegrees = "0"
Android: toDegrees = "400"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "3000"
/>
</Set> </SPAN>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Rotate
Android: fromDegrees = "0"
Android: toDegrees = "400"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "3000"
/>
</Set>
Translate Animation
[Html]
<SPAN style = "FONT-SIZE: 18px"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Translate
Android: fromxdel= "50%"
Android: toXDelta = "100%"
Android: fromYDelta = "50%"
Android: toYDelta = "100%"
Android: duration= "3000"
/>
</Set> </SPAN>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/accelerate_interpolator">
<Translate
Android: fromxdel= "50%"
Android: toXDelta = "100%"
Android: fromYDelta = "50%"
Android: toYDelta = "100%"
Android: duration= "3000"
/>
</Set>
Here we will focus on android: javastx and android: policty and android: fromXDelta, android: toXDelta
Android: Required Tx = "50" use absolute coordinates
Android: relative Tx = "50%" relative to yourself
Android: relative Tx = "50% p" relative to parent Control