First of all, say, has not written a blog for one months, and not lazy, but always feel that the time is not enough, think carefully today, write a blog time or can squeeze out, but I did not arrange a good time, after all, this is also a summary of learning knowledge, should adhere to, so from today onwards, I have to continue , say to yourself, come on!
Today to animation study to make a summary:
Always think the animation is cool, after learning the animation only to know that the original is so!
The Android animation is divided into two types:
(1) Frame animation frames Animation: A picture is a frame, a lot of frames are constantly switching to make up the animation
(2) Motion tween Tween Animation: Animate a picture by rotating (Rotate), panning (Translate), scaling (scale), transparency (Alpha) changes
First, set the animation resource in XML, then load and start the animation in mainactivity
Implementation of frame animations:
1. Create Animation Resource folder Anim under Res resource file, add frame_anim.xml layout file
<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:oneshot= "false" >
<item android:drawable= "@drawable/img001" ></item>
<item android:drawable= "@drawable/img002" ></item>
<item android:drawable= "@drawable/img003" ></item>
<item android:drawable= "@drawable/img004" ></item>
<item android:drawable= "@drawable/img006" ></item>
</animation-list>
2. Set the background as an animated resource on the content panel to be displayed
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:id= "@+id/content"
android:orientation= "Vertical"
android:background= "@anim/frame_anim"//Set up here
tools:context= "${relativepackage}.${activityclass}" >
</LinearLayout>
3. Set the animation to start in mainactivity when clicking on the display panel
LinearLayout content= (linearlayout) Findviewbyid (r.id.content);
Animationdrawable animation= (animationdrawable) content.getbackground (); The event listener is written inside
Animation.start ();
The implementation of the Tweened animation:
1. Add an XML layout file to the Anim Animation resource folder
Rotating:
<rotate
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"//animation change speed during execution, control transformation mode
android:fromdegrees= "0"//Starting angle
android:todegrees= "270"//End Angle
android:pivotx= "0.5"
android:pivoty= "0.5"///Rotary Center 0.5,0.5 means center of rotation in the centre of the picture
android:duration= "3000"//Animation duration
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
</rotate>
Translation:
<translate
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
Android:fromxdelta= "0"
Android:fromydelta= "0"
Android:toxdelta= "500"
Android:toydelta= "500"
Android:duration= "3000"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
</translate>
Scaling:
<scale
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
android:fromxscale= "0"
android:toxscale= "1"
android:fromyscale= "0"
android:toyscale= "1"
Android:duration= "3000"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
</scale>
Transparency:
<alpha
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
Android:fromalpha= "0"
Android:toalpha= "1"
Android:duration= "3000"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
</alpha>
2. Load the animation resource in mainactivity and start the animation
Animation rotate_anim=animationutils.loadanimation (Getapplicationcontext (), R.anim.rotate_anim); Load Rotation animation
Let a picture achieve this animation effect
Image.startanimation (Rotate_anim);
Second, add animation resources in the mainactivity and start the animation
The implementation of the Tweened animation:
Alphaanimation alpha_anim=new alaphaanimation (float fromdegree,float todegree);
To set properties:
Alpha_anim.setduration (1000); Duration
Alpha.setfillafter (Boolean B); Whether to stay in the current position after the animation ends
Alpha.setfillbefore (Boolean B); Whether to return to the starting position after the animation ends
Start animation
Image.startanimation (Alpha_anim);
Alpha_anim.setstartoffset (); Set the time before the animation starts
Alpha_anim.setrepeatcount (); Number of repetitions
Alpha_anim.setinterpolator (); Gradient mode
Third, the use of Animationset
Using Animationset, you can combine several animations into one animation,
Animationset animationset=new Animationset (true);
Animationset.addanimation (Alpha_anim);
Animationset.addanimation (Rotate_znim); This combines the transparency animation and the rotation animation into an animation
Iv. Use of Layoutanimationcontroller
The function of Layoutanimationcontroller is that a component in a layout can be displayed in the form of an animation you set, where all the components are displayed the same way.
For example, there is now a ListView: So how to make each row of the ListView appear row by line
1. Set the display mode animation:
Scaleanimation scale_anim=new scaleanimation (0,1,0,1);
Scale_anim.setduration (1000);
2. Set Layoutanimationcontroller
Layoutanimationcontroller lacon=new Layoutanimationcontroller (scale_anim,0.5f); 0.5f refers to the time interval shown on the previous line and on the writing line;
Set properties for Lacon: Lacon.setorder (); Set the orientation of the display
3. Associating a ListView with an animation
Listview.setlayoutanimation (Lacon);
Five, how to customize a animation
1. Create a class to inherit animation and override the Applytransformation () method in it
public class Zidingyianimation extends Animation {
@Override
protected void Applytransformation (float interpolatedtime, transformation t) {
TODO auto-generated Method Stub
T.getmatrix (). Settranslate (200*interpolatedtime, 200*interpolatedtime);
Super.applytransformation (Interpolatedtime, T);
}
}
This is done by matrix transformations to achieve our animation, where Interpolatedtime is a number from the 0-1 increment, that is 0,0.0000000......1,0.0000000000002 until 1
2. In Mainactivity, get the animation we defined and set the properties for it:
Zidingyianimation my_anim=new zidingyianimation ();
My_anim.setduration (1000);
3. Associate an animation with our pictures
Image.startanimation (My_anim);
Vi. Use of Animationlistener
For example, we can set up an animated listener event for the My_anim animation we've defined above.
My_anim.setanimationlistener (New Animationlistener () {
@Override
public void Onanimationstart (Animation Animation) {
TODO auto-generated Method Stub
}
@Override
public void Onanimationrepeat (Animation Animation) {
TODO auto-generated Method Stub
}
@Override
public void Onanimationend (Animation Animation) {
TODO auto-generated Method Stub
Toast.maketext (Mainactivity.this, "End,", Toast.length_short). Show ();
}
});
In each of the above methods, we can write our handling events!
So far animation content I know so much, after the study will certainly encounter a lot of knowledge about animation, and then slowly accumulate it!
Android Animation (Animation) Summary