Android animation effect Frame Animation (frame-wise Animation) (ii) (

Source: Internet
Author: User

Objective:

The previous article introduced the Android tween Animation (tweened animation) Android animation effect tween Animation (tweened animation), today to summarize under Android's other animation frame Animation (frame-wise animation).

Frame Animation (Progressive animation):

Frames-per-frame animation (frame-by-frame animations) is literally a frame-by-frame playback of a picture, just like a movie. As with motion tweens, it can be implemented either through XML or through Java code. The next step is to summarize how to use one of the draw animations in the current project. The implementation results are as follows:

Implementation process: 1.) in the Res/drawable directory the next file Lottery_animlist.xml, which reads as follows:
<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "false">    <Itemandroid:drawable= "@mipmap/lottery_1"android:duration= "$" />    <Itemandroid:drawable= "@mipmap/lottery_2"android:duration= "$" />    <Itemandroid:drawable= "@mipmap/lottery_3"android:duration= "$" />    <Itemandroid:drawable= "@mipmap/lottery_4"android:duration= "$" />    <Itemandroid:drawable= "@mipmap/lottery_5"android:duration= "$" />    <Itemandroid:drawable= "@mipmap/lottery_6"android:duration= "$" /></animation-list>

The root node is a animation-list (animated list) with one or more item nodes, and the OneShot property indicates whether to play only once, true to play only once, false to loop all the time, an animation frame is declared internally with the item node, android:drawable Specifies the picture resource that corresponds to this frame animation, and android:druation represents the duration, integer, and millisecond of this frame.

Attention:

Before using Eclipse or Android ADT Development, files can be placed under the Res/anim and res/drawable two folders, although Google recommended in the Res/drawable folder but will not error, in the use of Android Studio was not so lucky, if not placed under the Res/drawable folder, the following error will be reported:

2.) Use ImageView controls as animation vectors to display animations
< ImageView    Android:id = "@+id/animation_iv"    android:layout_width= "Wrap_content"   android:layout_height= "Wrap_ Content "   android:layout_gravity=" center "   Android:layout_margin  = "10DP"   android:src= "@drawable/lottery_animlist"/>

This time we run, we find that the animation is not running but stuck in the first frame, because the Animationdrawable play animation is attached to the window, and in the activity OnCreate method called when the window is not initialized, All will stay in the first frame, in order to achieve playback you must add the following code in the onwindowfocuschanged:

= (animationdrawable) imageview.getdrawable (); Animationdrawable.start ();

If you want to stop playing the animation, you can call the Animationdrawable stop method

  Imageview.setimageresource (r.drawable.lottery_animlist);   = (animationdrawable) imageview.getdrawable ();  Animationdrawable.stop ();
3.) Pure Java Code Implementation method
New animationdrawable ();      for (int i = 1; I <= 6; i++) {    int id = getresources (). Getidentifier ("Lottery_" + I, "mipmap ", Getpackagename ());     = getresources (). getdrawable (ID);     );    }    Anim.setoneshot (false);    Imageview.setimagedrawable (anim);    Anim.start ();
4.) animationdrawable a few common APIs
    • void start()-Start playing animations

    • void stop()-Stop playing animations

    • addFrame(Drawable frame, int duration)-Add a frame and set the duration of the frame display

    • void setOneShoe(boolean flag)-False for loop playback, true to play only once

    • boolean isRunning()-whether it is playing

Summarize:

Frame Animation (Step-by-step animation) is relatively simple, but the frequency used in the actual development is still relatively high. I hope that with this small example can grasp the frame by animation, but the frame-wise animation can only achieve a relatively small animation effect, if the complex and more than the frame number of animation is not recommended to use frame-wise animation, on the one hand because it will cause oom, on the other hand will appear very card, If it is really super complex animation, it is recommended to choose double buffering to draw the view to achieve.

Android animation effect Frame Animation (frame-wise Animation) (ii) (

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.