Android Getting Started note-Interface development-frame animation

Source: Internet
Author: User

Today to introduce the frame animation in Android, frame animation is actually a picture, in a certain order to play, and then form an animation.

First, let's take a picture:

Two buttons in the diagram, start to play the animation, stop the animation, to see the code:

1. Res/drawable/frame_animation.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:oneshot=" false ">    <item android:drawable=" @drawable/ma1 "android:duration=" 100 " ></item>    <item android:drawable= "@drawable/ma2" android:duration= "></item>    < Item android:drawable= "@drawable/ma3" android:duration= "></item>    <item android:drawable=" @ Drawable/ma4 "android:duration=" ></item>    <item android:drawable= "@drawable/ma5" Android: duration= "></item>    <item android:drawable=" @drawable/ma6 "android:duration=" "></" item>    <item android:drawable= "@drawable/ma7" android:duration= "></item> <item"    android:drawable= "@drawable/ma8" android:duration= "></item></animation-list>
This is the XML configuration frame animation, each item is a frame, the oneshot parameter means that you want to play the end of a time, false means to continue to loop


2. Res/layout/activity_main.xml

<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"    tools:context= ". Mainactivity ">    <button         android:id=" @+id/btn_start "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:text= "Start"/>    <button         android:id= "@+id/btn_ Stop "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" Stop "/>        <imageview         android:id=" @+id/ma_iv "        android:layout_width=        " Wrap_content " android:layout_height= "Wrap_content"        android:layout_centerinparent= "true"/>    </LinearLayout>

3. Mainactivity.java:

Package Com.example.demo5_04_frameanimation;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.animationdrawable;import Android.view.ContextMenu; Import Android.view.menu;import Android.view.view;import Android.view.contextmenu.contextmenuinfo;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity {private Animationdrawable ad = Null;imageview IV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); iv = (ImageView) Findviewbyid (R.ID.MA_IV); Iv.setimageresource (r.drawable.frame_animation); ad = (animationdrawable) iv.getdrawable (); Button Btn_start = (button) Findviewbyid (R.id.btn_start); Button Btn_stop = (button) Findviewbyid (r.id.btn_stop); Btn_start.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {Ad.start ();}}); Btn_stop.setonclicklistener (New View.onclicklisteNER () {@Overridepublic void OnClick (View v) {ad.stop ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

The code in Mainactivity is simpler, mainly how to initialize an animation. The ImageView is used here, the resources of IV are animated, and the animation resources are obtained through iv.getdrawable (), and then the ad is used to control the playback of the animation to stop. Very simple, we can see the understanding.






Android Getting Started note-Interface development-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.