Android Animated Frame Animation (frame-wise animation)

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).

Several other animation effects:

    • Android animation effect Tween Animation (motion tween)
    • Android Animated Frame Animation (frame-wise animation)
    • Android animation effect of the first known property Animation (attribute animation)
    • The property of the Android animation effect animation Advanced (attribute animation)
    • Android animation effects customization ViewGroup adding layout animations
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= "200"/><ItemAndroid:drawable= "@mipmap/lottery_2"Android:duration= "200"/><ItemAndroid:drawable= "@mipmap/lottery_3"Android:duration= "200"/><ItemAndroid:drawable= "@mipmap/lottery_4"  Android:duration=" "/> < item android:drawable= "@ Mipmap/lottery_5 " Android:duration=" "/> <item android: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/>            

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:

Imageview.setimageresource (r.drawable.lottery_animlist); Animationdrawable animationdrawable = (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 animationdrawable = (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 ()); drawable drawable = getresources (). getdrawable (ID); Anim.addframe (drawable, $);} 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 Animated Frame Animation (frame-wise animation)

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.