View与SurfaceView 的区别用法
一、View是没有缓存机制的,每次绘图都是重新绘制
如果要实用双缓冲机制的话。
1、自定义一个View 实现onDraw方法
2、调用这个方法,调用刷新onDraw()方法
ssinView.postInvalidate();
需要格外创建一个Canvas,每次都
3、
public void onDraw(Canvas canvas){ if(x >= 480){ x = 0; //清空画布 canvas.drawColor(Color.BLACK); //重新创建一个新的图 bitmap = Bitmap.createBitmap(480, 104, Config.ARGB_8888); }else{ //在缓冲中将之前的图绘制好,然后在真正的Canvas上绘制缓冲中的图 Canvas c = new Canvas(); c.setBitmap(bitmap); int y = (int)(A*Math.sin(x/180.0f*Math.PI)); System.out.println("==================y:"+y+"===x:"+x); c.drawPoint(x, y+A, paint); //canvas.save(); canvas.drawBitmap(bitmap, 0, 0,paint); x+=5; } }
二、Surface实现了双缓冲技术
提供了一个类SurfaceHolder
1、继承SurfaceView 实现Callback接口(在new SurfaceView的时候会自动回调)
2、刷新SurfaceView
// TODO Auto-generated method stub super.run(); while(flag == true){ //锁定,获得Canvas Canvas canvas = surfaceHolder.lockCanvas(null); onDraw(canvas); //释放锁 surfaceHolder.unlockCanvasAndPost(canvas); asinSurfaceView.x += 5; try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
相关推荐
琴弦第七 2012-07-11
nickey 2012-05-26
jackeysion 2016-03-21
peixiaopao 2015-01-26
lerayZhang 2014-02-15
wocao 2013-02-17
梦想是会飞的猪 2012-09-21
xuguolibeyondboy 2012-09-14
sunstarchen 2012-08-07
做好我自己 2012-07-11
lxrlxr 2012-07-11
fzfreeworld 2012-05-21
yxx0 2012-04-26
scott0 2012-04-24
PrisonJoker 2012-03-27
mywcyfl 2012-03-27
ohdajing 2012-02-15