android中用SurfaceHolder處理SurfaceView的畫圖

來源:互聯網
上載者:User

在用SurfaceView進行遊戲開發過程中,用到SurfaceHolder來處理它的Canvas上畫的效果和動畫是必不可少的。用於控製表面,大小,像素等。
Abstract interface to someone holding a display surface. Allows you to control the surface size and format, 
edit the pixels in the surface, and monitor changes to the surface. This interface is typically available 
through the SurfaceView class. 
其中特別要注意以下的幾個函數:
abstract void addCallback(SurfaceHolder.Callback callback);
// 給SurfaceView當前的持有人一個回調對象。
abstract Canvas lockCanvas();
// 鎖定畫布,一般在鎖定後就可以通過其返回的畫布對象Canvas,在其上面畫圖等操作了。
abstract Canvas lockCanvas(Rect dirty);
// 鎖定畫布的某個地區進行畫圖等..因為畫完圖後,會調用下面的unlockCanvasAndPost來改變顯示內容。
// 相對部分記憶體要求比較高的遊戲來說,可以不用重畫dirty外的其它地區的像素,可以提高速度。
abstract void unlockCanvasAndPost(Canvas canvas);
// 結束鎖定畫圖,並提交改變。

例子:

    class DrawThread extends Thread {        private SurfaceHolder holder;        private boolean running = true;        protected DrawThread(SurfaceHolder holder) {this.holder = holder;}        protected void doStop() { running = false; }        public void run() {            Canvas c = null;            while( running ) {                c = holder.lockCanvas(null);                // 鎖定整個畫布,在記憶體要求比較高的情況下,建議參數不要為null                try {                    synchronized(holder) {                        bGrid.drawGrid(c);//畫遊戲中的網格                        BBoom.drawBooms(c, booms); //畫遊戲中的炸彈                        bFairy.drawFairy(c);//畫遊戲中的主角                        // 畫的內容是z軸的,後畫的會覆蓋前面畫的。                    }                } catch(Exception ex) {}                finally {                    holder.unlockCanvasAndPost(c);                    //更新螢幕顯示內容                }                }        }    };

轉自 http://blog.csdn.net/py890000/article/details/5439233

聯繫我們

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