智汇工业-智慧工业、智能制造及工业智能、工业互联门户网站,专业的工业“互联网+”传媒

Android應(yīng)用之SurfaceView的雙緩沖使用

來(lái)源:網(wǎng)絡(luò)

點(diǎn)擊:2267

A+ A-

所屬頻道:新聞中心

關(guān)鍵詞: Android,SurfaceView,雙緩沖

        這次介紹SurfaceView的雙緩沖使用。雙緩沖是為了防止動(dòng)畫閃爍而實(shí)現(xiàn)的一種多線程應(yīng)用,基于SurfaceView的雙緩沖實(shí)現(xiàn)很簡(jiǎn)單,開一條線程并在其中繪圖即可。本文介紹基于SurfaceView的雙緩沖實(shí)現(xiàn),以及介紹類似的更高效的實(shí)現(xiàn)方法。

        本文程序運(yùn)行截圖如下,左邊是開單個(gè)線程讀取并繪圖,右邊是開兩個(gè)線程,一個(gè)專門讀取圖片,一個(gè)專門繪圖:

     

        對(duì)比一下,右邊動(dòng)畫的幀速明顯比左邊的快,左右兩者都沒使用Thread.sleep()。為什么要開兩個(gè)線程一個(gè)讀一個(gè)畫,而不去開兩個(gè)線程像左邊那樣都“邊讀邊畫”呢?因?yàn)镾urfaceView每次繪圖都會(huì)鎖定Canvas,也就是說(shuō)同一片區(qū)域這次沒畫完下次就不能畫,因此要提高雙緩沖的效率,就得開一條線程專門畫圖,開另外一條線程做預(yù)處理的工作。

    main.xml的源碼:

    view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" android:layout_height="fill_parent" 
        android:orientation="vertical"> 
     
        <LinearLayout android:id="@+id/LinearLayout01" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"> 
            <Button android:id="@+id/Button01" android:layout_width="wrap_content" 
                android:layout_height="wrap_content" android:text="單個(gè)獨(dú)立線程"></Button> 
            <Button android:id="@+id/Button02" android:layout_width="wrap_content" 
                android:layout_height="wrap_content" android:text="兩個(gè)獨(dú)立線程"></Button> 
        </LinearLayout> 
        <SurfaceView android:id="@+id/SurfaceView01" 
            android:layout_width="fill_parent" android:layout_height="fill_parent"></SurfaceView> 
    </LinearLayout> 
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent" android:layout_height="fill_parent"
     android:orientation="vertical">

     <LinearLayout android:id="@+id/LinearLayout01"
      android:layout_width="wrap_content" android:layout_height="wrap_content">
      <Button android:id="@+id/Button01" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:text="單個(gè)獨(dú)立線程"></Button>
      <Button android:id="@+id/Button02" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:text="兩個(gè)獨(dú)立線程"></Button>
     </LinearLayout>
     <SurfaceView android:id="@+id/SurfaceView01"
      android:layout_width="fill_parent" android:layout_height="fill_parent"></SurfaceView>
    </LinearLayout>
     

    本文程序的源碼:

     view plaincopy to clipboardprint?
    package com.testSurfaceView;  
     
    import java.lang.reflect.Field;  
    import java.util.ArrayList;  
    import android.app.Activity;  
    import android.graphics.Bitmap;  
    import android.graphics.BitmapFactory;  
    import android.graphics.Canvas;  
    import android.graphics.Paint;  
    import android.graphics.Rect;  
    import android.os.Bundle;  
    import android.util.Log;  
    import android.view.SurfaceHolder;  
    import android.view.SurfaceView;  
    import android.view.View;  
    import android.widget.Button;  
     
    public class testSurfaceView extends Activity {  
        /** Called when the activity is first created. */ 
        Button btnSingleThread, btnDoubleThread;  
        SurfaceView sfv;  
        SurfaceHolder sfh;  
        ArrayList<Integer> imgList = new ArrayList<Integer>();  
        int imgWidth, imgHeight;  
        Bitmap bitmap;//獨(dú)立線程讀取,獨(dú)立線程繪圖  
     
        @Override 
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
     
            btnSingleThread = (Button) this.findViewById(R.id.Button01);  
            btnDoubleThread = (Button) this.findViewById(R.id.Button02);  
            btnSingleThread.setOnClickListener(new ClickEvent());  
            btnDoubleThread.setOnClickListener(new ClickEvent());  
            sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);  
            sfh = sfv.getHolder();  
            sfh.addCallback(new MyCallBack());// 自動(dòng)運(yùn)行surfaceCreated以及surfaceChanged  
        }  
     
        class ClickEvent implements View.OnClickListener {  
     
            @Override 
            public void onClick(View v) {  
     
                if (v == btnSingleThread) {  
                    new Load_DrawImage(0, 0).start();//開一條線程讀取并繪圖  
                } else if (v == btnDoubleThread) {  
                    new LoadImage().start();//開一條線程讀取  
                    new DrawImage(imgWidth + 10, 0).start();//開一條線程繪圖  
                }  
     
            }  
     
        }  
     
        class MyCallBack implements SurfaceHolder.Callback {  
     
            @Override 
            public void surfaceChanged(SurfaceHolder holder, int format, int width,  
                    int height) {  
                Log.i("Surface:", "Change");  
     
            }  
     
            @Override 
            public void surfaceCreated(SurfaceHolder holder) {  
                Log.i("Surface:", "Create");   
      


                // 用反射機(jī)制來(lái)獲取資源中的圖片ID和尺寸  
                Field[] fields = R.drawable.class.getDeclaredFields();  
                for (Field field : fields) {  
                    if (!"icon".equals(field.getName()))// 除了icon之外的圖片  
                    {  
                        int index = 0;  
                        try {  
                            index = field.getInt(R.drawable.class);  
                        } catch (IllegalArgumentException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        } catch (IllegalAccessException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                        // 保存圖片ID  
                        imgList.add(index);  
                    }  
                }  
                // 取得圖像大小  
                Bitmap bmImg = BitmapFactory.decodeResource(getResources(),  
                        imgList.get(0));  
                imgWidth = bmImg.getWidth();  
                imgHeight = bmImg.getHeight();  
            }  
     
            @Override 
            public void surfaceDestroyed(SurfaceHolder holder) {  
                Log.i("Surface:", "Destroy");  
     
            }  
     
        }  
     
        /* 
         * 讀取并顯示圖片的線程 
         */ 
        class Load_DrawImage extends Thread {  
            int x, y;  
            int imgIndex = 0;  
     
            public Load_DrawImage(int x, int y) {  
                this.x = x;  
                this.y = y;  
            }  
     
            public void run() {  
                while (true) {  
                    Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x  
                            + imgWidth, this.y + imgHeight));  
                    Bitmap bmImg = BitmapFactory.decodeResource(getResources(),  
                            imgList.get(imgIndex));  
                    c.drawBitmap(bmImg, this.x, this.y, new Paint());  
                    imgIndex++;  
                    if (imgIndex == imgList.size())  
                        imgIndex = 0;  
     
                    sfh.unlockCanvasAndPost(c);// 更新屏幕顯示內(nèi)容  
                }  
            }  
        };  
     
        /* 
         * 只負(fù)責(zé)繪圖的線程 
         */ 
        class DrawImage extends Thread {  
            int x, y;  
     
            public DrawImage(int x, int y) {  
                this.x = x;  
                this.y = y;  
            }  
     
            public void run() {  
                while (true) {  
                    if (bitmap != null) {//如果圖像有效  
                        Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x  
                                + imgWidth, this.y + imgHeight));  
     
                        c.drawBitmap(bitmap, this.x, this.y, new Paint());  
     
                        sfh.unlockCanvasAndPost(c);// 更新屏幕顯示內(nèi)容  
                    }  
                }  
            }  
        };  
     
        /* 
         * 只負(fù)責(zé)讀取圖片的線程 
         */ 
        class LoadImage extends Thread {  
            int imgIndex = 0;  
     
            public void run() {  
                while (true) {  
                    bitmap = BitmapFactory.decodeResource(getResources(),  
                            imgList.get(imgIndex));  
                    imgIndex++;  
                    if (imgIndex == imgList.size())//如果到盡頭則重新讀取  
                        imgIndex = 0;  
                }  
            }  
        };  

     

     

    (審核編輯: 智匯小新)

    聲明:除特別說(shuō)明之外,新聞內(nèi)容及圖片均來(lái)自網(wǎng)絡(luò)及各大主流媒體。版權(quán)歸原作者所有。如認(rèn)為內(nèi)容侵權(quán),請(qǐng)聯(lián)系我們刪除。

    主站蜘蛛池模板: 乌鲁木齐万疆通管道设备有限公司 销售热线;13565955557-新疆 乌鲁木齐 万疆通 管道设备 波纹补偿器 膨胀节 金属软管 伸缩器 管件 阀门 维修 | 思为网络_成都百度优化快照排名-成都网站建设优化_成都网页设计_成都SEO公司 | 挖掘机|小型挖掘机|挖掘机抓木机|轮式挖掘机|宝鼎挖掘机-宝鼎液压机械公司厂家直销 | 噪声治理_噪音治理公司「杭州创雅环境科技」 | 山东万通液压股份有限公司-自卸车专用油缸,能源采掘设备油缸,机械装备用油缸,油气弹簧,工程机械油缸,液压元件 | 家用座椅电梯 斜挂升降平台 无障碍升降机 残疾人升降机的生产厂家超易达机械 | 河南反渗透设备,河南纯净水设备,河南软化水设备,郑州EDI超纯水设备,郑州水处理设备厂家_河南江宇环保科技有限公司 | 注塑模具厂,注塑模具加工,塑胶模具加工-东莞世邦塑胶官网 | 双效-多效-三效-废水-污水蒸发器_离心喷雾-污泥干燥机_蒸发浓缩器【天辰环保 】 | 微机继电保护测试仪,单相继电保护测试仪,三相继电保护测试仪,六相继电保护测试仪,介质损耗测试仪,氧化锌避雷器测试仪,无线核相仪-扬州豪泰电力科技有限公司 | 眉山净源居环保科技有限公司,眉山除甲醛公司,眉山甲醛治理,眉山保洁服务,眉山家政保洁,眉山家电维修 - 眉山净源居环保科技有限公司,眉山除甲醛公司,眉山甲醛治理,眉山保洁服务,眉山家政保洁,眉山家电维修 | 襄阳燃烧器厂家-低氮燃烧器价格-河北五通道燃烧器就找襄阳市胜合燃力设备有限公司一站式服务 | 天象文仪办公家具,25年一站式配齐经验厂家-办公家具官网 | 河北新鑫矿冶设备有限公司-河北新鑫矿冶设备有限公司 | 苏州交通设施_道路划线_停车场划线_厂区划线_环氧地坪厂家-推荐【飞扬市政交通设施公司】专注交通设施8年! | 学校洗碗机-郑州洗碗机厂家-商用洗碗机-郑州旭申环保科技有限公司 | 合金锤头_破碎机锤头_耐磨锤头_巩义市东辰实业有限公司 | 托辊|滚筒|聚氨酯托辊|缓冲托辊|尼龙托琨|衡水良龙输送机械有限公司 | 透明膜包装机_三维包装机_上海拓懿机械有限公司 | 上海云屹国际快递-承接化工品_液体_粉末_化妆品_食品_锂电池等国际快递业务 | 木材粉碎机-树枝秸秆粉碎机价格-双轴金属撕碎机生产线-金禾机械厂家 | 柯赛德斯-加美润滑油-专业做汽车工业润滑油品牌加盟代理 | 拼装式电磁屏蔽室厂家,屏蔽机柜生产厂家,电波暗室制造商,屏蔽配件-常州麦思恩屏蔽机柜生产厂家 | 木工圆锯片,进口锯片厂家,合金锯片生产厂家,木工合金锯片,BAK(百恪)刀具有限公司 | 油管家,货车油管家,工程车油管家-淄博畅行电子科技有限公司 | 水硬度在线分析仪-氟离子|悬浮固体浓度分析仪-ldo分析仪-上海植茂 | 削片机|木材破碎机|木材粉碎机|模板破碎机|双轴撕碎机_郑州木工机械制造厂 | 江苏宇力医疗器械有限公司 | 淄博润裕机械设备有限公司-搅拌器,搅拌桨叶,反应釜,机械密封,化工搅拌 | 频闪仪,便携式频闪仪厂家_灯管,频闪仪之父-杭州品拓电子技术有限公司 | 上海建发物资有限公司 | 铸造厂_铸造厂家_硅溶胶熔模铸造-盐城市春秋精密机械有限公司 | 紫外线光疗仪|白癜风光疗仪|牛皮癣治疗仪|308纳米led|SIGMA|上海希格玛高技术有限公司 | 深圳蓝枫印刷_画册印刷_彩页印刷_宣传册印刷_包装盒印刷_彩盒印刷厂_不干胶印刷厂 | 无锡防火门|无锡放火卷帘门|无锡市防火卷帘门厂有限公司 | 潍坊特钢集团有限公司| 机械设备回收_二手机器回收_设备拆除回收_广州益美机械设备回收公司 | 山东亮化工程_亮化公司_亮化资质-山东星汇照明工程有限公司 | 铜陵爱家装饰有限公司官网 | 九江江菱电梯有限公司| 西安木包装箱出口托盘定做价格-抽真空实木包装箱免熏蒸木箱多层板木箱哪家好-模压托盘及白松原木-西安宇森木业 |