package cn.m15.xys;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
/**
* xuanyusong@gmail.com
* @author 宣雨松
*
*/
public class AnimationAcitvity extends Activity {
AnimView mAnimView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 获取屏幕宽高
Display display = getWindowManager().getDefaultDisplay();
// 显示自定义的游戏View
mAnimView = new AnimView(this,display.getWidth(), display.getHeight());
setContentView(mAnimView);
}
public class AnimView extends View {
/**向下移动动画**/
public final static int ANIM_DOWN = 0;
/**向左移动动画**/
public final static int ANIM_LEFT = 1;
/**向右移动动画**/
public final static int ANIM_RIGHT = 2;
/**向上移动动画**/
public final static int ANIM_UP = 3;
/**动画的总数量**/
public final static int ANIM_COUNT = 4;
Animation mHeroAnim [] = new Animation[ANIM_COUNT];
Animation mTestAnim [] = new Animation[ANIM_COUNT];
Paint mPaint = null;
/**任意键被按下**/
private boolean mAllkeyDown = false;
/**按键下**/
private boolean mIskeyDown = false;
/**按键左**/
private boolean mIskeyLeft = false;
/**按键右**/
private boolean mIskeyRight = false;
/**按键上**/
private boolean mIskeyUp = false;
//当前绘制动画状态ID
int mAnimationState = 0;
//上一篇已经介绍了地图块的绘制 这里我暂时先写成一张PNG
Bitmap mMapImage = null;
/**
* 构造方法
*
* @param context
*/
public AnimView(Context context,int screenWidth, int screenHeight) {
super(context);
mPaint = new Paint();
//这里可以用循环来处理总之我们需要把动画的ID传进去
mHeroAnim[ANIM_DOWN] = new Animation(context,new int []{R.drawable.hero_down_a,R.drawable.hero_down_b,R.drawable.hero_down_c,R.drawable.hero_down_d},true);
mHeroAnim[ANIM_LEFT] = new Animation(context,new int []{R.drawable.hero_left_a,R.drawable.hero_left_b,R.drawable.hero_left_c,R.drawable.hero_left_d},true);
mHeroAnim[ANIM_RIGHT]= new Animation(context,new int []{R.drawable.hero_right_a,R.drawable.hero_right_b,R.drawable.hero_right_c,R.drawable.hero_right_d},true);
mHeroAnim[ANIM_UP] = new Animation(context,new int []{R.drawable.hero_up_a,R.drawable.hero_up_b,R.drawable.hero_up_c,R.drawable.hero_up_d},true);
//利用程序来切割图片
Bitmap testmap = ReadBitMap(context,R.drawable.enemy);
Bitmap [][]bitmap = new Bitmap[ANIM_COUNT][ANIM_COUNT];
int tileWidth = testmap.getWidth() / ANIM_COUNT;
int tileHeight = testmap.getHeight() / ANIM_COUNT;
int i = 0,x = 0,y = 0;
for(i =0; i < ANIM_COUNT; i++) {
y = 0;
bitmap[ANIM_DOWN][i] = BitmapClipBitmap(testmap,x,y,tileWidth,tileHeight);
y+=tileHeight;
bitmap[ANIM_LEFT][i] = BitmapClipBitmap(testmap,x,y,tileWidth,tileHeight);
y+=tileHeight;
bitmap[ANIM_RIGHT][i] = BitmapClipBitmap(testmap,x,y,tileWidth,tileHeight);
y+=tileHeight;
bitmap[ANIM_UP][i] = BitmapClipBitmap(testmap,x,y,tileWidth,tileHeight);
x+= tileWidth;
}
mTestAnim[ANIM_DOWN] = new Animation(context,bitmap[ANIM_DOWN],true);
mTestAnim[ANIM_LEFT] = new Animation(context,bitmap[ANIM_LEFT],true);
mTestAnim[ANIM_RIGHT]= new Animation(context,bitmap[ANIM_RIGHT],true);
mTestAnim[ANIM_UP] = new Animation(context,bitmap[ANIM_UP],true);
mMapImage = ReadBitMap(context,R.drawable.map);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mMapImage, 0,0, mPaint);
canvas.save();
canvas.clipRect(0, 0,320, 30);
mPaint.setColor(Color.WHITE);
canvas.drawRect(0, 0,480, 30, mPaint);
mPaint.setColor(Color.RED);
canvas.restore();
/**根据按键更新显示动画**/
if (mAllkeyDown) {
if (mIskeyDown) {
mAnimationState = ANIM_DOWN;
canvas.drawText("按下下键,开始播放向下动画开始", 0, 20, mPaint);
} else if (mIskeyLeft) {
mAnimationState = ANIM_LEFT;
canvas.drawText("按下左键,开始播放向左动画开始", 0, 20, mPaint);
} else if (mIskeyRight) {
mAnimationState = ANIM_RIGHT;
canvas.drawText("按下右键,开始播放向右动画开始", 0, 20, mPaint);
} else if (mIskeyUp) {
mAnimationState = ANIM_UP;
canvas.drawText("按下上键,开始播放向上动画开始", 0, 20, mPaint);
}
/**绘制主角动画**/
mHeroAnim[mAnimationState].DrawAnimation(canvas, mPaint, 20, 100);
mTestAnim[mAnimationState].DrawAnimation(canvas, mPaint, 100, 100);
}else {
/**按键抬起后人物停止动画**/
mHeroAnim[mAnimationState].DrawFrame(canvas, mPaint, 20, 100, 0);
mTestAnim[mAnimationState].DrawFrame(canvas, mPaint, 100, 100, 0);
canvas.drawText("按键已经抬起动画停止", 0, 20, mPaint);
}
super.onDraw(canvas);
invalidate();
}
/**
* 设置按键状态true为按下 false为抬起
* @param keyCode
* @param state
*/
public void setKeyState(int keyCode, boolean state) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
mIskeyDown = state;
break;
case KeyEvent.KEYCODE_DPAD_UP:
mIskeyUp = state;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
mIskeyLeft = state;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
mIskeyRight = state;
break;
}
mAllkeyDown = state;
}
/**
* 程序切割图片
* @param bitmap
* @param x
* @param y
* @param w
* @param h
* @return
*/
public Bitmap BitmapClipBitmap(Bitmap bitmap,int x, int y, int w, int h) {
return Bitmap.createBitmap(bitmap, x, y, w, h);
}
/**
* 读取本地资源的图片
*
* @param context
* @param resId
* @return
*/
public Bitmap ReadBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
mAnimView.setKeyState(keyCode,true);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
mAnimView.setKeyState(keyCode,false);
return super.onKeyUp(keyCode, event);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
第三讲之帧动画.rar (47个子文件)
Animation
.project 845B
proguard.cfg 1KB
src
cn
m15
xys
startActivity.java 819B
Animation.java 3KB
AnimationAcitvity.java 7KB
AndroidManifest.xml 707B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
jay.png 24KB
drawable-mdpi
hero_right_c.png 4KB
hero_left_a.png 4KB
icon.png 3KB
hero_left_d.png 4KB
hero_right_d.png 4KB
hero_up_d.png 4KB
hero_right_a.png 4KB
enemy.png 9KB
hero_left_c.png 4KB
hero_up_c.png 4KB
hero_left_b.png 4KB
hero_down_d.png 4KB
map.png 63KB
hero_down_a.png 4KB
hero_down_c.png 4KB
hero_right_b.png 4KB
hero_up_b.png 4KB
hero_up_a.png 4KB
hero_down_b.png 4KB
layout
main.xml 1KB
values
strings.xml 193B
assets
gen
cn
m15
xys
R.java 2KB
default.properties 362B
.classpath 280B
bin
resources.ap_ 122KB
cn
m15
xys
R$layout.class 370B
R$drawable.class 1015B
R$attr.class 316B
AnimationAcitvity$AnimView.class 5KB
AnimationAcitvity.class 2KB
R$id.class 457B
startActivity.class 1006B
R.class 475B
R$string.class 403B
Animation.class 3KB
startActivity$1.class 1KB
Animation.apk 130KB
classes.dex 10KB
共 47 条
- 1
资源评论
- 梓琪天使2013-12-03很好用,谢谢
- 缓解2014-04-02不怎么好用啊
- u0105392742014-04-03有帮助,可以看看!
- Yilia_伊丽娅2013-04-10资源好,对学习android动画很有帮助!
niyijianwojiuxiao
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功