当前位置:   article > 正文

SurfaceView绘图机制_surfaceview ondraw绘制背景不消失

surfaceview ondraw绘制背景不消失

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

虽然前面都使用的View来进行绘图,但View绘图机制存在两个缺陷:

1、缺乏双缓冲机制

2、更新图像时,必须更新View上的整张图片,效率低。

所以在游戏绘制中推荐使用SurfaceView。它可以只对指定区域进行更新,可以提高效率。下面用一个简单示例来演示SurfaceView的使用:

Activity:

  1. package com.home.activity;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Rect;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11. import android.view.SurfaceHolder;
  12. import android.view.SurfaceHolder.Callback;
  13. import android.view.SurfaceView;
  14. import android.view.View;
  15. import android.view.View.OnTouchListener;
  16. import com.home.surfaceviewtest.R;
  17. public class SurfaceViewActivity extends Activity {
  18. // SurfaceHolder负责维护SurfaceView上绘制的内容
  19. private SurfaceHolder holder;
  20. private Paint paint;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. paint = new Paint();
  26. // 获取SurfaceView实例
  27. SurfaceView surface = (SurfaceView) findViewById(R.id.main_sv);
  28. // 初始化SurfaceHolder对象
  29. holder = surface.getHolder();
  30. holder.addCallback(new Callback() {
  31. //当surface将要被销毁时回调该方法
  32. @Override
  33. public void surfaceDestroyed(SurfaceHolder holder) {
  34. }
  35. //当surface被创建时回调该方法
  36. @Override
  37. public void surfaceCreated(SurfaceHolder holder) {
  38. // 锁定整个SurfaceView
  39. Canvas canvas = holder.lockCanvas();
  40. // 获取背景资源
  41. Bitmap bitmap = BitmapFactory.decodeResource(
  42. SurfaceViewActivity.this.getResources(),
  43. R.drawable.image2);
  44. // 绘制背景
  45. canvas.drawBitmap(bitmap, 0, 0, null);
  46. // 绘制完成,释放画布,提交修改
  47. holder.unlockCanvasAndPost(canvas);
  48. // 重新锁两次,避免下次lockCanvas遮挡
  49. holder.lockCanvas(new Rect(0, 0, 0, 0));
  50. holder.unlockCanvasAndPost(canvas);
  51. holder.lockCanvas(new Rect(0, 0, 0, 0));
  52. holder.unlockCanvasAndPost(canvas);
  53. }
  54. //当一个surface的格式或大小发生改变时回调该方法
  55. @Override
  56. public void surfaceChanged(SurfaceHolder holder, int format,
  57. int width, int height) {
  58. }
  59. });
  60. surface.setOnTouchListener(new OnTouchListener() {
  61. @Override
  62. public boolean onTouch(View v, MotionEvent event) {
  63. // 只处理按下事件
  64. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  65. int cx = (int) event.getX();
  66. int cy = (int) event.getY();
  67. // 锁定SurfaceView的局部区域,只更新局部内容
  68. Canvas canvas = holder.lockCanvas(new Rect(cx - 60,
  69. cy - 60, cx + 60, cy + 60));
  70. // 保存canvas的当前状态
  71. canvas.save();
  72. // 旋转画布
  73. canvas.rotate(30, cx, cy);
  74. paint.setColor(Color.RED);
  75. // 绘制红色方块
  76. canvas.drawRect(cx - 40, cy - 40, cx, cy, paint);
  77. // 恢复canvas之前的保存状态
  78. canvas.restore();
  79. paint.setColor(Color.GREEN);
  80. // 绘制绿色方块
  81. canvas.drawRect(cx, cy, cx + 40, cy + 40, paint);
  82. // 绘制完成,释放画布,提交修改
  83. holder.unlockCanvasAndPost(canvas);
  84. }
  85. return false;
  86. }
  87. });
  88. }
  89. }


 


布局XML:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <SurfaceView
  6. android:id="@+id/main_sv"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"/>
  9. </LinearLayout>


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/711383
推荐阅读
相关标签
  

闽ICP备14008679号