直接上demo,图是自己切的,将就用吧。点击左右两边分别向左右移动。

  1. public class MySurfaceView extends SurfaceView implements Callback, Runnable {
  2. private Thread th;
  3. private SurfaceHolder sfh;
  4. private int SH, SW;
  5. private Canvas canvas;
  6. private Paint p;
  7. private Paint p2;
  8. private Resources res;
  9. private Bitmap bmp;
  10. private int bmp_x = 100, bmp_y = 300;
  11. private boolean LEFT, RIGHT;
  12. private int animation_left[] = { 0, 1, 2 };
  13. private int animation_right[] = { 3, 4, 5 };
  14. private int animation_init[] = animation_left;
  15. private int anim_count;
  16. // 一张图上图的数量
  17. private static final int bm_count = 6;
  18. private boolean threadFlag;
  19.  
  20. public MySurfaceView(Context context) {
  21. super(context);
  22. this.setKeepScreenOn(true);
  23. res = this.getResources();
  24. bmp = BitmapFactory.decodeResource(res, R.drawable.move_bitmap);
  25. sfh = this.getHolder();
  26. sfh.addCallback(this);
  27. p = new Paint();
  28. p.setColor(Color.YELLOW);
  29. p2 = new Paint();
  30. p2.setColor(Color.RED);
  31. p.setAntiAlias(true);
  32. setFocusable(true);
  33. }
  34.  
  35. public void surfaceCreated(SurfaceHolder holder) {
  36. SH = this.getHeight();
  37. SW = this.getWidth();
  38. threadFlag = true;
  39. th = new Thread(this);
  40. th.start();
  41. }
  42.  
  43. public void draw() {
  44. canvas = sfh.lockCanvas();
  45. if (canvas != null) {
  46. canvas.drawRect(0, 0, SW, SH, p);
  47. canvas.save();
  48. canvas.drawText("奋斗的小猿", bmp_x + 5, bmp_y - 10, p2);
  49. canvas.clipRect(bmp_x, bmp_y, bmp_x + bmp.getWidth() / bm_count, bmp_y + bmp.getHeight());
  50. if (animation_init == animation_left) {
  51. canvas.drawBitmap(bmp, bmp_x - animation_left[anim_count] * (bmp.getWidth() / bm_count), bmp_y, p);
  52. } else if (animation_init == animation_right) {
  53. canvas.drawBitmap(bmp, bmp_x - animation_right[anim_count] * (bmp.getWidth() / bm_count), bmp_y, p);
  54. }
  55. canvas.restore();
  56. sfh.unlockCanvasAndPost(canvas);
  57. }
  58. }
  59.  
  60. public void cycle() {
  61. if (LEFT) {
  62. bmp_x -= 5;
  63. if (bmp_x <= 0) {
  64. bmp_x = 0;
  65. }
  66. } else if (RIGHT) {
  67. bmp_x += 5;
  68. if ((bmp_x + bmp.getWidth() / bm_count) >= SW) {
  69. bmp_x = SW - bmp.getWidth() / bm_count;
  70. }
  71. }
  72. if (LEFT || RIGHT) {
  73. if (anim_count < 2) {
  74. anim_count++;
  75. } else {
  76. anim_count = 0;
  77. }
  78. }
  79. if (LEFT == false && RIGHT == false) {
  80. anim_count = 0;
  81. }
  82. }
  83.  
  84. @Override
  85. public boolean onTouchEvent(MotionEvent event) {
  86. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  87. if (event.getX() < 100) {
  88. if (LEFT == false) {
  89. animation_init = animation_left;
  90. LEFT = true;
  91. }
  92. } else if (event.getX() > (SW - 100)) {
  93. if (RIGHT == false) {
  94. animation_init = animation_right;
  95. RIGHT = true;
  96. }
  97. }
  98. } else if (event.getAction() == MotionEvent.ACTION_UP) {
  99. if (LEFT) {
  100. LEFT = false;
  101. } else if (RIGHT) {
  102. RIGHT = false;
  103. }
  104. }
  105.  
  106. return true;
  107. }
  108.  
  109. public void run() {
  110. while (threadFlag) {
  111. draw();
  112. cycle();
  113. try {
  114. Thread.sleep(20);
  115. } catch (Exception ex) {
  116. }
  117. }
  118. }
  119.  
  120. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  121. }
  122.  
  123. public void surfaceDestroyed(SurfaceHolder holder) {
  124. threadFlag = false;
  125. }
  126. }

资源图:

效果图:

android SurfaceView绘制 重新学习--控制动画移动的更多相关文章

  1. android SurfaceView绘制 重新学习--基础绘制

    自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎. 直接上代 ...

  2. android SurfaceView绘制 重新学习--切图clipRect详解

    解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...

  3. android SurfaceView绘制实现原理解析

    在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...

  4. Android -- SurfaceView绘制

    SurfaceView SurfaceView是View的一个特殊子类,它的目的是另外提供一个线程进行绘制操作. 步骤 1.用SurfaceView进行绘制,首先要创建一个类,继承 SurfaceVi ...

  5. Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现

    1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...

  6. Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现

    1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...

  7. Android(java)学习笔记263:Android下的属性动画(Property Animation)

    1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...

  8. Android(java)学习笔记207:Android下的属性动画(Property Animation)

    1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...

  9. 深入学习jQuery动画控制

    × 目录 [1]动画状态 [2]停止动画 [3]动画延迟[4]全局控制 前面的话 jQuery动画可以使用fade.hide.slide等方法实现基本动画效果,可以使用animate实现自定义动画,甚 ...

随机推荐

  1. gallery左右滑动时图片淡入淡出

    前几天,公司项目有一个功能要做成滑动图片的淡入淡出,要一边滑动一边改变,所以ViewFlipper左右滑动效果就不能了.网上找了很久,也找不到资料,所以自己写了一个,通过滑动改变imageView的透 ...

  2. 最简便的MySql数据库备份方法

    http://www.kankanews.com/ICkengine/archives/194.shtml 使用MYSQL进行数据库备份,又很正规的数据库备份方法,同其他的数据库服务器有相同的概念,但 ...

  3. ecto注册码

    First name: Good Last name: Life Serial: ecto_at585-RP00-MP3F-VB8R-L82N-N0CC   First Name: The Last ...

  4. locale 详解

    locale 是国际化与本土化过程中的一个非常重要的概念,个人认为,对于中文用户来说,通常会涉及到的国际化或者本土化,大致包含三个方面: 看中文,写中文,与 window中文系统的兼容和通信.从实际经 ...

  5. Android_SeekBarAndProgressBar

    xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  6. LaTeX 标题中使用 \bm 命令与 hyperref 的冲突

    问题 当使用 hyperref 宏包时,在标题中使用 \bm 为数学符号加粗会出现错误 \documentclass{article} \usepackage{bm} \usepackage{hype ...

  7. SQL Server 2008 TOP 新用途

    /***************创建测试表******************************/ IF NOT OBJECT_ID('[Demo_Top]') IS NULL DROP TAB ...

  8. 关于onSaveInstanceState的javadoc的渣渣翻译

    /** * Called to retrieve per-instance state from an activity before being * killed so that the state ...

  9. C++Primer笔记一

    作为一名半路出家的JAVA程序员,又要开始学半路中放弃的C++了,因为真的很重要. 先来看一段代码,  #include <iostream> using namespace std; i ...

  10. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...