View 和 SurfaceView的區別

來源:互聯網
上載者:User

標籤:des   java   color   使用   os   資料   io   width   

這裡說的是在繪圖中兩者的區別:

1View在繪圖中,重寫onDraw(Canvas canvas)方法,通過invaldate()和pastInvalidate()兩個方法進行重新繪製畫布;

invalidate()不能再自己建立的線程中迴圈調用;

postInvalidate()可以在自己建立的線程中迴圈調用執行。如果不在當前View 建立線程迴圈重繪畫布的話,這兩種重繪畫布的函數就沒什麼區別了,都可以用。

public class MyView extends View {private float x;private float y;public MyView(Context context) {super(context);}//需要繪製映像時調用@Overrideprotected void onDraw(Canvas canvas) {Paint mPaint = new Paint();mPaint.setColor(Color.BLUE);canvas.drawLine(0, 0, x, y, mPaint);super.onDraw(canvas);}@Overridepublic boolean onTouchEvent(MotionEvent event) {x = event.getX();y = event.getY();//重繪畫布invalidate();//postInvalidate();return true;}}

2,surfaceView繼承自view

在使用surfaceView時,你會發現複寫父類的onDraw(Canvas canvas)方法並不會執行。原因是在SurfaceView中,我們並不會直接跟他打交道,而是通過SurfaceHolder來控制。使用SurfaceHolder的lockCanvas()函數來擷取到SurfaceView的Canvas對象,再通過Canvas上繪製內容來修改SurfaceView中的資料

public class MySurfaceView extends SurfaceView implements Callback, Runnable {private SurfaceHolder sfh;private Paint paint;private Canvas canvas;private Thread th;private boolean flag;private int textX;private int textY;public MySurfaceView(Context context) {super(context);sfh = this.getHolder();sfh.addCallback(this);paint = new Paint();paint.setColor(Color.WHITE);}@Overridepublic void surfaceCreated(SurfaceHolder holder) {flag = true;th = new Thread(this);th.start();}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false;}@Overridepublic boolean onTouchEvent(MotionEvent event) {textX = (int) event.getX();textY = (int) event.getY();return true;}public void myDraw() {// 擷取一個加鎖的畫布,為了防止surfaceview在繪製過程中被修改,銷毀等狀況Paint blackPaint = new Paint();blackPaint.setColor(Color.BLACK);try {canvas = sfh.lockCanvas();if (canvas != null) {canvas.drawRect(0, 0, getWidth(), getHeight(), blackPaint);// 刷屏1//canvas.drawColor(Color.BLACK);//刷屏2//canvas.drawRGB(0, 0, 0);//刷屏3canvas.drawText("game", textX, textY, paint);}} catch (Exception e) {e.printStackTrace();}finally{if (canvas != null) {// 解鎖畫布和提交sfh.unlockCanvasAndPost(canvas);}}}/** * 遊戲邏輯 */private void logic() {}@Overridepublic void run() {while (flag) {long start = System.currentTimeMillis();myDraw();logic();long end = System.currentTimeMillis();try {if (end - start < 50) {Thread.sleep(50 - (end - start));}} catch (InterruptedException e) {e.printStackTrace();}}}}

他們兩個更新畫布的區別:

在View視圖中對於畫布的重新繪製,是通過調用View 提供的postInvalidate()與inValidate()這兩個函數來執行的,也就是說畫布是由系統主UI進行更新。那麼當系統主UI線程被繪製函數阻塞,這樣一來則會引發無法響應按鍵,觸屏等訊息的問題;

SurfaceView視圖中對於畫布的重繪是由一個新的單獨線程去執行處理,所以不會出現因主UI線程阻塞而導致無法響應按鍵,觸屏資訊等問題。


surfaceview 有雙緩衝機制,而view 沒有




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.