ANDROID_MARS学习笔记_S02_010_Animation_动画效果
一、流程
1.把要实现动画的一系列图片复制到res/drawable文件夹
2.在此文件新建一个xml文件用来组织图片
3.在mainactivity中用imageView.setBackgroundResource(R.drawable.anim_item)来使用这个动画
二、代码
1.xml
(1res\drawable-hdpi\anim_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:oneshot="false"> 4 <item android:drawable="@drawable/x1" android:duration="500" /> 5 <item android:drawable="@drawable/x2" android:duration="500" /> 6 <item android:drawable="@drawable/x3" android:duration="500" /> 7 <item android:drawable="@drawable/x4" android:duration="500" /> 8 <item android:drawable="@drawable/x5" android:duration="500" /> 9 </animation-list>
2.java
(1)MainActivity.java
1 package com.animation4_frametoframe; 2 3 import android.app.Activity; 4 import android.graphics.drawable.AnimationDrawable; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.ImageView; 10 11 public class MainActivity extends Activity { 12 13 private Button button = null; 14 private ImageView imageView = null; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 imageView = (ImageView)findViewById(R.id.imageViewId); 20 button = (Button)findViewById(R.id.buttonId); 21 button.setOnClickListener(new ButtonListener()); 22 } 23 24 public class ButtonListener implements OnClickListener { 25 @Override 26 public void onClick(View v) { 27 imageView.setBackgroundResource(R.drawable.anim_item); 28 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); 29 animationDrawable.start(); 30 } 31 } 32 }
三、运行效果
You can do anything you set your mind to, man!