高效能繪圖桌布-Canvas
SurfaceView為高效能繪圖介面, 也是邁向遊戲設計之路的起點. 從前都是在Activity中放入R.layout.xxx的畫面設定. 如今, 只是在Activity中放入一個SurfaceView, 再由SurfaceView的holder.lockCanvas()取得一張圖畫紙(canvas), 然後在這張圖畫紙上畫上想要畫的圖形.
使用Surface元件, 需繼承SurfaceView並實作SurfaceHolder.Callback的三個方法
surfaceCreated(), surfaceChanged(), surfaceDestroyed()
在Canvas圖畫紙上, 可以畫線, 畫圓, 畫圖檔
public class MyCanvas extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder holder=getHolder(); Canvas canvas; Paint paint; public MyCanvas(Context context) { super(context); holder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { canvas=holder.lockCanvas(); paint=new Paint(); paint.setColor(Color.RED); canvas.drawCircle(500,500,250, paint); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); canvas.drawLine(200,500,800,500, paint); canvas.drawLine(500, 200, 500, 800, paint); holder.unlockCanvasAndPost(canvas); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {} @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) {} }
然後在MainActivity的onCreate()更改setContentView即可
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(new MyCanvas(this));
}
}
清空畫布
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
反彈球
下面代碼, 畫了一顆球, 並在Canvas持續的移動反彈
public class MyCanvas extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder holder=getHolder(); Canvas canvas; Paint paint=new Paint(); int r=30; int cx=r, cy=r; int xdir=1, ydir=1; int xspeed=10, yspeed=10; public MyCanvas(Context context) { super(context); holder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(()->{ while(true) { canvas = holder.lockCanvas(); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); Log.d("Thomas", String.format("%d, %d", canvas.getWidth(), canvas.getHeight())); paint.setColor(Color.RED); cx+=xdir*xspeed;cy+=ydir*yspeed; if(cx>canvas.getWidth()-r){ cx=canvas.getWidth()-r; xdir=-xdir; } if(cx<r){ cx=r; xdir=-xdir; } if(cy>canvas.getHeight()-r){ cy=canvas.getHeight()-r; ydir=-ydir; } if(cy<r){ cy=r; ydir=-ydir; } canvas.drawCircle(cx, cy, r, paint); holder.unlockCanvasAndPost(canvas); } }).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} @Override public void surfaceDestroyed(SurfaceHolder holder) {} }