Android uses animation-list to implement frame-by-frame animation

Source: Internet
Author: User

First look

Below are two animated XML files

Animation1.xml

<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/icon1" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon2" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon3" Android: duration = "250"> </item> <item Android: drawable = "@ drawable/icon4" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon5" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon6" Android: duration = "250"> </item> </animation-List>

Animation2.xml

<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/icon6" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon5" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon4" Android: duration = "250"> </item> <item Android: drawable = "@ drawable/icon3" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon2" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon1" Android: duration = "250"> </item> </animation-List>

XML layout file:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <imageview Android: id = "@ + ID/animationiv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: src = "@ anim/animation1" Android: scaletype = "center"/> <button Android: Id = "@ + ID/buttona" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: text = "sequence Display"/> <button Android: id = "@ + ID/buttonb" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: TEXT = "stop"/> <button Android: Id = "@ + ID/buttonc" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: text = "reverse display"/> </linearlayout>

Java code:

package com.example.animationdemo;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.Window;import android.widget.ImageView;public class MainActivity extends Activity {    private ImageView animationIV;    private AnimationDrawable animationDrawable;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        animationIV = (ImageView) findViewById(R.id.animationIV);    }    public void onClick(View v) {        switch (v.getId()) {        case R.id.buttonA:            start_animation(R.anim.animation1);            break;        case R.id.buttonB:            stop_animation();            break;        case R.id.buttonC:            start_animation(R.anim.animation2);            break;        default:            break;        }    }    private void start_animation(int id){        animationIV.setImageResource(id);        animationDrawable = (AnimationDrawable) animationIV                .getDrawable();        animationDrawable.start();    }    private void stop_animation(){        animationDrawable.stop();    }}

Http://download.csdn.net/detail/wenwei19861106/4856995 (csdn)

 

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.