日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > web前端 > htmlcss > Android之SurfaceView學(xué)習(xí)(一)

Android之SurfaceView學(xué)習(xí)(一)

來源:程序員人生   發(fā)布時(shí)間:2015-04-02 08:08:16 閱讀次數(shù):3295次

首先我們先來看下官方API對(duì)SurfaceView的介紹

SurfaceView的API介紹

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen

The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. 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. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.

Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder().

The Surface will be created for you while the SurfaceView's window is visible; you should implement surfaceCreated(SurfaceHolder) and surfaceDestroyed(SurfaceHolder) to discover when the Surface is created and destroyed as the window is shown and hidden.

One of the purposes of this class is to provide a surface in which a secondary thread can render in to the screen. If you are going to use it this way, you need to be aware of some threading semantics:

?All SurfaceView and SurfaceHolder.Callback methods will be called from the thread running the SurfaceView's window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.
?You must ensure that the drawing thread only touches the underlying Surface while it is valid -- between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed().
對(duì)應(yīng)的中文翻譯
SurfaceView是視圖(View)的繼承類,這個(gè)視圖里內(nèi)嵌了1個(gè)專門用于繪制的Surface。你可以控制這個(gè)Surface的格式和尺寸。Surfaceview控制這個(gè)Surface的繪制位置。
        surface是縱深排序(Z-ordered)的,這表明它總在自己所在窗口的后面。surfaceview提供了1個(gè)可見區(qū)域,只有在這個(gè)可見區(qū)域內(nèi) 的surface部份內(nèi)容才可見,可見區(qū)域外的部份不可見。surface的排版顯示遭到視圖層級(jí)關(guān)系的影響,它的兄弟視圖結(jié)點(diǎn)會(huì)在頂端顯示。這意味者 surface的內(nèi)容會(huì)被它的兄弟視圖遮擋,這1特性可以用來放置遮蓋物(overlays)(例如,文本和按鈕等控件)。注意,如果surface上面 有透明控件,那末它的每次變化都會(huì)引發(fā)框架重新計(jì)算它和頂層控件的透明效果,這會(huì)影響性能。
        你可以通過SurfaceHolder接口訪問這個(gè)surface,getHolder()方法可以得到這個(gè)接口。
        surfaceview變得可見時(shí),surface被創(chuàng)建;surfaceview隱藏前,surface被燒毀。這樣能節(jié)省資源。如果你要查看 surface被創(chuàng)建和燒毀的時(shí)機(jī),可以重載surfaceCreated(SurfaceHolder)和 surfaceDestroyed(SurfaceHolder)。
        surfaceview的核心在于提供了兩個(gè)線程:UI線程和渲染線程。這里應(yīng)注意:
        1> 所有SurfaceView和SurfaceHolder.Callback的方法都應(yīng)當(dāng)在UI線程里調(diào)用,1般來講就是利用程序主線程。渲染線程所要訪問的各種變量應(yīng)當(dāng)作同步處理。
        2> 由于surface可能被燒毀,它只在SurfaceHolder.Callback.surfaceCreated()和 SurfaceHolder.Callback.surfaceDestroyed()之間有效,所以要確保渲染線程訪問的是合法有效的surface。

 

接下來呢,說說自己對(duì)它的理解
1、定義

可以直接從內(nèi)存或DMA等硬件接口獲得圖象數(shù)據(jù),是個(gè)非常重要的繪圖容器。

它的特性是:可以在主線程以外的線程中向屏幕繪圖上。這樣可以免畫圖任務(wù)沉重的時(shí)候造成主線程阻塞,從而提高了程序的反應(yīng)速度。在游戲開發(fā)中多用到SurfaceView,游戲中的背景、人物、動(dòng)畫等等盡可能在畫布canvas中畫出。

2、實(shí)現(xiàn)

首先繼承SurfaceView并實(shí)現(xiàn)SurfaceHolder.Callback接口
使用接口的緣由:由于使用SurfaceView 有1個(gè)原則,所有的繪圖工作必須得在Surface 被創(chuàng)建以后才能開始(Surface―表面,這個(gè)概念在 圖形編程中常常被提到。基本上我們可以把它當(dāng)作顯存的1個(gè)映照,寫入到Surface 的內(nèi)容
                      可以被直接復(fù)制到顯存從而顯示出來,這使得顯示速度會(huì)非常快),而在Surface 被燒毀之前必須結(jié)束。所以Callback 中的surfaceCreated 和surfaceDestroyed 就成了繪圖處理代碼的邊界。

需要重寫的方法

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

     //在surface的大小產(chǎn)生改變時(shí)激起

 (2)public void surfaceCreated(SurfaceHolder holder){}

     //在創(chuàng)建時(shí)激起,1般在這里調(diào)用畫圖的線程。

 (3)public void surfaceDestroyed(SurfaceHolder holder) {}

     //燒毀時(shí)激起,1般在這里將畫圖的線程停止、釋放。

