One Android Animation: Drawable Animation
I am going to write a few blogs to explain the Android animation. First, I will introduce the overall outline of the Android animation.
Android animation is mainly divided into three categories:
View Animation
Drawable AnimationProperty Animation among them, Drawable Animations is the easiest to understand for most people. In fact, it is the frame-by-frame animation mentioned in many books ). Property Animation and View Animation are relatively confusing. The following describes the differences between them. View Animation has two disadvantages: (1) View Animation can only modify partial attributes of a component (View Object), such as scaling and rotation ), however, the background color of the component cannot be modified. (2) View Animation: After a widget moves an Animation effect for a period of time, for example, moving from the left side of the screen to the right side, the entire process is actually the effect drawn, the actual position of the component is still on the left. The component can be triggered only when the position on the left is clicked. To really move a component, you need to add code implementation after the animation ends.
Property Animation has no restrictions on View Animation. Property Animation can modify any attribute of any Object (View Object or non-view Object), such as size, rotation, and color. In addition, the position of the moved component also changes with the change.
Property Animation is recommended on the Android official website, but View Animation also has its advantages: ease of use, so it is also a good choice when View Animation can easily and quickly solve the needs.
We have a general understanding of Android Animation. Next we will introduce Drawable Animation that is easier to learn. As mentioned above, Drawable Animation is a frame-by-frame Animation, so you must define each frame before using it. We can use code definition or xml file definition. The latter is generally used. As follows:
Android: oneshot = "true" indicates that the animation is played only once. If it is equal to false, it is played cyclically. The tag defines the image displayed for each frame. Display order according Define the sequence. Next we will introduce an actual scenario, that is, our common loading interface. There is only one center ImageView in the interface layout, which is relatively simple and will not be listed in the layout file. More importantly, we need to define an xml file to describe each frame, as shown below:
DrawableAnimationDemo \ res \ drawable \ loading. xml
The preceding xml file is not the same as the previous xml file. Label. This label is easy to use, The contained images are stacked and displayed at the same frame. As follows:
+ =
Check the activity file on the main interface as follows:
DrawableAnimationDemo \ src \ com \ example \ drawableanimationdemo \ MainActivity. java
Public class MainActivity extends Activity {private AnimationDrawable loadingAnimation; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); // set the frame-by-frame xml file to the background ImageView loadingImg = (ImageView) findViewById (R. id. loading); loadingImg. setBackgroundResource (R. drawable. loading); loadingAnimation = (AnimationDrawable) loadingImg. getBackground ();}/*** touch screen, end animation */public boolean onTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {loadingAnimation. stop (); return true;} return super. onTouchEvent (event);}/*** the animation is enabled when the activity is displayed on the screen */@ Overridepublic void onWindowFocusChanged (boolean hasFocus) {// TODO Auto-generated method stubsuper. onWindowFocusChanged (hasFocus); if (hasFocus) loadingAnimation. start ();}}
The image materials are based on the open-source project of OSchina. Download.
Blog source code