Android成长之路-实现简单动画

实现简单动画:

在drawable目录中放入图片,

Android成长之路-实现简单动画_第1张图片

并且创建xml文件 frame.xml 存入图片,如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
	android:oneshot="false">
	<!-- 可以重复添加,实现循环 -->
	<!-- android:duration="100"设置的是图片持续的时间长短 -->
	<item android:drawable="@drawable/girl_1" android:duration="100" />
	<item android:drawable="@drawable/girl_2" android:duration="100" />
	<item android:drawable="@drawable/girl_3" android:duration="100" />
	<item android:drawable="@drawable/girl_4" android:duration="100" />
	<item android:drawable="@drawable/girl_5" android:duration="100" />
	<item android:drawable="@drawable/girl_6" android:duration="200" />
	<item android:drawable="@drawable/girl_7" android:duration="300" />
	<item android:drawable="@drawable/girl_6" android:duration="200" />
	<item android:drawable="@drawable/girl_7" android:duration="300" />
	<item android:drawable="@drawable/girl_8" android:duration="200" />
	<item android:drawable="@drawable/girl_9" android:duration="100" />
	<item android:drawable="@drawable/girl_10" android:duration="100" />
	<item android:drawable="@drawable/girl_11" android:duration="100" />

</animation-list>




然后定义一个布局frame_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

    <!-- 承载图片 -->
    <ImageView
        android:id="@+id/frameIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>


 

然后写activity类,FrameActivity.java:

package cn.class3g.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class FrameActivity extends Activity {

	AnimationDrawable attackAnimation;//定义动画对象
	ImageView frameIV;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.frame_layout);
		
		init();
	}

	private void init() {
		frameIV = (ImageView) this.findViewById(R.id.frameIV);
		frameIV.setBackgroundResource(R.drawable.frame);//得到图片并添加到布局中(当作背景图片)
		attackAnimation = (AnimationDrawable) frameIV.getBackground();//得到背景图片给动画对象
		
	}
	//点击屏幕触发
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			attackAnimation.start();//点击屏幕后启动动画
		}
		return super.onTouchEvent(event);
	}
	
	

}


 

此时,在清单中注册后就可以运行并实现了

你可能感兴趣的:(android,xml,layout,Class,encoding)