Android 2D animation implementation

Source: Internet
Author: User

No matter, go first

Code: Only one activity

MainActivity: the code is very simple.

package com.animation.test;import zhuojin.lession15.animation.frame.R;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class MainActivity extends Activity {    ImageView img1=null;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        img1=(ImageView)findViewById(R.id.img1);    }    AnimationDrawable   anim=new AnimationDrawable();        public  void startBtn(View v)    {      for(int i=0;i<8;i++)      {          anim.addFrame(this.getResources().getDrawable(R.drawable.walk1+i), 100);      }        anim.setOneShot(false);        img1.setBackgroundDrawable(anim);        if(anim.isRunning())        {            anim.stop();        }else        {            anim.stop();            anim.start();        }    }}

Code structure

There is a movie girl. xml file in the drawbale folder. This file actually describes a frame rate for switching images. In fact, the implementation of 2D animation is the display of multiple images.

Registrant girl. xml

<?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/walk1" android:duration="50" />    <item android:drawable="@drawable/walk2" android:duration="50" />    <item android:drawable="@drawable/walk3" android:duration="50" />    <item android:drawable="@drawable/walk4" android:duration="50" />    <item android:drawable="@drawable/walk5" android:duration="50" />    <item android:drawable="@drawable/walk6" android:duration="50" />    <item android:drawable="@drawable/walk7" android:duration="50" />    <item android:drawable="@drawable/walk8" android:duration="50" /></animation-list>

It's speechless. How can I upload code in this blog Park ..

Related Article

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.