Android -- SurfaceView繪製

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   os   io   ar   

SurfaceView                                                                          

SurfaceView是View的一個特殊子類,它的目的是另外提供一個線程進行繪製操作。

  • 步驟

1.用SurfaceView進行繪製,首先要建立一個類,繼承 SurfaceView,同時這個類應該實現SurfaceHolder.Callback介面。

  這個介面中的三個回呼函數(surfaceChanged(SurfaceHolder holder, int format, int width, int height),surfaceCreated(SurfaceHolder holder),surfaceDestroyed(SurfaceHolder holder))分別對應Surface何時更改、建立和銷毀。

2.對Surface對象的操作是通過SurfaceHolder來進行的。所以,在你的SurfaceView類初始化的時候,你需要調用 getHolder()來獲得SurfaceHolder對象,然後用addCallback()加上回調介面(因為你的類實現了相應的介面,所以此處傳入this即可)。

3.在你的SurfaceView類中應該建立一個線程類,處理繪製操作。為此,要向這個線程類傳遞上面獲得的SurfaceHolder對象。

4.繪製:線上程類的run()方法中進行繪製操作,通過lockCanvas()方法獲得Canvas對象,然後就可以用這個對象進行繪製,繪製完成後調用unlockCanvasAndPost(),傳入Canvas對象,這時Surface將會按Canvas進行繪製。

  注意:每次利用SurfaceHolder獲得畫布時,前一次的內容將會保留。

Code                                                                                   
public class MySurfaceView extends SurfaceView implements        SurfaceHolder.Callback{    private DrawThread mThread = null;    public MySurfaceView(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        init();    }    public MySurfaceView(Context context, AttributeSet attrs)    {        super(context, attrs);        init();    }    public MySurfaceView(Context context)    {        super(context);        init();    }    private void init()    {        Log.d(AppConstants.LOG_TAG, "init");        SurfaceHolder holder = getHolder();        holder.addCallback(this);        mThread = new DrawThread(holder);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh)    {        Log.d(AppConstants.LOG_TAG, "onSizeChanged");        super.onSizeChanged(w, h, oldw, oldh);    }    @Override    public void surfaceCreated(SurfaceHolder holder)    {        Log.d(AppConstants.LOG_TAG, "surfaceCreated");        mThread.setRun(true);        mThread.start();    }    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width,            int height)    {        Log.d(AppConstants.LOG_TAG, "surfaceChanged");    }    @Override    public void surfaceDestroyed(SurfaceHolder holder)    {        Log.d(AppConstants.LOG_TAG, "surfaceDestroyed");        mThread.setRun(false);    }    /**     * 繪製線程類     *      */    public class DrawThread extends Thread    {        private SurfaceHolder mHolder = null;        private boolean isRun = false;        public DrawThread(SurfaceHolder holder)        {            Log.d(AppConstants.LOG_TAG, "DrawThread Constructor");            mHolder = holder;        }        public void setRun(boolean isRun)        {            Log.d(AppConstants.LOG_TAG, "DrawThread setRun: " + isRun);            this.isRun = isRun;        }        @Override        public void run()        {            Log.d(AppConstants.LOG_TAG, "DrawThread run");            int count = 0;            while (isRun)            {                Canvas canvas = null;                synchronized (mHolder)                {                    try                    {                        Log.d(AppConstants.LOG_TAG, "Drawing-------------");                        canvas = mHolder.lockCanvas();                        canvas.drawColor(Color.WHITE);                        Paint p = new Paint();                        p.setColor(Color.BLACK);                        Rect r = new Rect(100, 50, 300, 250);                        canvas.drawRect(r, p);                        canvas.drawText("這是第" + (count++) + "秒", 100, 310, p);                        Thread.sleep(1000);// 睡眠時間為1秒                    }                    catch (Exception e)                    {                        Log.d(AppConstants.LOG_TAG, "throw Exception in run");                        e.printStackTrace();                    }                    finally                    {                        if (null != canvas)                        {                            mHolder.unlockCanvasAndPost(canvas);                        }                    }                }            }        }    }}
我是天王蓋地虎的分割線                                                             

 

 

 

參考:http://www.cnblogs.com/mengdd/archive/2013/04/09/3009897.html

Android -- 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.