【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11528字,纯文字阅读大概需要17分钟。
内容图文
![【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)](/upload/InfoBanner/zyjiaocheng/1202/d23c2e7711164e5d97a4022fd562c9df.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063636831.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063636970.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063637251.jpg)
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 protectedvoid 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 publicboolean 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); }elseif(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); }elseif(keyCode==KeyEvent.KEYCODE_DPAD_LEFT) { //移动动画效果 Animation m3 = new TranslateAnimation(0, 100, 0, 100); m3.setDuration(2000); this.startAnimation(m3); }elseif(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); } returnsuper.onKeyDown(keyCode, event); } /** * 动画开始时响应的函数 */ @Override publicvoid onAnimationStart(Animation animation) { } /** * 动画结束时响应的函数 */ @Override publicvoid onAnimationEnd(Animation animation) { } /** * 动画重播时响应的函数 */ @Override publicvoid onAnimationRepeat(Animation animation) { } }
记住,Animation 的每种动画都是对整个画布进行操作。
2.自定义动画
(1)动态位图
在之前随笔中,曾在屏幕上让文本字符串跟随玩家手指移动,从而行程一个动态的效果;那么让一张位图形成动态效果也很容易,只要不断改变位图的X或者Y轴的坐标即可。下面利用一张位图形成海的波浪效果。
新建项目,游戏框架为SurfaceView 框架,准备图片water.png如下:
修改MySurfaceView 类,代码如下:
![技术分享](/upload/getfiles/default/2022/11/13/20221113063636970.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063637251.jpg)
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轴坐标privateint bmpX, bmpY; // 声明一个画布private Canvas canvas; // 声明一个线程private Thread th; // 线程消亡的标识符privateboolean flag; public MySurfaceView(Context context) { super(context); // 实例SurfaceView sfh = this.getHolder(); // 为SurfaceView添加状态监听 sfh.addCallback(this); } /** * SurfaceView 视图创建,响应此函数 */ @Override publicvoid 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 publicvoid surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * SurfaceView 视图消亡时,响应此函数 */ @Override publicvoid surfaceDestroyed(SurfaceHolder holder) { flag = false; } privatevoid 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); } } } /** * 游戏逻辑 */privatevoid logic() { bmpX += 5; } @Override publicvoid 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(); } } } }
(2)帧动画
前面是利用改变位图的X或者Y坐标行程动画效果。当然在游戏开发中,很多动态的帧数不仅仅只有一帧,而所谓帧动画,其实就是一帧一帧按照一定的顺序进行播放实现的。下面是实例演示,效果如下:
新建项目,游戏框架为 SurfaceView 框架,在项目中导入下面6张png图片:
修改MySurfaceView 类,代码如下:
![技术分享](/upload/getfiles/default/2022/11/13/20221113063636970.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063637251.jpg)
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; // 线程消亡的标识符privateboolean flag; private Paint paint; // 首先声明6个容量的位图数组private Bitmap wifiBmp[] = new Bitmap[6]; // 对应6张图片的Idprivateint[] wifiBmpId = newint[] { R.drawable.wifi01, R.drawable.wifi02, R.drawable.wifi03, R.drawable.wifi04, R.drawable.wifi05, R.drawable.wifi06 }; // 记录当前播放帧privateint 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 publicvoid surfaceCreated(SurfaceHolder holder) { flag = true; // 实例线程 th = new Thread(this); // 启动线程 th.start(); } /** * SurfaceView 视图状态发生改变时,响应此函数 */ @Override publicvoid surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * SurfaceView 视图消亡时,响应此函数 */ @Override publicvoid surfaceDestroyed(SurfaceHolder holder) { flag = false; } privatevoid 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); } } } /** * 游戏逻辑 */privatevoid logic() { currentFrame++; // 当播放的当前帧大于并且等于帧数组时,重置当前帧为0if (currentFrame >= wifiBmp.length) { currentFrame = 0; } } @Override publicvoid 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(); } } } }
(3)剪切图动画
剪切图动画类似于帧动画的形式,唯一的区别就是动态帧全部放在了一张图片中,然后通过设置可视区域完成。简单的解释,绘图时通过控制X轴或者Y轴坐标,来逐帧显示要显示的图片部分,达到动态的效果。下面是实例,运行效果和前面的帧动画一样。步骤如下:
新建项目,游戏框架为 SurfaceView 框架,在项目中导入下面这张png图片:
修改MySurfaceView 类,代码如下:
![技术分享](/upload/getfiles/default/2022/11/13/20221113063636970.jpg)
![技术分享](/upload/getfiles/default/2022/11/13/20221113063637251.jpg)
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; // 线程消亡的标识符privateboolean flag; private Paint paint; // 创建位图private Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.wifi_all); // 记录当前播放帧privateint currentFrame; public MySurfaceView(Context context) { super(context); // 实例SurfaceView sfh = this.getHolder(); // 为SurfaceView添加状态监听 sfh.addCallback(this); paint = new Paint(); // 画笔无锯齿 paint.setAntiAlias(true); } /** * SurfaceView 视图创建,响应此函数 */ @Override publicvoid surfaceCreated(SurfaceHolder holder) { flag = true; // 实例线程 th = new Thread(this); // 启动线程 th.start(); } /** * SurfaceView 视图状态发生改变时,响应此函数 */ @Override publicvoid surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * SurfaceView 视图消亡时,响应此函数 */ @Override publicvoid surfaceDestroyed(SurfaceHolder holder) { flag = false; } privateint minW = bmp.getWidth() / 6; privatevoid 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); } } } /** * 游戏逻辑 */privatevoid logic() { currentFrame++; // 当播放的当前帧大于并且等于帧数组时,重置当前帧为0if (currentFrame >= 6) { currentFrame = 0; } } @Override publicvoid 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(); } } } }
原文:http://www.cnblogs.com/yc-755909659/p/4171732.html
内容总结
以上是互联网集市为您收集整理的【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)全部内容,希望文章能够帮你解决【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。