SurfaceView簡單架構

來源:互聯網
上載者:User

標籤:

  • 簡介

可以直接從記憶體或者DMA等硬體介面取得映像資料,是個非常重要的繪圖容器。

它的特性是:可以在子線程向螢幕上繪圖。這樣可以避免畫圖任務繁重的時候造成主線程阻塞,從而提高了程式的反應速度。

  • 實現過程

繼承SurfaceView實現SurfaceView.Callback介面,實現介面中方法管理Surface生命週期。

SurfaceView.getHolder()獲得SurfaceHolder對象,是Surface的控制器。

SurfaceHolder.addCallback(callback)添加回調介面。

SurfaceHolder.lockCanvas()獲得Canvas對象並鎖定畫布

Canvas繪圖,在這裡進行一系列繪圖操作,被緩衝到Surface。

SurfaceHolder.unlockCanvasAndPost(canvas)結束鎖定並提交,顯示映像。

  • 代碼
 1 import android.content.Context; 2 import android.graphics.Canvas; 3 import android.graphics.Paint; 4 import android.util.AttributeSet; 5 import android.view.SurfaceHolder; 6 import android.view.SurfaceView; 7  8 public abstract class SimpleSurfaceView extends SurfaceView implements 9         SurfaceHolder.Callback, Runnable {10 11     private Canvas canvas;12     private SurfaceHolder holder;// Surface的控制器13     private boolean isRun;14     protected long sleepTime = 500;// 刷幀頻率,睡眠毫秒數15     private Paint paint;16 17     /**18      * 構造方法19      */20     public SimpleSurfaceView(Context context) {21         super(context);22         this.holder = getHolder();// 擷取SurfaceHolder對象23         holder.addCallback(this);// 給這個SurfaceHolder添加Surface生命週期回調24 25         paint = new Paint();//建立畫筆對象26         paint.setAntiAlias(true);//設定畫筆消除鋸齒27     }28 29     /**30      * 構造方法,以xml方式構建介面是必須有。31      */32     public SimpleSurfaceView(Context context, AttributeSet attrs) {33         super(context, attrs);34         this.holder = getHolder();// 擷取SurfaceHolder對象35         holder.addCallback(this);// 給這個SurfaceHolder添加Surface生命週期回調36 37         paint = new Paint();//建立畫筆對象38         paint.setAntiAlias(true);//設定畫筆消除鋸齒39     }40 41     /**42      * 來自Callback介面, Surface生命週期方法,建立時調用43      */44     @Override45     public void surfaceCreated(SurfaceHolder surfaceHolder) {46         isRun = true;47         new Thread(this).start();48     }49 50     /**51      * 來自Callback介面, Surface生命週期方法,改變時調用52      */53     @Override54     public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2,55             int i3) {56 57     }58 59     /**60      * 來自Callback介面, Surface生命週期方法,銷毀時調用61      */62     @Override63     public void surfaceDestroyed(SurfaceHolder surfaceHolder) {64         isRun = false;65     }66 67     /**68      * 來自Runnable介面69      */70     @Override71     public void run() {72         while (isRun) {73             try {74                 canvas = holder.lockCanvas();//鎖定畫布75                 simpleDraw(canvas, paint);// 子類重寫此方法繪圖76             } catch (Exception e) {77                 e.printStackTrace();78             } finally {79                 if (canvas != null)80                     holder.unlockCanvasAndPost(canvas);//解鎖畫布81             }82 83             // 睡眠片刻,降低刷幀頻率84             try {85                 Thread.sleep(sleepTime);86             } catch (InterruptedException e) {87                 e.printStackTrace();88             }89         }90     }91 92     protected abstract void simpleDraw(Canvas canvas, Paint paint);// 繪圖,由子類具體實現93 }

 

SurfaceView簡單架構

聯繫我們

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