As a blog "Android One of the animations: drawable Animation". Android animation is divided into three main parts. The previous blog has explained the use of drawable animation, that is, to display the picture frames by frame, often used to dynamically display a progress animation, which is the most frequent application scenario. Next. This article is going to be gradual. Describes the view Animation.
View Animation is also a lot of the books we usually call the tweened Animation (someone translated as tweened animation). View animation is divided into 4 categories: Alphaanimation,rotateanimation,scaleanimation,translateanimation, respectively, corresponding transparency. rotation, size, displacement four kinds of changes.
The effect of the View animation is determined by four factors: 1) The initial state. 2) End State, 3) duration, 4) interpolator.
So to define a view Animation, you just need to define the above four factors, how the process changes in the middle of the system itself to calculate the initiative.
The first 3 factors are very easy to understand, the fourth factor interpolator more special, the word does not know how to translate more suitable. The translation of very many books is very strange. Interpolator is the change in the speed at which the animation is determined. For example, you move a button from the left side of the screen to the right side of the screen. Can keep it moving faster. Or slow down and slow down first. This is the role that interpolator play, the detailed use of the following will say, first from the simple.
Like drawable animation, you define a view animation in a way that can be used in code or in an XML file. Let's start by writing the simplest example of moving a HelloWorld string. Rotating. Amplification. Darken. Implemented in code and XML files, respectively.
First implemented in code.
First create a new Project:viewanimationdemo and create a new layout file: Animationjavacode.xml, for example, the following:
Viewanimationdemo\res\layout\animationjavacode.xml
<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/translation "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_marginbottom= "50DP" android:text= "I Want to move"/> <Tex TView android:id= "@+id/rotate" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:layout_below=" @id/translation "android:layout_marginbottom=" 50DP "android:text=" I want to rotate "/> ; <textview android:id= "@+id/scale" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/rotat E "android:layout_marginbottom=" 50DP "android:text=" I Want to Grow "/> <textview android:id=" @+id/alph A "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_below=" @id /scale "android:layout_marginbottom=" 200DP "android:text=" I want to darken "/> <button android:id=" @+id/ Fire "android:layout_width=" Match_parent "android:layout_height=" 50DP "android:layout_below=" @id/alph A "android:text=" movement "/></relativelayout>"
The corresponding activity for the XML layout file is as follows:
Viewanimationdemo/src/com/csdn/viewanimationdemo/animcodeactivity.java
public class Animcodeactivity extends Activity {private TextView translation;private TextView rotate;private TextView SCA Le;private TextView alpha;private button button; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.animationjavacode); Translation = (TextView) Findviewbyid (r.id.translation); Rotate = (TextView) Findviewbyid (r.id.rotate); Scale = (TextView) Findviewbyid (R.id.scale); Alpha = (TextView) Findviewbyid (R.id.alpha); Button = (button) Findviewbyid (R.id.fire); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//1&2: Determine the starting state, end state translateanimation Tanim = new Translateanimation (0, 400, 0, 0);//Lateral Displacement 400 units rotateanimation Ranima = new Rotateanimation (0, 70);//clockwise rotation 70 degrees scaleanimation Sanima = new ScaleaniMation (0, 5, 0, 5);//zoom in 5 times times, vertical magnification 5 times times alphaanimation Aanima = new Alphaanimation (1.0f, 0.0f);//from full opacity to full transparent//3: Determine duration tanim.setduration (ranima.setduration), sanima.setduration (+), Aanima.setduration (2000);//4: Determine Interpolatortanim.setinterpolator (new Acceleratedecelerateinterpolator ());//Start Animation translation.startanimation ( Tanim); rotate.startanimation (Ranima); scale.startanimation (Sanima); alpha.startanimation (AAnima);}); }}
It is not difficult to see from the code, first determine the starting state and end state of the animation, and then set the duration through Setduration (2000), assuming that the duration is not set here, will default to feel 30mx. The basic naked eye cannot see. Finally we set up a interpolator, where the effect is that the animation slows down first and then slows down.
The default state is constant.
That's right. The basic way to use interpolator is so easy. The above code is for example:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq0haaxjveq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Next explain how to use XML definition animation, according to the introduction of the Android website, the definition of the View animation XML file format, such as the following:
<?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 andro id: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 oid:fromdegrees= "float" android:todegrees= "float" android:pivotx= "float" android:pivoty= "float"/> <set> </set></set>
The file can only have one root node, which can be <set>,<alpha>,<scale>,<translate>,<rotate>, while,<set> Nodes can include child nodes, which can include <set>,<alpha>,<scale>,<translate>,<rotate>, and so on.
The two tag attributes that need to be explained above are Android:interpolator and Android:shareinterpolator. The former represents the interpolator you are using. Can be the system comes with, can also be its own definition.
The latter represents whether the interpolator is shared with the child nodes. The properties of other sub-tags are very easy to understand, basically look at the property name to understand. In addition to the two, Android:pivotx and Android:pivoty, we know that pivot means the axis meaning, so these two properties define the axis position of the animation change, the default is the upper left corner, when we assign both of them to a value of 50%, Then the axis of change is centered.
Next, write a detailed example, such as the following, or in the project just now. In general we put the XML file that defines the animation in the Res/anim folder, first we create a new Anim folder. Then create a new XML file, such as the following:
Viewanimationdemo/res/anim/myanim.xml
<set xmlns:android= "Http://schemas.android.com/apk/res/android" android:shareinterpolator= "false" > < Scale android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" android:fromxscale= "1.0" android:toxscale= "1.4" android:fromyscale= "1.0" android:toyscale= "0.6" android:pivotx= "50%" Android:pivoty= "50%" android:fillafter= "false" android:duration= "+"/> <set android:inter Polator= "@android: Anim/accelerate_interpolator" android:startoffset= "> <scale android: fromxscale= "1.4" android:toxscale= "0.0" android:fromyscale= "0.6" android:toyscale= "0.0" "50%" android:pivoty= "50%" android:duration= "android:pivotx="/> <rotate android:fromdegrees= "0" android:todegrees= "android:toyscale=" 0.0 "android:p ivotx= "50%" Android:Pivoty= "50%" android:duration= "/>" </set></set>
The examples above are as follows:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq0haaxjveq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
In the code above. There is only one root node <set> it has two child nodes <scale> and <set>, and then the child nodes in it <set> have two of their own child nodes <scale> and <rotate>.
Explain some of the label properties that you haven't seen above.
Andoird:fillafter: In the previous example, after the end of our animation HelloWorld back to the original state, by setting Fillafter to True, the animation will remain in the end state. But. As the previous blog "One of Android Animation: drawable Animation" said, the animation effect of the View Animation is drawn, not the component really moved, this problem we might continue to explore.
Now just need to know to set Fillafter to true. The animation remains in the end state, which is mostly applied to the design of the animation that occurs continuously.
Startoffset: This property defines how long the animation is delayed, and with this setting, we are able to design some pre-and post-order animations, except, of course, that the other animation has to be set Fillafter to true in addition to the last one.
Interpolator: Here we use the system's own interpolator
Next we define a layout file that has only one picture. A button that animates an image by tapping the button, which is relatively simple and not listed here. How do I start an animation in the corresponding activity code for this layout file, such as the following code:
Viewanimationdemo/src/com/csdn/viewanimationdemo/animaxmlactivity.java
public class Animaxmlactivity extends Activity {private Button btn;private ImageView img; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_anima_xml); btn = (Button) Findviewbyid (R.ID.XML_BTN); img = (ImageView) Findviewbyid ( R.ID.XMLANIMIMG); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubanimation Anim = Animationutils.loadanimation (Animaxmlactivity.this, R.anim.myanim); Img.startanimation (Anim);}});}}
The above code is very easy to understand. We typically use animationutils to read animations that are defined in an XML file.
So far, the basics of view animation have been largely covered. Then write a detailed example of the practice, to achieve the commonly seen side slip function, such as NetEase news:
This side-slip effect is not difficult to achieve. A lot of people use asynctask threads, but as a result you'll find that there are sometimes noticeable stuttering, but let's assume that the animation will be much smoother. We then implement this feature:
Create a new project first: Swipewithanim.
and create a new layout file, Activity_main.xml. For example, the following
Swipewithanim/res/layout/activity_main.xml
<framelayout 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 "Tools:context =". Mainactivity "> <view android:id=" @+id/content "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" android:background= "#e23451"/> <linearlayout android:id= "@+id/menu" Android:layout_width= "400DP" android:layout_height= "match_parent" android:layout_gravity= "left" android:layout_marginleft= " -380DP" android:orientation= "horizontal" > <view android:layout _width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" android:backg round= "#eeeee1"/> <view android:id= "@+id/dragarea" android:layout_width= "20DP" android:layout_height= "Match_parent" android:background= "#00000000"/> </LinearLayout></FrameLayout>
The content is displayed on the screen. On the left side of the menu, the knowledge puts the transparent part of 20DP on the left side of the screen to trigger the Ontouch event. The corresponding activity code for the XML file is as follows:
Swipewithanim/src/com/csdn/swipewithanim/mainactivity.java
public class Mainactivity extends Activity {private View menu;private final static int show_menu = 1;private final static int hide_menu = -1;private int swipe_tag = show_menu;private int max_menu_margin = 0;private int min_menu_margin;private f Loat beginx;private float latestx;private float diffx;private float latestmargin;private framelayout.layoutparams lp;/* * (non-javadoc) * * @see android.app.activity#oncreate (android.os.Bundle) */@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main); menu = Findviewbyid (r.id.menu); LP = (Framelayout.layoutparams) Menu.getlayoutparams (); min_menu_margin = Lp.leftmargin;menu.setontouchlistener (new Ontouchlistener () {@ Overridepublic boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubint action = Motioneventcompa T.getactionmasked (event); switch (action) {Case MotionEvent.ACTION_DOWN:beginX = Event.getx(); Break;case MotionEvent.ACTION_MOVE:latestX = Event.getx ();d iffx = Latestx-beginx;swipe_tag = diffx > 0?
Show_menu:hide_menu;latestmargin = Lp.leftmargin + diffx;if (Latestmargin > min_menu_margin&& latestMargin & Lt Max_menu_margin) {lp.leftmargin = (int) (latestmargin); menu.setlayoutparams (LP);} Break;case MotionEvent.ACTION_UP:TranslateAnimation tanim;if (Swipe_tag = = show_menu) {Tanim = new Translateanimation (0 , Max_menu_margin-latestmargin, 0, 0); Tanim.setinterpolator (new Decelerateinterpolator ()); tanim.setduration (800); Menu.startanimation (Tanim);} else {Tanim = new Translateanimation (0, min_menu_margin-latestmargin, 0, 0); tanim.setduration (800); Menu.startanimation (Tanim);} At the end of the animation, move the location of the menu. Make the menu really move. Tanim.setanimationlistener (New Animationlistener () {@Overridepublic void Onanimationstart (Animation Animation) {// Todo auto-generated method stub} @Overridepublic void Onanimationrepeat (Animation Animation) {//Todo auto-generated Method stub} @Overridepublic void Onanimationend (Animation Animation) {//TODO auto-generated method stubif (Swipe_tag = = S How_menu) {LP.LEFTMArgin = MAX_MENU_MARGIN;MENU.SETLAYOUTPARAMS (LP);} else {lp.leftmargin = MIN_MENU_MARGIN;MENU.SETLAYOUTPARAMS (LP);} Menu.clearanimation ();}}); break;} return true;}});}}
As above code. Listen for the Ontouch event on the menu and move the menu based on the move distance. When the finger is picked up, start the animation so that the move proceeds to the end.
As we mentioned above, the View animation is just the result of the drawing, and the components of the animation are not really moving, so we are listening to animationlistener in order to solve the problem here. At the end of the animation, move the position of the menu to the end of the animation so that the menu is moved successfully. For example, the following. Because of the software relationship, it looks like Chuck, and the test on the real machine is completely smooth.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq0haaxjveq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Finally, I enclose the two source code before and after this blog:
Viewanimationdemo
Swipewithanim
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Android Animation II: View Animation