SurfaceView繪圖機制

來源:互聯網
上載者:User

雖然前面都使用的View來進行繪圖,但View繪圖機制存在兩個缺陷:

1、缺乏雙緩衝機制

2、更新映像時,必須更新View上的整張圖片,效率低。

所以在遊戲繪製中推薦使用SurfaceView。它可以只對指定地區進行更新,可以提高效率。下面用一個簡單樣本來示範SurfaceView的使用:

Activity:

 

package com.home.activity;  import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener;  import com.home.surfaceviewtest.R;  public class SurfaceViewActivity extends Activity {     // SurfaceHolder負責維護SurfaceView上繪製的內容      private SurfaceHolder holder;     private Paint paint;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         paint = new Paint();         // 擷取SurfaceView執行個體          SurfaceView surface = (SurfaceView) findViewById(R.id.main_sv);         // 初始化SurfaceHolder對象          holder = surface.getHolder();         holder.addCallback(new Callback() {             //當surface將要被銷毀時回調該方法              @Override             public void surfaceDestroyed(SurfaceHolder holder) {              }             //當surface被建立時回調該方法              @Override             public void surfaceCreated(SurfaceHolder holder) {                 // 鎖定整個SurfaceView                  Canvas canvas = holder.lockCanvas();                 // 擷取背景資源                  Bitmap bitmap = BitmapFactory.decodeResource(                         SurfaceViewActivity.this.getResources(),                         R.drawable.image2);                 // 繪製背景                  canvas.drawBitmap(bitmap, 0, 0, null);                 // 繪製完成,釋放畫布,提交修改                  holder.unlockCanvasAndPost(canvas);                 // 重新鎖兩次,避免下次lockCanvas遮擋                  holder.lockCanvas(new Rect(0, 0, 0, 0));                 holder.unlockCanvasAndPost(canvas);                 holder.lockCanvas(new Rect(0, 0, 0, 0));                 holder.unlockCanvasAndPost(canvas);             }             //當一個surface的格式或大小發生改變時回調該方法              @Override             public void surfaceChanged(SurfaceHolder holder, int format,                     int width, int height) {              }         });         surface.setOnTouchListener(new OnTouchListener() {              @Override             public boolean onTouch(View v, 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 - 60,                             cy - 60, cx + 60, cy + 60));                     // 儲存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;             }         });     }  } package com.home.activity;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.os.Bundle;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.view.View;import android.view.View.OnTouchListener;import com.home.surfaceviewtest.R;public class SurfaceViewActivity extends Activity { // SurfaceHolder負責維護SurfaceView上繪製的內容 private SurfaceHolder holder; private Paint paint; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  paint = new Paint();  // 擷取SurfaceView執行個體  SurfaceView surface = (SurfaceView) findViewById(R.id.main_sv);  // 初始化SurfaceHolder對象  holder = surface.getHolder();  holder.addCallback(new Callback() {   //當surface將要被銷毀時回調該方法   @Override   public void surfaceDestroyed(SurfaceHolder holder) {   }   //當surface被建立時回調該方法   @Override   public void surfaceCreated(SurfaceHolder holder) {    // 鎖定整個SurfaceView    Canvas canvas = holder.lockCanvas();    // 擷取背景資源    Bitmap bitmap = BitmapFactory.decodeResource(      SurfaceViewActivity.this.getResources(),      R.drawable.image2);    // 繪製背景    canvas.drawBitmap(bitmap, 0, 0, null);    // 繪製完成,釋放畫布,提交修改    holder.unlockCanvasAndPost(canvas);    // 重新鎖兩次,避免下次lockCanvas遮擋    holder.lockCanvas(new Rect(0, 0, 0, 0));    holder.unlockCanvasAndPost(canvas);    holder.lockCanvas(new Rect(0, 0, 0, 0));    holder.unlockCanvasAndPost(canvas);   }   //當一個surface的格式或大小發生改變時回調該方法   @Override   public void surfaceChanged(SurfaceHolder holder, int format,     int width, int height) {   }  });  surface.setOnTouchListener(new OnTouchListener() {   @Override   public boolean onTouch(View v, 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 - 60,       cy - 60, cx + 60, cy + 60));     // 儲存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;   }  }); }} 


布局XML:


 

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <SurfaceView         android:id="@+id/main_sv"         android:layout_width="match_parent"         android:layout_height="match_parent"/>  </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <SurfaceView        android:id="@+id/main_sv"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout> 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.