Android Custom View Tutorial-------------Android frame animation

Source: Internet
Author: User

The ultimate goal of this blog series is to teach you to implement more sophisticated Android custom controls. So the knowledge points are not just cramped on the custom view itself. In fact, some of the more brilliant custom controls on GitHub are now made up of three parts.

First: Animation second: Custom view third: Touch slide control. So our series also starts with animation. You'll end up with some of the better custom controls on GitHub.

Android Frame animation is a relatively simple basis for the content, in the past 2.x 3.x version of many people will use this as a loading diagram implementation method. But a lot of people don't use frame when they actually do the loading chart effect recently.

The reason for this is that there is an easier way----play a GIF picture directly better. More convenient. But in the past 10-12 years this time period of Android machine hardware is still relatively normal time to avoid this, because the direct play GIF pictures compared to the CPU and memory consumption.

It's better to use frame animations to do the work. Needless to say, there are two simple ways to implement frame animation.

First create a new folder under Res Anim, and then create a new animation file in this folder (this file is not placed under Drawable, some Android version will be here to make an error must be placed under Anim). For example

<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:oneshot=" true ">    <item        android:drawable=" @drawable/aaa "        android:duration = "/>"    <item        android:drawable= "@drawable/bbb"        android:duration= "    +"/> <item        android:drawable= "@drawable/ccc"        android:duration= "/>    <item        android:drawable=" @ DRAWABLE/DDD "        android:duration=" "/>    <item        android:drawable=" @drawable/eee "        android:duration= "/></animation-list>"

  

This is the animated content we're going to play. Android:oneshot This property refers to whether it is played one time or the end of true is the end of one time false is always playing down android:duration= "1000" means replacing a picture every 1000 milliseconds.

You can then refer to this file in Mainactivity XML to

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<ImageView8Android:id= "@+id/im"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:background= "@anim/dance"/> A  -</LinearLayout>

After you can see the effect, of course, some people want to do a cool point, such as resource files are pulled from the Internet, to make dynamic loading animation, then it is possible, through the code to control can

1  Packagecom.example.frametest;2 3 Importandroid.app.Activity;4 Importandroid.graphics.drawable.AnimationDrawable;5 Importandroid.graphics.drawable.Drawable;6 ImportAndroid.os.Bundle;7 ImportAndroid.widget.ImageView;8 9  Public classMainactivityextendsActivity {Ten  One     PrivateImageView IV; A  -     PrivateAnimationdrawable ANIMDR =Newanimationdrawable (); -  the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +          This. IV = (ImageView) This. Findviewbyid (r.id.im); -         //This is a convenient way to load the Drawable folder directly below the picture if you need to load the SD card directly under the image +Drawable drawable =getresources (). getdrawable (R.DRAWABLE.AAA); AAnimdr.addframe (drawable, 1000); atdrawable Drawable2 =getresources (). getdrawable (R.DRAWABLE.BBB); -Animdr.addframe (Drawable2, 1000); -drawable Drawable3 =getresources (). getdrawable (R.DRAWABLE.CCC); -Animdr.addframe (Drawable3, 1000); -drawable drawable4 =getresources (). getdrawable (R.DRAWABLE.DDD); -Animdr.addframe (DRAWABLE4, 1000); in  -Animdr.setoneshot (false); to iv.setbackgrounddrawable (ANIMDR); + Animdr.start (); -  the     } *}

Android Custom View Tutorial-------------Android frame 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.