<span id="Label3"></p><p><p><span style="font-size:14px">Frame animation is a kind of Android animation, similar to the movie we see, animation is composed of N pictures, in the time of the ground to display pictures in turn to achieve animation Phenomenon. There are two ways to implement frame animation, one is to write animation layout in xml, and the other is to implement pure Code.</span></p></p><p><p><span style="font-size:14px">First look at the XML layout method of the frame animation:</span></p></p><p><p><span style="font-size:14px"><?xml version= "1.0" encoding= "utf-8"?><br><animation-list xmlns:android= "http://schemas.android.com/apk/res/android" ><br><item android:drawable= "@drawable/animation_1" android:duration= "/>"<br><item android:drawable= "@drawable/animation_2" android:duration= "/>"<br><item android:drawable= "@drawable/animation_3" android:duration= "/>"<br><item android:drawable= "@drawable/animation_4" android:duration= "/>"<br><item android:drawable= "@drawable/animation_5" android:duration= "/>"<br><item android:drawable= "@drawable/animation_6" android:duration= "/>"<br></animation-list><br></span></p></p><p><p><span style="font-size:14px">Implement the code in three Sentences:</span></p></p><p><p><span style="font-size:14px">Animationiv.setbackgroundresource (r.anim.test_animation); /**animationiv is the imageview that we want to show, already written in the bureau file */<br>Animationdrawable anim = (animationdrawable) animationiv.getbackground ();<br>Anim.start ();</span></p></p><p><p><span style="font-size:14px">Another is a purely code implementation:</span></p></p><p><span style="font-size:14px">public class Frameanimationactivity extends Activity {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private Button startbtn;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private ImageView animationiv;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>protected void OnCreate (Bundle Savedinstancestate) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TODO auto-generated Method Stub<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Super.oncreate (savedinstancestate);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Setcontentview (r.layout.activity_photo);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>STARTBTN = (Button) Findviewbyid (r.id.start_btn);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Animationiv = (ImageView) Findviewbyid (r.id.animation_iv);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Startbtn.setonclicklistener (new Onclicklistener () {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void OnClick (View V) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Startanimation (animationiv);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>});<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** using code to implement frame animation */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>private void Startanimation (ImageView Iv) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/**frame Animated Object */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Animationdrawable anim = new Animationdrawable ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** adding animated elements */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>for (int i = 1; I <= 6; i++) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** gets the corresponding resource based on the resource name and directory id*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>int id = getresources (). getidentifier ("animation_" + i, "drawable", getpackagename ());<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** get to drawable object by ID */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>drawable drawable = getresources (). getdrawable (id);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** added to animationdrawable, 300 refers to the animation time, in milliseconds */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Anim.addframe (drawable, 300);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** sets the looping mode of the animation, true to only once, Fasle for Continuous loop play */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Anim.setoneshot (false); Set to Loop<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** set the background of the ImageView as an animated object */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Iv.setbackgrounddrawable (anim);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** Start Animation */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Anim.start ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/** placed in the Anim or drawable directory under/res (/res/[anim | drawable]/filename.xml), The file name can be referenced in the code as a resource ID<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* We need to put this code in the Onwindowfocuschanged method, when the activity is presented to the user, the Onwindowfocuschanged method is called,<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* We are just at this time to achieve our animated Effect. Onwindowfocuschanged is called after Oncreate. */<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br>public void onwindowfocuschanged (boolean Hasfocus) {//here is an animation written in xml, with an animation of the Click event above, you can just keep any one<br>Super.onwindowfocuschanged (hasfocus);<br>Animationiv.setbackgroundresource (r.anim.test_animation);<br>Animationdrawable anim = (animationdrawable) animationiv.getbackground ();<br>Anim.start ();<br>}<br>}</span><br></p><p><p>Android Animation: Frame animation</p></p></span>
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