搜尋此網誌

2012年6月1日 星期五

Surface View example

package my.surface;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceActivity extends Activity {
    SurfaceActivity.MyCallBack myCallBacks = new MyCallBack();
    SurfaceView sView = null; //Create an abstract view which can display something later.
    SurfaceHolder sHolder = null;//with holder to do actual painting
    
    //This class is created for actual behaviors when meets following status :surface create , destroy and change
    private class MyCallBack implements SurfaceHolder.Callback{

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            
        }

        public void surfaceCreated(SurfaceHolder holder) {
            Canvas canvas = holder.lockCanvas();
            canvas.drawColor(Color.GREEN);
            holder.unlockCanvasAndPost(canvas);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            
        }
        
    }
    
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        sView = new SurfaceView(this);//This activity is treated as a context for the surface view.
        sHolder = sView.getHolder();//get a holder from this view to do control.
        sHolder.addCallback(myCallBacks);
        setContentView(sView);
    }

}

沒有留言:

張貼留言