全部進(jìn)程:繼承SurfaceView并實(shí)現(xiàn)SurfaceHolder.Callback接口 ----> SurfaceView.getHolder()取得SurfaceHolder對(duì)象 ---->SurfaceHolder.addCallback(callback)添加回調(diào)函數(shù)---->SurfaceHolder.lockCanvas()取得Canvas對(duì)象并鎖定畫布----> Canvas繪畫 ---->SurfaceHolder.unlockCanvasAndPost(Canvas canvas)結(jié)束鎖定畫圖,并提交改變,將圖形顯示。


3、SurfaceHolder
這里用到了1個(gè)類SurfaceHolder,可以把它當(dāng)做surface的控制器,用來操縱surface。處理它的Canvas上畫的效果和動(dòng)畫,控制表面,大小,像素等。
幾個(gè)需要注意的方法:
(1)、abstract void addCallback(SurfaceHolder.Callback callback);
// 給SurfaceView當(dāng)前的持有者1個(gè)回調(diào)對(duì)象。
(2)、abstract Canvas lockCanvas();
// 鎖定畫布,1般在鎖定后就能夠通過其返回的畫布對(duì)象Canvas,在其上面畫圖等操作了。
(3)、abstract Canvas lockCanvas(Rect dirty);
// 鎖定畫布的某個(gè)區(qū)域進(jìn)行畫圖等..由于畫完圖后,會(huì)調(diào)用下面的unlockCanvasAndPost來改變顯示內(nèi)容。
// 相對(duì)部份內(nèi)存要求比較高的游戲來講,可以不用重畫dirty外的其它區(qū)域的像素,可以提高速度。
(4)、abstract void unlockCanvasAndPost(Canvas canvas);
// 結(jié)束鎖定畫圖,并提交改變。
4、實(shí)例

這里的例籽實(shí)現(xiàn)了1個(gè)矩形和1個(gè)計(jì)時(shí)器

package xl.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ViewTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
    //視圖內(nèi)部類
    class MyView extends SurfaceView implements SurfaceHolder.Callback
    {
        private SurfaceHolder holder;
        private MyThread myThread;
        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            holder = this.getHolder();
            holder.addCallback(this);
            myThread = new MyThread(holder);//創(chuàng)建1個(gè)繪圖線程
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            myThread.isRun = true;
            myThread.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            myThread.isRun = false;
        }
        
    }
    //線程內(nèi)部類
    class MyThread extends Thread
    {
        private SurfaceHolder holder;
        public boolean isRun ;
        public  MyThread(SurfaceHolder holder)
        {
            this.holder =holder;
            isRun = true;
        }
        @Override
        public void run()
        {
            int count = 0;
            while(isRun)
            {
                Canvas c = null;
                try
                {
                    synchronized (holder)
                    {
                        c = holder.lockCanvas();//鎖定畫布,1般在鎖定后就能夠通過其返回的畫布對(duì)象Canvas,在其上面畫圖等操作了。
                        c.drawColor(Color.BLACK);//設(shè)置畫布背景色彩
                        Paint p = new Paint(); //創(chuàng)建畫筆
                        p.setColor(Color.WHITE);
                        Rect r = new Rect(100, 50, 300, 250);
                        c.drawRect(r, p);
                        c.drawText("這是第"+(count++)+"秒", 100, 310, p);
                        Thread.sleep(1000);//睡眠時(shí)間為1秒
                    }
                }
                catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                finally
                {
                    if(c!= null)
                    {
                        holder.unlockCanvasAndPost(c);//結(jié)束鎖定畫圖,并提交改變。

                    }
                }
            }
        }
    }
}

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国内精品视频在线播放 | 欧美在线综合视频 | 在线看的毛片 | 久久99精品久久久久久 | 欧美aa在线观看 | 亚洲一区在线免费观看 | 精品久久久久一区二区国产 | 亚洲国产免费 | 国产综合亚洲精品一区二 | 欧美性受 | 日韩3区 | 在线中文字幕av | av二三区 | 黑人中文字幕一区二区三区 | 中文字幕av在线播放 | 成人黄色一级毛片 | 日韩精品二区 | 91视频一区二区 | 国产成人影院 | 国产成人av一区二区三区 | av一区二区在线播放 | 综合色区| 亚洲欧洲成人精品av97 | 国产伦精品一区二区三 | 一级黄视频 | 欧美一区二区三区在线 | 国产视频色 | 综合久久亚洲 | 亚洲色图第一页 | 日韩精品视频在线播放 | 日韩a级毛片免费看 | 日韩一区免费 | 国产99视频精品免视看9 | 黄色片视频免费 | 久久精品国产v日韩v亚洲 | 国产精品三级 | 国产精品综合 | 久草一区 | 久久久精品亚洲 | 国产美女久久 | 99re视频|