今天介绍一下SurfaceView的用法,SurfaceView一般与SurfaceHolder结合使用,SurfaceHolder用于向与之关联的SurfaceView上绘图,调用SurfaceView的getHolder()方法即可获取SurfaceView关联的SurfaceHolder。

SurfaceHolder提供一下几个方法来获取Canvas对象:

Canvas lockCanvas():锁定整个SurfaceView对象,获取该Surface上的Canvas。

Canvas lockCanvas(Rect dirty):锁定SurfaceView上Rect划分的区域,获取该Surface上的Canvas。

当通过lockCanvas获取指定SurfaceView上的Canvas之后,接下来程序就可以调用Canvas进行绘图,绘图完成后,调用一下方法来释放绘图、提交所绘制的图形:

unlockCanvasAndPost(canvas)。

下面直接上个实例:

 public class SurfaceViewTest extends Activity
{
// SurfaceHolder负责维护SurfaceView上绘制的内容
private SurfaceHolder holder;
private Paint paint;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
paint = new Paint();
SurfaceView surface = (SurfaceView) findViewById(R.id.show);
// 初始化SurfaceHolder对象
holder = surface.getHolder();
holder.addCallback(new Callback()
{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3)
{
} @Override
public void surfaceCreated(SurfaceHolder holder)
{
// 锁定整个SurfaceView
Canvas canvas = holder.lockCanvas();
// 绘制背景
Bitmap back = BitmapFactory.decodeResource(
SurfaceViewTest.this.getResources(), R.drawable.sun);
//绘制背景
canvas.drawBitmap(back, 0, 0, null);
// 绘制完成,释放画布,提交修改
holder.unlockCanvasAndPost(canvas);
//重新锁一次,"持久化"上次所绘制的内容
holder.lockCanvas(new Rect(0, 0, 0, 0));
holder.unlockCanvasAndPost(canvas);
} @Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
});
//为surface的触摸事件绑定监听器
surface.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View source, MotionEvent event)
{
// 只处理按下事件
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
int cx = (int) event.getX();
int cy = (int) event.getY();
// 锁定SurfaceView的局部区域,只更新局部内容
Canvas canvas = holder.lockCanvas(new Rect(cx - 50,
cy - 50, cx + 50, cy + 50));
// 保存canvas的当前状态
canvas.save();
// 旋转画布
canvas.rotate(30, cx, cy);
paint.setColor(Color.RED);
// 绘制红色方块
canvas.drawRect(cx - 40, cy - 40, cx, cy, paint);
// 恢复Canvas之前的保存状态
canvas.restore();
paint.setColor(Color.GREEN);
// 绘制绿色方块
canvas.drawRect(cx, cy, cx + 40, cy + 40, paint);
// 绘制完成,释放画布,提交修改
holder.unlockCanvasAndPost(canvas);
}
return false;
}
});
}
}
04-29 02:02