欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程之SurfaceView学习示例详解

程序员文章站 2023-11-28 21:47:58
本文实例讲述了android编程之surfaceview学习示例。分享给大家供大家参考,具体如下: surfaceview是view的子类,使用的方式与任何view所派生...

本文实例讲述了android编程之surfaceview学习示例。分享给大家供大家参考,具体如下:

surfaceview是view的子类,使用的方式与任何view所派生的类都是完全相同的,可以像其他view那样应用动画,并把它们放到布局中。

surfaceview封装的surface支持使用本章前面所描述的所有标准canvas方法进行绘图,同时也支持完全的opengl es库。

使用opengl,你可以再surface上绘制任何支持的2d或者3d对象,与在2d画布上模拟相同的效果相比,这种方法可以依靠硬件加速(可用的时候)来极大地提高性能。

对于显示动态的3d图像来说,例如,那些使用google earth功能的应用程序,或者那些提供沉浸体验的交互式游戏,surfaceview特别有用。它还是实时显示摄像头预览的最佳选择。

surfaceview 和 view 的明显不同之处在于:

1、继承surfaceview  的视图可以另起一个线程,或者说在子线程中更新视图。
2、 surfaceview 的画图方法是在子线程中执行的 而 view类的那个示例 的画图方法是在ui线程中执行的。
3、surfaceview在绘图之前必须使用lockcanvas 方法锁定画布,并得到画布,然后再画布上绘制;当绘制完成后,使用unlockcanvasandpost 方法解锁画布,然后就显示到屏幕上。
surfaceview 类的事件处理规则和view一样。

具体示例:

activity

public class activity01 extends activity { 
  gamesurfaceview mgamesurfaceview; 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    mgamesurfaceview = new gamesurfaceview(this); 
    setcontentview(mgamesurfaceview); 
  } 
  @override 
  public boolean ontouchevent(motionevent event) { 
    if(event.getaction() == motionevent.action_down){ 
      mgamesurfaceview.x = event.getx(); 
      mgamesurfaceview.y = event.gety(); 
    } 
    return true; 
  } 
  @override 
  public boolean onkeydown(int keycode, keyevent event) { 
    if(keycode == keyevent.keycode_back){ 
      this.finish(); 
    } 
    return true; 
  } 
}

gamesurfaceview

public class gamesurfaceview extends surfaceview implements surfaceholder.callback, runnable { 
  boolean mbloop = false; 
  surfaceholder msurfaceholder = null; 
  int count = 0; 
  float x = 50, y = 50; 
  int screenwidth = 480, screenheight = 800; 
  public gamesurfaceview(context context) { 
    super(context); 
    mbloop = true; 
    msurfaceholder = this.getholder(); 
    msurfaceholder.addcallback(this); 
    this.setfocusable(true); 
  } 
  @override 
  public void surfacecreated(surfaceholder holder) { 
    new thread(this).start();  // start paint thread 
  } 
  @override 
  public void surfacechanged(surfaceholder holder, int format, int width, int height) { 
    screenwidth = width;    // reset width when screen orientation is changed 
    screenheight = height;   // reset height when screen orientation is changed 
  } 
  @override 
  public void surfacedestroyed(surfaceholder holder) { 
    mbloop = false; 
  } 
  @override 
  public void run() { 
    while (mbloop) { 
      synchronized (msurfaceholder) { 
        ondraw(); 
      } 
      try { 
        thread.sleep(200); 
      } catch (exception e) { 
      } 
    } 
  } 
  public void ondraw() { 
    canvas canvas = msurfaceholder.lockcanvas(); 
    if (msurfaceholder == null || canvas == null) { 
      return; 
    } 
    if (count < 100) { 
      count++; 
    } else { 
      count = 0; 
    } 
    paint mpaint = new paint(); 
    mpaint.setantialias(true); 
    mpaint.setcolor(color.cyan); 
    canvas.drawrect(0, 0, screenwidth, screenheight, mpaint);  // repaint background color 
    switch (count % 4) { 
    case 0: 
      mpaint.setcolor(color.blue); 
      break; 
    case 1: 
      mpaint.setcolor(color.green); 
      break; 
    case 2: 
      mpaint.setcolor(color.red); 
      break; 
    case 3: 
      mpaint.setcolor(color.yellow); 
      break; 
    default: 
      mpaint.setcolor(color.white); 
      break; 
    } 
    canvas.drawcircle(x, y, 50, mpaint); 
    msurfaceholder.unlockcanvasandpost(canvas); 
  } 
}

运行效果:

Android编程之SurfaceView学习示例详解

完整实例代码代码点击此处。

希望本文所述对大家android程序设计有所帮助。