android 小遊戲 ---- 數獨(二)

來源:互聯網
上載者:User

android 小遊戲 ---- 數獨(二)
> 首先建立一個自己的View類 -->繼承SurfaceView並實現SurfaceHolder.Callback介面  --> SurfaceView.getHolder()獲得SurfaceHolder對象  -->SurfaceHolder.addCallback(callback) 添加回呼函數 -->SurfaceHolder.lockCanvas()獲得Canvas對象並鎖定畫布 --> Canvas繪畫  -->SurfaceHolder.unlockCanvasAndPost(Canvas canvas)結束鎖定,並提交改變,將圖形顯示。      public class GameSFView extends SurfaceView implements Callback{     private SurfaceHolder surfaceHolder;    private int selectedX = 0;        private int selectedY = 0;    /**     * 儲存格的寬度     */    private float width;        /**     *儲存格的高度      */    private float height;    /**     *main.xml 中引用此surfaceview類 構造方法必須使用兩個形參的形式      */    public GameSFView(Context context, AttributeSet attrs) {        super(context, attrs);        surfaceHolder = this.getHolder(); // 擷取SurfaceHolder對象        //監聽Surface的生命週期  給SurfaceView當前的持有人一個回調對象。        surfaceHolder.addCallback(this); // 添加回調            }} 實現三個回呼函數 //在surface的大小發生改變時激發    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {            }     //在建立時激發,一般在這裡調用畫圖的線程。    //最好在Surface被建立的時候,開啟繪圖線程。     //子線程繪圖 緩衝到surface中    @Override    public void surfaceCreated(SurfaceHolder holder) {        draw();    }     //銷毀時激發,一般在這裡將畫圖的線程停止、釋放。    //最好在Surface被銷毀的時候,銷毀繪圖線程    @Override    public void surfaceDestroyed(SurfaceHolder holder) {     }   自訂繪製方法  /**     * 自訂繪製方法     */    public void draw() {        synchronized(surfaceHolder){            // 擷取Canvas對象            // 鎖定畫布,一般在鎖定後就可以通過其返回的畫布對象Canvas,在其上面畫圖等操作了。            Canvas canvas = null;            Paint paint = new Paint();            try{                canvas = surfaceHolder.lockCanvas(); // 鎖住Canvas                                // 清理螢幕 繪製背景                initGameView(canvas, paint);                //重繪surfaceview 填充新增的資料                inflateNewNum(canvas,paint);            }catch(Exception e){                e.printStackTrace();            }finally{                if(canvas != null)                // 結束鎖定畫圖,並提交改變。                surfaceHolder.unlockCanvasAndPost(canvas);  // 解鎖Canvas,更新            }        }        } 這裡最好使用同步鎖,並且在使用try catch finally 進行異常捕獲。    private void initGameView(Canvas canvas,Paint paint){        //bgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background);        //初始化遊戲背景        initGameBg(canvas,paint);        //初始化遊戲線條        initGameLine(canvas,paint);        //初始化遊戲資料        initFirstNumber(canvas,paint);            }   繪製棋盤上的線條private void initGameLine(Canvas canvas,Paint paint) {        //設定畫筆顏色        paint.setColor(Color.BLACK);                //畫橫線        for(int i=1;i<10;i++){                        canvas.drawLine(0 ,i*height, getWidth(), i*height, paint);        }        //畫豎線        for(int i=1;i<9;i++){                        canvas.drawLine(i*height,0, i*height,getWidth(), paint);        }        //畫三道橫粗線        paint.setStrokeWidth(4);        for(int i=1;i<4;i++){            canvas.drawLine(0 ,i*height*3, getWidth(), i*height*3, paint);        }        //畫三道豎粗線        for(int i=1;i<9;i++){                        canvas.drawLine(i*height*3,0, i*height*3,getWidth(), paint);        }    }

聯繫我們

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