android SurfaceView绘制 重新学习--控制动画移动
直接上demo,图是自己切的,将就用吧。点击左右两边分别向左右移动。
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
private Thread th;
private SurfaceHolder sfh;
private int SH, SW;
private Canvas canvas;
private Paint p;
private Paint p2;
private Resources res;
private Bitmap bmp;
private int bmp_x = 100, bmp_y = 300;
private boolean LEFT, RIGHT;
private int animation_left[] = { 0, 1, 2 };
private int animation_right[] = { 3, 4, 5 };
private int animation_init[] = animation_left;
private int anim_count;
// 一张图上图的数量
private static final int bm_count = 6;
private boolean threadFlag;
public MySurfaceView(Context context) {
super(context);
this.setKeepScreenOn(true);
res = this.getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.move_bitmap);
sfh = this.getHolder();
sfh.addCallback(this);
p = new Paint();
p.setColor(Color.YELLOW);
p2 = new Paint();
p2.setColor(Color.RED);
p.setAntiAlias(true);
setFocusable(true);
}
public void surfaceCreated(SurfaceHolder holder) {
SH = this.getHeight();
SW = this.getWidth();
threadFlag = true;
th = new Thread(this);
th.start();
}
public void draw() {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawRect(0, 0, SW, SH, p);
canvas.save();
canvas.drawText("奋斗的小猿", bmp_x + 5, bmp_y - 10, p2);
canvas.clipRect(bmp_x, bmp_y, bmp_x + bmp.getWidth() / bm_count, bmp_y + bmp.getHeight());
if (animation_init == animation_left) {
canvas.drawBitmap(bmp, bmp_x - animation_left[anim_count] * (bmp.getWidth() / bm_count), bmp_y, p);
} else if (animation_init == animation_right) {
canvas.drawBitmap(bmp, bmp_x - animation_right[anim_count] * (bmp.getWidth() / bm_count), bmp_y, p);
}
canvas.restore();
sfh.unlockCanvasAndPost(canvas);
}
}
public void cycle() {
if (LEFT) {
bmp_x -= 5;
if (bmp_x <= 0) {
bmp_x = 0;
}
} else if (RIGHT) {
bmp_x += 5;
if ((bmp_x + bmp.getWidth() / bm_count) >= SW) {
bmp_x = SW - bmp.getWidth() / bm_count;
}
}
if (LEFT || RIGHT) {
if (anim_count < 2) {
anim_count++;
} else {
anim_count = 0;
}
}
if (LEFT == false && RIGHT == false) {
anim_count = 0;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getX() < 100) {
if (LEFT == false) {
animation_init = animation_left;
LEFT = true;
}
} else if (event.getX() > (SW - 100)) {
if (RIGHT == false) {
animation_init = animation_right;
RIGHT = true;
}
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (LEFT) {
LEFT = false;
} else if (RIGHT) {
RIGHT = false;
}
}
return true;
}
public void run() {
while (threadFlag) {
draw();
cycle();
try {
Thread.sleep(20);
} catch (Exception ex) {
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
threadFlag = false;
}
}
资源图:

效果图:

android SurfaceView绘制 重新学习--控制动画移动的更多相关文章
- android SurfaceView绘制 重新学习--基础绘制
自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎. 直接上代 ...
- android SurfaceView绘制 重新学习--切图clipRect详解
解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...
- android SurfaceView绘制实现原理解析
在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...
- Android -- SurfaceView绘制
SurfaceView SurfaceView是View的一个特殊子类,它的目的是另外提供一个线程进行绘制操作. 步骤 1.用SurfaceView进行绘制,首先要创建一个类,继承 SurfaceVi ...
- Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- Android(java)学习笔记263:Android下的属性动画(Property Animation)
1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...
- Android(java)学习笔记207:Android下的属性动画(Property Animation)
1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...
- 深入学习jQuery动画控制
× 目录 [1]动画状态 [2]停止动画 [3]动画延迟[4]全局控制 前面的话 jQuery动画可以使用fade.hide.slide等方法实现基本动画效果,可以使用animate实现自定义动画,甚 ...
随机推荐
- shell 两个rpm包做差分
当前目录下两个rpm包做差分. *.rpm 两个目录,解压rpm包. export LANG=c 差分判断是脚本还是Binary Files 脚本添加命令行提示符,awk. [root@sj_x8 ...
- Java基础知识强化之IO流笔记40:字符流缓冲流之特殊功能 [ newLine() / readLine() ]
1. 字符缓冲流的特殊方法 BufferedWriter: public void newLine():根据系统来决定换行符 BufferedReader: public String readLin ...
- iOS类初始化
类继承下来的初始化有三种: +(void)load: +(void)initialize: -(instancetype)init: +(void)load:会自动调用(也可手动调用),只要有引用 ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- ThinkPHP函数详解:cache方法
cache方法是3.0版本开始新增的缓存管理方法.注意:3.1.2版本后因cache方法并入原S方法,所以cache方法不再建议使用,用S方法即可. cache 用于缓存设置.获取.删除操作 用法ca ...
- 通过js实时检测文本框内容
思路 1,在获取文本框焦点后,启动定时器,每隔100毫秒来查看文本内容的改变 2,在文本框失去焦点后,清除定时器 下面是一个简单的例子 <!DOCTYPE html> <html&g ...
- JQ实现复选框的全选反选不选
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- zzzzw_在线考试系统①准备篇
在弄完购物系统之后,小博也了解了解怎么用struts这个框架捣鼓一个在线考试系统 购物系统用的是MVC模式,现在这个struts2原理上也是基于MVC模式的.那么要做这个东西之前先了解一下难点在哪里 ...
- updatepannel的使用
注意:放在updatepannel中的数据,单击事件之后,只重新加载后台数据,不加载前台数据,所以如果页面上有js的插件,对js插件的引用和赋值不要放在updatepannel中,同时尽量减小upda ...
- action方法不返回
当被请求的action方法中还有资源没有释放时,请求方法是不会返回的,会一直停留在方法中,即使是最后一行,因为请求方法一旦返回,那方法中的资源,引用就没有位置住了,所以所请求的方法会一直不返回,直到方 ...