Android Development (43) animated demo, will run the villain, run from the left side of the screen to the right

Source: Internet
Author: User

Want to make an animation, a will run the villain, from the right side of the screen to the right, so did a try:

Three steps are required to accomplish this:

1. Do a frame animation (frame animation), composed of a number of pictures, the formation of the villain running continuously.

2. Make a displacement animation so that the villain from left to right to produce movement.

3. Start the animation in OnStart

The following are explained separately:

---The first step------------------

To prepare a picture of multiple actions, write an XML animation:

<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "false" >    <Itemandroid:drawable= "@drawable/loading_iv_00"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_01"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_02"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_03"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_04"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_05"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_06"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_07"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_08"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_09"android:duration= "$">    </Item>    <Itemandroid:drawable= "@drawable/loading_iv_10"android:duration= "$">    </Item></animation-list>

Code:

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         = (ImageView) Findviewbyid (r.id.imageview1);        Imageview1.setimageresource (r.anim.loading);        Imageview1.setvisibility (View.gone);         = (animationdrawable) imageview1.getdrawable ();        Manimationdrawable.setoneshot (false);    }

---The second step-----------------------

Write the code for the Displacement animation:

         New Translateanimation (animation.relative_to_parent, -0.2f,                1, animation.relative_to_self, 0,                0);        Translate.setduration (+);        Translate.setrepeatcount (animation.infinite);

When the meaning of this sentence is moved, the Y axis does not change relative to the parent container's x-coordinate, and always loops

---The third step--------------------------

Start the animation, paste the full code:

 PackageCom.example.demo_run;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewTreeObserver;Importandroid.view.animation.Animation;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {ImageView imageView1;    Animationdrawable manimationdrawable; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ImageView1=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1);        Imageview1.setimageresource (r.anim.loading);        Imageview1.setvisibility (View.gone); Manimationdrawable=(animationdrawable) imageview1.getdrawable (); Manimationdrawable.setoneshot (false); } @Overrideprotected voidOnStart () {startanimation (); Super. OnStart (); }    Private voidstartanimation () {Manimationdrawable.start (); Animation Translate=NewTranslateanimation (Animation.relative_to_parent, -0.2f, Animation.relative_to_parent,1, animation.relative_to_self, 0, Animation.relative_to_self,0); Translate.setduration (3000);        Translate.setrepeatcount (Animation.infinite);        Imageview1.startanimation (translate);    Imageview1.setvisibility (view.visible); }}

Page layout:

<relativelayout 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:background= "#3F99C3"    tools:context= ". Mainactivity ">    <ImageView        android:id=" @+id/imageview1 "        android:layout_ Width= "Wrap_content"        android:layout_height= "Wrap_content"        android:layout_ Alignparentbottom= "true"        android:layout_alignparentleft= "true"/></relativelayout >

---last-------------------

Demo code download.

Android Development (43) animated demo, will run the villain, run from the left side of the screen to the right

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.