Android SurfaceView学习

常用的三种视图 :

  • View : 内置了画布(Canvas) , 提供了按键事件、触屏事件处理的回调方法,适用于被动刷屏游戏设计。
  • SurfaceView : 是View的派生类,实现内部的双缓冲技术,更适用于主动刷屏的2D游戏设计。
  • GLSurfaceView : 是SurfaceView的派生类,适用于3D游戏设计

1、extends SurfaceView
2、implement SurfaceHolder.Callback
监视surface的创建、改变和销毁

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

3、SurfaceHolder
通过getHolder()获取SurfaceHolder对象
4、Canvas
surface里面包含Canvas
getholder().lockCanvas() 获取Canvas对象
mHolder.unlockCanvasAndPost(mCanvas);
Api:

Canvas
android.view.SurfaceHolder.lockCanvas()

Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface’s bitmap. A null is returned if the surface has not been created or otherwise cannot be edited. You will usually need to implement Callback.surfaceCreated to find out when the Surface is available for use.

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved.

If you call this repeatedly when the Surface is not ready (before Callback.surfaceCreated or after Callback.surfaceDestroyed), your calls will be throttled to a slow rate in order to avoid consuming CPU.

If null is not returned, this function internally holds a lock until the corresponding unlockCanvasAndPost call, preventing SurfaceView from creating, destroying, or modifying the surface while it is being drawn. This can be more convenient than accessing the Surface directly, as you do not need to do special synchronization with a drawing thread in Callback.surfaceDestroyed.

Returns:
Canvas Use to draw into the surface.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值