Animations's second method of using XML
The code is written in the Java file.
Advantages: Beginners Looking comfortable, code debugging are very convenient, breakpoints.
Disadvantage: The reusability of the code is too low . Object-oriented ultimate goal: reduce duplication of code .
The code is written in the XML file.
Pros: A and B controls can use an animated effect at the same time, improving reusability. the maintainability is getting higher . For example, the boss says: The ramp effect becomes smaller
Cons: Code syntax can be checked by the compiler, and debugging is laborious.
Steps for using animations tweenedanimations in XML form
1. Create a new folder named anim below the Res folder
2, create the XML file under the folder, and first add the set tag , the Label property is set as follows: Set represents the collection, a series of admations
<xmlns:android= "http://schemas.android.com/apk/res/android" Android : interpolator= "@android: Anim/accelerate_interpolator" android:shareinterpolator = "true" > </ Set >
3. add rotate, alpha, scale, or translate tags to the set tag
4, use animation Utils in code(usually Utils end of the class contains some static functions, tool methods) to load the XML file, and generate animation object
1. Alpha animation in the form of XML
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"Android:interpolator= "@android: Anim/accelerate_interpolator"> <AlphaAndroid:fromalpha= "1.0"Android:toalpha= "0.0"Android:startoffset= "$"android:duration= "$"> </Alpha></Set>
2. Triggering in Java
Use Animationutils to mount the animation settings file.
Imageview.startanimation (animation);
Private class Implements view.onclicklistener{ @Override publicvoid OnClick (View v) { = Animationutils. loadanimation (Mainactivity. This , R.anim.alpha); Imageview.startanimation (animation); } }
The previous execution of Animationset.addanimation () then executes the Admationset object.
How do you directly execute the animation object here?
First Startanimation (animation) accepts the parameter is the animation type, why the last wear animationset also no problem, because Animationset is the subclass of animation, upward transformation .
1, rotate animation in the form of XML
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"Android:interpolator= "@android: Anim/accelerate_interpolator"> <Rotateandroid:fromdegrees= "0"android:todegrees= "+350"Android:pivotx= "50%"Android:pivoty= "50%"android:duration= " the"> </Rotate></Set>
Three types of Pivox y:
- android:pivotx= "50" using absolute position positioning
- Android:pivotx= "50%" is positioned relative to the control itself
- android:pivotx= "50%p" relative to the control's parent control positioning
<Translate
Android:fromxdelta= "50%" Android:toxdelta= "70% "Android:fromydelta= "0%" Android:toydelta= "80% "Android:duration= " the"/> < ScaleAndroid:fromxscale= "1.0"Android:toxscale= "0.0" android:fromyscale= "1.0 "Android:toyscale= "0.0"Android:pivotx= "50%" android:pivoty= "50% "Android:duration= " the"/>
Use of Android--animations (ii)