Android之SurfaceView簡介(三)

    在android中開發遊戲,一般來說,或想寫一個複雜一點的遊戲,是必須用到SurfaceView來開發的。經過這一陣子對android的學習,我找到了自已在android中游戲開發的誤區,不要老想着用Layout和view去實現,不要將某個遊戲中的對象做成一個組件來處理。應該儘量想着在Canvas(畫布)中畫出遊戲中的背景、人物、動畫等。
    SurfaceView提供直接訪問一個可畫圖的界面,可以控制在界面頂部的子視圖層。SurfaceView是提供給需要直接畫像素而不是使用

窗體部件的應用使用的。Android圖形系統中一個重要的概念和線索是surface。View及其子類(如TextView, Button)要畫在surface上。每個surface創建一個Canvas對象(但屬性時常改變),用來管理view在surface上的繪圖操作,如畫點畫線。

    還要注意的是,使用它的時候,一般都是出現在最頂層的:The view hierarchy will take care of correctly compositing
with the Surface any siblings of the SurfaceView that would normally appear on top of it.

    使用SurfaceView的時候,一般情況下還要對其進行創建,銷燬,改變時的情況進行監視,這就要用到SurfaceHolder.Callback.



例子1:

public class BBatt extends SurfaceView implements SurfaceHolder.Callback, OnKeyListener {
    private BFairy bFairy;
    private DrawThread drawThread;
    public BBatt(Context context) {
        super(context);
        this.setLayoutParams(new ViewGroup.LayoutParams(Global.battlefieldWidth, Global.battlefieldHeight));
        this.getHolder().addCallback( this );
        this.setFocusable( true );
        this.setOnKeyListener( this );
        bFairy = new BFairy(this.getContext());
    }
    public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
        drawThread = new DrawThread(holder);
        drawThread.start();
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
        if( drawThread != null ) {
            drawThread.doStop();
            while (true) try {
                drawThread.join();
                break ;
            } catch(Exception ex) {}
         }
    }
    public boolean onKey(View view, int keyCode, KeyEvent event) {}
}


實例2: 用線程畫一個藍色的長方形。



public class Test extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
    //內部類
    class MyView extends SurfaceView implements SurfaceHolder.Callback{
        SurfaceHolder holder;
        public MyView(Context context) {
            super(context);
            holder = this.getHolder();//獲取holder
            holder.addCallback(this);
           //setFocusable(true);
        }
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            new Thread(new MyThread()).start();
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {}
        //內部類的內部類
        class MyThread implements Runnable{
            @Override
            public void run() {
                Canvas canvas = holder.lockCanvas(null);//獲取畫布
                Paint mPaint = new Paint();
                mPaint.setColor(Color.BLUE);
                canvas.drawRect(new RectF(40,60,80,80), mPaint);
                holder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的圖像
            }
        }
    }
}


   訪問SurfaceView的底層圖形是通過SurfaceHolder接口來實現的,通過getHolder()方法可以得到這個SurfaceHolder對象。你應該實現surfaceCreated(SurfaceHolder)和surfaceDestroyed(SurfaceHolder)方法來知道在這個Surface在窗口的顯示和隱藏過程中是什麼時候創建和銷燬的。
    SurfaceView可以在多線程中被訪問。
    注意:一個SurfaceView只在SurfaceHolder.Callback.surfaceCreated() 和 SurfaceHolder.Callback.surfaceDestroyed()調用之間是可用的,其他時間是得不到它的Canvas對象的(null)。
    我的訪問過程:
        創建一個SurfaceView的子類,實現SurfaceHolder.Callback接口。
        得到這個SurfaceView的SurfaceHolder對象holder。
        holder.addCallback(callback),也就是實現SurfaceHolder.Callback接口的類對象。
        在SurfaceHolder.Callback.surfaceCreated()調用過後holder.lockCanvas()對象就可以得到SurfaceView對象對應的Canvas對象canvas了。
        用canvas對象畫圖。
        畫圖結束後調用holder.unlockCanvasAndPost()就把圖畫在窗口中了。
        SurfaceView可以多線程訪問,在多線程中畫圖。


    如何讓 SurfaceView 響應事件,當然創建你自己的類時,你還是得extends SurfaceView and implements Callback接口,然後在構造函數裏設置一個屬性 this.setLongClickable(true);//這裏很重要,它是讓你的設備支持長按效果的屬性,如果它爲false 的時候MotionEvent 只能監聽到ACTION_DOWN這個事件。

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private Context mContext;
    private SurfaceHolder mHolder;
    public TouchScreenAdjusterSurfaceView(Context context,) {
        super(context);
        mContext = context;
        mHolder = TouchScreenAdjusterSurfaceView.this.getHolder();
        mHolder.addCallback(TouchScreenAdjusterSurfaceView.this);
        this.setFocusableInTouchMode(true); // to make sure that we can get
        // touch events and key events,and
        // "setFocusable()" to make sure we
        // can get key events
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //now you can get the Canvas and draw something here
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
    public void drawMyShape(PointPostion ps) {
        mCanvas = mHolder.lockCanvas();
       // draw anything you like
       mHolder.unlockCanvasAndPost(mCanvas);
    }                                                                                                                                                                                                                                          
    @Override
    public boolean onTouchEvent(MotionEvent event){
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("MotionEvent", "ACTION_DOWN");
            break;
            case MotionEvent.ACTION_UP:
                Log.d("MotionEvent", "ACTION_UP");
            break;
            case MotionEvent.ACTION_MOVE:
                Log.d("MotionEvent", "ACTION_MOVE");
            break;
        }
        return super.onTouchEvent(event);
    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章