【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)

package com.example.ex4_10; 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.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation; public class MyView extends View implements AnimationListener{
private Paint paint;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
setFocusable(true);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.pic01);
//黑色背景
canvas.drawColor(Color.BLACK);
canvas.drawText("方向键 ↑ 渐变透明度动画效果", 80, this.getHeight()-80, paint);
canvas.drawText("方向键 ↓ 渐变尺寸伸缩动画效果", 80, this.getHeight()-60, paint);
canvas.drawText("方向键 → 画面转换位置移动动画效果", 80, this.getHeight()-40, paint);
canvas.drawText("方向键 ← 画面转移旋转动画效果", 80, this.getHeight()-20, paint);
//绘制位图,居中
canvas.drawBitmap(bmp, this.getWidth()/2-bmp.getWidth()/2, this.getHeight()/2-bmp.getHeight()/2, paint);
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
{
//渐变透明效果
Animation m1 = new AlphaAnimation(0.1f, 1.0f);
//设置动画播放时间为3秒
m1.setDuration(3000);
//启动动画效果
this.startAnimation(m1);
}else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN)
{
//渐变尺寸缩放动画效果
Animation m2 = new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.0f);
m2.setDuration(2000);
this.startAnimation(m2);
}else if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
{
//移动动画效果
Animation m3 = new TranslateAnimation(0, 100, 0, 100);
m3.setDuration(2000);
this.startAnimation(m3);
}else if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT)
{
//旋转动画效果,这里是旋转360°
Animation m4 = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
m4.setDuration(2000);
this.startAnimation(m4);
}
return super.onKeyDown(keyCode, event);
} /**
* 动画开始时响应的函数
*/
@Override
public void onAnimationStart(Animation animation) { } /**
* 动画结束时响应的函数
*/
@Override
public void onAnimationEnd(Animation animation) { } /**
* 动画重播时响应的函数
*/
@Override
public void onAnimationRepeat(Animation animation) { } }
MyView
记住,Animation 的每种动画都是对整个画布进行操作。
2.自定义动画
(1)动态位图
在之前随笔中,曾在屏幕上让文本字符串跟随玩家手指移动,从而行程一个动态的效果;那么让一张位图形成动态效果也很容易,只要不断改变位图的X或者Y轴的坐标即可。下面利用一张位图形成海的波浪效果。
新建项目,游戏框架为SurfaceView 框架,准备图片water.png如下:

