"Android" instance in the spirit of the disturbed

Source: Internet
Author: User

Create a project in Android studio with the name "Animation and multimedia" and create a module in the project with the name "Frame-by-frame Animation". Implement this example in this module, with the following steps.

(1) First add an XML resource file named Fairy.xml in the Res\drawable directory, and then define the image resource that makes up the animation in the file, with the following code:

01 <?xml version= "1.0" encoding= "Utf-8"? >02 <animation-list xmlns:android= "http://schemas.android.com/apk/ Res/android ">03 <item android:drawable=" @drawable/img001 "android:duration="/>04 <item android: drawable= "@drawable/img002" android:duration= "/>05 <item android:drawable=" @drawable/img003 "Android: duration= "/>06 <item android:drawable=" @drawable/img004 "android:duration="/>07 <item Androi d:drawable= "@drawable/img005" android:duration= "/>08 <item android:drawable=" @drawable/img006 "Android: duration= "/>09 </animation-list>";  

  (2) Modify the layout file under the Res\layout directory of the new project Activity_ Main.xml, modifies the layout manager that is added by default to the vertical linear layout manager, and removes the TextView component that is added by default in the Layout manager, and then sets the Android:id and Android:background properties for the modified linear layout manager. Set the Android:background property to the animation resource created in step (1), with the following modified code:

   <linearlayout02       android:id= "@+id/linearlayout"       xmlns:android= "http://schemas.android.com/ Apk/res/android "       xmlns:tools=" Http://schemas.android.com/tools "       android:layout_width=" match_parent "       android:layout_height=" match_parent "       android:orientation=" vertical "       android:background=" @ Drawable/fairy "       tools:context=" com.mingrisoft.MainActivity ">10   </LinearLayout>

  (3) Open the default created Mainactivity, in which a private Boolean variable flag=true is defined first, used to determine the start and stop animations, and then in OnCreate () method to get the vertical linear layout manager and the Animationdrawable object, add a click event for the layout manager, and finally start and stop the animation based on the value of the flag variable in the onclick () method, with the following code:

01 Public classMainactivityextendsappcompatactivity {02Private BooleanFlag =true;03@Override04protected voidonCreate (Bundle savedinstancestate) {05Super. OnCreate (savedinstancestate);06Setcontentview (r.layout.activity_main);07//Get layout managerLinearLayout linearlayout=(LinearLayout) Findviewbyid (r.id.linearlayout);09//Get Animationdrawable Object10FinalAnimationdrawable anim=(animationdrawable) linearlayout.getbackground ();11//to add a click event for the layout managerLinearlayout.setonclicklistener (NewView.onclicklistener () {13@Override14 Public voidOnClick (View v) {15if(flag) {Anim.start ();//start playing animationsflag=false;18}Else {Anim.stop ();//stop playing an animationflag=true;21st                   }22               }23           });24       }25}

(4) Add screenorientation attribute in the <activity> tag of androidmanifest.xml file, set its horizontal screen display, the key code is as follows:

android:screenorientation= "Landscape"

(5) Locate the drop-down list box in the toolbar, select the app you want to run (here is Frame-by-frame Animation), and click the button on the right, and run effect 10.1 as shown.

Fig. 10.1 The spirit of the disturbed

Thank readers for the support of tomorrow's science and technology, if you have any questions about the book and the source of the CD-ROM, you can directly login to the community on the http://www.mingrisoft.com to post, your questions in detail, our technical staff will be there to reply to you!

This digest from Tomorrow Science and Technology published the "0 basic Android", Li Lei reproduced please indicate the source!!!

"Android" instance in the spirit of the disturbed

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.