欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Animation Frame动画

程序员文章站 2022-03-16 15:12:59
...
1.java代码实现:


package com.Aina.Android;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

/**
* com.Aina.Android Pro_AnimationFrame
*
* @author Aina.huang E-mail: [email protected]
* @version 创建时间:2010 Jun 18, 2010 1:56:18 PM 类说明
*/
public class GameView extends View {

private AnimationDrawable mAnimationDrawable = null;
private Context mContext = null;
private Drawable mDrawable = null;

public GameView(Context context) {
super(context);
this.mContext = context;

mAnimationDrawable = new AnimationDrawable();
for (int i = 1; i <= 15; i++) {
int id = this.getResources().getIdentifier("a" + i, "drawable",
mContext.getPackageName());
mDrawable = this.getResources().getDrawable(id);
mAnimationDrawable.addFrame(mDrawable, 100);// 为动画添加一帧.时间为毫秒
}
mAnimationDrawable.setOneShot(false);// 设置播放模式是否循环,false循环,true不循环.

// ImageView iv = new ImageView(mContext);
// iv.setBackgroundResource(R.anim.frame);
// mAnimationDrawable = (AnimationDrawable) iv.getBackground();
this.setBackgroundDrawable(mAnimationDrawable);// 显示动画.

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_1){
mAnimationDrawable.start();//启动动画.
}
return super.onKeyDown(keyCode, event);
}
}




package com.Aina.Android;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class Test_Frame extends Activity {
/** Called when the activity is first created. */
private GameView gv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gv = new GameView(this);
setContentView(gv);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return gv.onKeyDown(keyCode, event);
}

}


2.xml布局文件实现:


<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a1" android:duration="100" />
<item android:drawable="@drawable/a2" android:duration="100" />
<item android:drawable="@drawable/a3" android:duration="100" />
<item android:drawable="@drawable/a4" android:duration="100" />
<item android:drawable="@drawable/a5" android:duration="100" />
<item android:drawable="@drawable/a6" android:duration="100" />
<item android:drawable="@drawable/a7" android:duration="100" />
<item android:drawable="@drawable/a8" android:duration="100" />
<item android:drawable="@drawable/a9" android:duration="100" />
<item android:drawable="@drawable/a10" android:duration="100" />
<item android:drawable="@drawable/a11" android:duration="100" />
<item android:drawable="@drawable/a12" android:duration="100" />
<item android:drawable="@drawable/a13" android:duration="100" />
<item android:drawable="@drawable/a14" android:duration="100" />
<item android:drawable="@drawable/a15" android:duration="100" />
</animation-list>



package com.Aina.Android;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

/**
* com.Aina.Android Pro_AnimationFrame
*
* @author Aina.huang E-mail: [email protected]
* @version 创建时间:2010 Jun 18, 2010 1:56:18 PM 类说明
*/
public class GameView extends View {

private AnimationDrawable mAnimationDrawable = null;
private Context mContext = null;
private Drawable mDrawable = null;

public GameView(Context context) {
super(context);
this.mContext = context;
/*
mAnimationDrawable = new AnimationDrawable();
for (int i = 1; i <= 15; i++) {
int id = this.getResources().getIdentifier("a" + i, "drawable",
mContext.getPackageName());
mDrawable = this.getResources().getDrawable(id);
mAnimationDrawable.addFrame(mDrawable, 100);// 为动画添加一帧.时间为毫秒
}
mAnimationDrawable.setOneShot(false);// 设置播放模式是否循环,false循环,true不循环.
*/
ImageView iv = new ImageView(mContext);
iv.setBackgroundResource(R.anim.frame);
mAnimationDrawable = (AnimationDrawable) iv.getBackground();
this.setBackgroundDrawable(mAnimationDrawable);// 显示动画.

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_1){
mAnimationDrawable.start();//启动动画.
}
return super.onKeyDown(keyCode, event);
}
}