修改MySurfaceView 类,代码如下:
package com.example.ex4_11; 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.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Callback, Runnable { // 用于控制SurfaceView 的大小、格式等,并且主要用于监听SurfaceView 的状态
private SurfaceHolder sfh;
// 声明一张波浪图
private Bitmap bmp;
// 声明波浪图的X,Y轴坐标
private int bmpX, bmpY;
// 声明一个画布
private Canvas canvas;
// 声明一个线程
private Thread th;
// 线程消亡的标识符
private boolean flag; public MySurfaceView(Context context) {
super(context);
// 实例SurfaceView
sfh = this.getHolder();
// 为SurfaceView添加状态监听
sfh.addCallback(this);
} /**
* SurfaceView 视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
bmp = BitmapFactory.decodeResource(this.getResources(),
R.drawable.water);
// 让位图初始化X轴正好充满屏幕
bmpX = -bmp.getWidth() + this.getWidth();
// 让位图绘制在画布的最下方,且图片Y坐标正好是(屏幕高-图片高)
bmpY = this.getHeight() - bmp.getHeight();
flag = true;
// 实例线程
th = new Thread(this);
// 启动线程
th.start();
} /**
* SurfaceView 视图状态发生改变时,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { } /**
* SurfaceView 视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
} private void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
//刷屏,画布白色
canvas.drawColor(Color.WHITE);
//绘制位图
canvas.drawBitmap(bmp, bmpX, bmpY, new Paint() );
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null) {
sfh.unlockCanvasAndPost(canvas);
}
}
} /**
* 游戏逻辑
*/
private void logic() {
bmpX += 5;
} @Override
public void run() {
while (flag) {
long start = System.currentTimeMillis();
myDraw();
logic();
long end = System.currentTimeMillis();
try {
if (end - start < 50) {
Thread.sleep(50 - (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
} } }
MySurfaceView
(2)帧动画
前面是利用改变位图的X或者Y坐标行程动画效果。当然在游戏开发中,很多动态的帧数不仅仅只有一帧,而所谓帧动画,其实就是一帧一帧按照一定的顺序进行播放实现的。下面是实例演示,效果如下:

新建项目,游戏框架为 SurfaceView 框架,在项目中导入下面6张png图片:






修改MySurfaceView 类,代码如下:
package com.example.ex4_11; 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.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Callback, Runnable { // 用于控制SurfaceView 的大小、格式等,并且主要用于监听SurfaceView 的状态
private SurfaceHolder sfh;
// 声明一个画布
private Canvas canvas;
// 声明一个线程
private Thread th;
// 线程消亡的标识符
private boolean flag;
private Paint paint;
// 首先声明6个容量的位图数组
private Bitmap wifiBmp[] = new Bitmap[6];
// 对应6张图片的Id
private int[] wifiBmpId = new int[] { R.drawable.wifi01, R.drawable.wifi02,
R.drawable.wifi03, R.drawable.wifi04, R.drawable.wifi05,
R.drawable.wifi06 };
// 记录当前播放帧
private int currentFrame; public MySurfaceView(Context context) {
super(context);
// 实例SurfaceView
sfh = this.getHolder();
// 为SurfaceView添加状态监听
sfh.addCallback(this);
paint = new Paint();
// 画笔无锯齿
paint.setAntiAlias(true);
// 将帧图放入帧数组
for (int i = 0; i < wifiBmp.length; i++) {
wifiBmp[i] = BitmapFactory.decodeResource(this.getResources(),
wifiBmpId[i]);
}
} /**
* SurfaceView 视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
// 实例线程
th = new Thread(this);
// 启动线程
th.start();
} /**
* SurfaceView 视图状态发生改变时,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { } /**
* SurfaceView 视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
} private void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
// 刷屏,画布白色
canvas.drawColor(Color.WHITE);
// 绘制位图,居中显示
canvas.drawBitmap(
wifiBmp[currentFrame],
this.getWidth() / 2 - wifiBmp[currentFrame].getWidth()
/ 2,
this.getHeight() / 2
- wifiBmp[currentFrame].getHeight() / 2, paint);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null) {
sfh.unlockCanvasAndPost(canvas);
}
}
} /**
* 游戏逻辑
*/
private void logic() {
currentFrame++;
// 当播放的当前帧大于并且等于帧数组时,重置当前帧为0
if (currentFrame >= wifiBmp.length) {
currentFrame = 0;
}
} @Override
public void run() {
while (flag) {
long start = System.currentTimeMillis();
myDraw();
logic();
long end = System.currentTimeMillis();
try {
if (end - start < 500) {
Thread.sleep(500 - (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
} } }
MySurfaceView
(3)剪切图动画
剪切图动画类似于帧动画的形式,唯一的区别就是动态帧全部放在了一张图片中,然后通过设置可视区域完成。简单的解释,绘图时通过控制X轴或者Y轴坐标,来逐帧显示要显示的图片部分,达到动态的效果。下面是实例,运行效果和前面的帧动画一样。步骤如下:
新建项目,游戏框架为 SurfaceView 框架,在项目中导入下面这张png图片:

修改MySurfaceView 类,代码如下:
package com.example.ex4_11; 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.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Callback, Runnable { // 用于控制SurfaceView 的大小、格式等,并且主要用于监听SurfaceView 的状态
private SurfaceHolder sfh;
// 声明一个画布
private Canvas canvas;
// 声明一个线程
private Thread th;
// 线程消亡的标识符
private boolean flag;
private Paint paint;
// 创建位图
private Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),
R.drawable.wifi_all);
// 记录当前播放帧
private int currentFrame; public MySurfaceView(Context context) {
super(context);
// 实例SurfaceView
sfh = this.getHolder();
// 为SurfaceView添加状态监听
sfh.addCallback(this);
paint = new Paint();
// 画笔无锯齿
paint.setAntiAlias(true); } /**
* SurfaceView 视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
// 实例线程
th = new Thread(this);
// 启动线程
th.start();
} /**
* SurfaceView 视图状态发生改变时,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { } /**
* SurfaceView 视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
} private int minW = bmp.getWidth() / 6; private void myDraw() {
try {
//居中点X轴坐标
int cW = this.getWidth() / 2 - minW / 2;
//居中点Y轴坐标
int cH = this.getHeight() / 2 - bmp.getHeight() / 2;
canvas = sfh.lockCanvas();
if (canvas != null) {
// 刷屏,画布白色
canvas.drawColor(Color.WHITE);
canvas.save();
// 设置画布的可视区域,大小为每帧的大小,居中
canvas.clipRect(cW, cH, cW + minW, cH + bmp.getHeight());
// 绘制位图,居中
canvas.drawBitmap(bmp, cW - currentFrame * minW, cH, paint);
canvas.restore();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null) {
sfh.unlockCanvasAndPost(canvas);
}
}
} /**
* 游戏逻辑
*/
private void logic() {
currentFrame++;
// 当播放的当前帧大于并且等于帧数组时,重置当前帧为0
if (currentFrame >= 6) {
currentFrame = 0;
}
} @Override
public void run() {
while (flag) {
long start = System.currentTimeMillis();
myDraw();
logic();
long end = System.currentTimeMillis();
try {
if (end - start < 500) {
Thread.sleep(500 - (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
} } }
MySurfaceView
【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)的更多相关文章
- Windows游戏编程之从零开始d
Windows游戏编程之从零开始d I'm back~~恩,几个月不见,大家还好吗? 这段时间真的好多童鞋在博客里留言说或者发邮件说浅墨你回来继续更新博客吧. woxiangnifrr童鞋说每天都在来 ...
- Java并发编程的艺术读书笔记(2)-并发编程模型
title: Java并发编程的艺术读书笔记(2)-并发编程模型 date: 2017-05-05 23:37:20 tags: ['多线程','并发'] categories: 读书笔记 --- 1 ...
- Java并发编程的艺术读书笔记(1)-并发编程的挑战
title: Java并发编程的艺术读书笔记(1)-并发编程的挑战 date: 2017-05-03 23:28:45 tags: ['多线程','并发'] categories: 读书笔记 --- ...
- 读书笔记--Android Gradle权威指南(下)
前言 最近看了一本书<Android Gradle 权威指南>,收获挺多,就想着来记录一些读书笔记,方便后续查阅. 本篇内容是基于上一篇:读书笔记--Android Gradle权威指南( ...
- 《Essential C++》读书笔记 之 C++编程基础
<Essential C++>读书笔记 之 C++编程基础 2014-07-03 1.1 如何撰写C++程序 头文件 命名空间 1.2 对象的定义与初始化 1.3 撰写表达式 运算符的优先 ...
- 【读书笔记《Android游戏编程之从零开始》】18.游戏开发基础(碰撞检测)
1.矩形碰撞 所谓矩形碰撞,就是利用两个矩形之间的位置关系来进行判断,如果矩形的像素在另外一个矩形之中,或者之上都可以认为这两个矩形发生了碰撞. 如果单纯的去考虑哪些情况会判定两个矩形发生碰撞,倒不如 ...
- 【读书笔记《Android游戏编程之从零开始》】19.游戏开发基础(游戏音乐与音效)
在一款游戏中,除了华丽的界面 UI 直接吸引玩家外,另外重要的就是游戏的背景音乐与音效:合适的背景音乐以及精彩的音效搭配会令整个游戏上升一个档次. 在 Android 中.常用于播放游戏背景音乐的类是 ...
- 【读书笔记《Android游戏编程之从零开始》】14.游戏开发基础(Bitmap 位图的渲染与操作)
Bitmap 是图形类,Android 系统支持的图片格式有 png.jpg.bmp 等. 对位图操作在游戏中是很重要的知识点,比如游戏中需要两张除了大小之外其他完全相同的图,那么如果会对位图进行缩放 ...
- 【读书笔记《Android游戏编程之从零开始》】12.游戏开发基础(Canvas 画布)
1.Canvas 画布 画布类 Canvas 封装了图形和图片绘制等内容,此类常用的函数说明如下: drawColor(int color) 作用:绘制颜色覆盖画布,常用于刷屏 参数:颜色值,也可用十 ...
随机推荐
- JavaScript跨域总结与解决办法
什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...
- 论httpclient上传带参数【commons-httpclient和apache httpclient区别】
需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...
- struts2进阶篇(4)
一.使用ActionContext访问Servlet API strtus2提供了一个ActionContext类,该类别称为Action上下文或者Action环境,Action可以通过该类来访问最常 ...
- 蒙特卡洛树搜索算法(UCT): 一个程序猿进化的故事
前言: 本文是根据的文章Introduction to Monte Carlo Tree Search by Jeff Bradberry所写. Jeff Bradberry还提供了一整套的例子,用p ...
- JAVA 缓存Ehcache详细解毒
链接地址:http://raychase.iteye.com/blog/1545906 作者:RayChase 写的真是太好了,郑重推荐.
- python基础(2)
1.lambda函数 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,函数同样有简单的表示方法 # ###################### 普通函数 ######### ...
- Matlab2014a 提示未找到支持的编译器或 SDK的解决方法
最近在写论文,用到了matlab版本的libsvm,在混合编译的时候遇到的一点小问题. 我电脑上装的是matlab2014a,window7 64位 >> mbuild -setup 错误 ...
- Android Activity/Service/Broadcaster三大组件之间互相调用
我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...
- Atitit.atiInputMethod v2词库清理策略工具 q229
Atitit.atiInputMethod v2词库清理策略工具 q229 1.1. Foreigncode 外码清理1 1.2. 垃圾词澄清1 1.1. Foreigncode 外码清理 On ...
- SCOM Visio监控 与sharepoint 2010 整合
激活sharepoint 2010的企业版网站功能 在sharepoint 前端服务器安装:OpsMgrDataModule.msi,安装好后可以看到如下东东: 在Sharepoint前端服务器中启动 ...