Android Animation Frame动画
程序员文章站
2022-03-16 15:12:59
...
1.java代码实现:
2.xml布局文件实现:
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);
}
}
推荐阅读
-
一看就喜欢的loading动画效果Android分析实现
-
Android实现卡片翻转动画
-
Android属性动画Property Animation系列一之ObjectAnimator_html/css_WEB-ITnose
-
简单谈谈Core Animation 动画效果
-
Android自定义view绘制圆环占比动画
-
Android逐帧动画实现代码
-
Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移、旋转)
-
Android动画之补间动画(Tween Animation)实例详解
-
Android动画之逐帧动画(Frame Animation)实例详解
-
Android自定义渐变式炫酷ListView下拉刷新动画