`
老七的米店
  • 浏览: 48859 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Api Demo - .graphics(20)>>CompressedTextureActivity

阅读更多
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.graphics;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.ETC1Util;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;

import com.example.android.apis.R;

/**
 * Demonstrate how to use ETC1 format compressed textures.
 * This sample can be recompiled to use either resource-based
 * textures (compressed offline using the etc1tool), or
 * textures created on the fly by compressing images.
 *
 */
public class CompressedTextureActivity extends Activity {
    private final static String TAG = "CompressedTextureActivity";
    /**
     * Choose between creating a compressed texture on the fly or
     * loading a compressed texture from a resource.
     */
    private final static boolean TEST_CREATE_TEXTURE = false;
    /**
     * When creating a compressed texture on the fly, choose
     * whether or not to use the i/o stream APIs.
     */
    private final static boolean USE_STREAM_IO = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLView = new GLSurfaceView(this);
        mGLView.setEGLConfigChooser(false);
        StaticTriangleRenderer.TextureLoader loader;//纹理装载器
        if (TEST_CREATE_TEXTURE) {
            loader = new SyntheticCompressedTextureLoader();//运行时创建资源。
        } else {
            loader = new CompressedTextureLoader();//压缩从资源文件中获得的纹理
        }
        mGLView.setRenderer(new StaticTriangleRenderer(this, loader));//设置渲染
        setContentView(mGLView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    /**
     * Demonstrate how to load a compressed texture from an APK resource.
     *
     */
    private class CompressedTextureLoader implements StaticTriangleRenderer.TextureLoader {
        public void load(GL10 gl) {
            Log.w(TAG, "ETC1 texture support: " + ETC1Util.isETC1Supported());
            InputStream input = getResources().openRawResource(R.raw.androids);//从raw资源获取输入流
            try {
                ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                        GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, input);//通过输入流设置纹理
            } catch (IOException e) {
                Log.w(TAG, "Could not load texture: " + e);
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                    // ignore exception thrown from close.
                }
            }
        }
    }

    /**
     * Demonstrate how to create a compressed texture on the fly.
     */
    private class SyntheticCompressedTextureLoader implements StaticTriangleRenderer.TextureLoader {
        public void load(GL10 gl) {
            int width = 128;
            int height = 128;
            Buffer image = createImage(width, height);//创建纹理缓存
            /*
             * 将Buffer封装成ETC1Texture
             */
            ETC1Util.ETC1Texture etc1Texture = ETC1Util.compressTexture(image, width, height, 3, 3 * width);
            if (USE_STREAM_IO) {
                // Test the ETC1Util APIs for reading and writing compressed textures to I/O streams.
                try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ETC1Util.writeTexture(etc1Texture, bos);//将ETC1Texture写入输出流
                    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());//将输入流转换成Byte输入流
                    ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                            GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, bis);//通过输入流设置纹理
                } catch (IOException e) {
                    Log.w(TAG, "Could not load texture: " + e);
                }
            } else {
                ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                        GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, etc1Texture);
            }
        }

        private Buffer createImage(int width, int height) {//快速创建纹理
            int stride = 3 * width;
            ByteBuffer image = ByteBuffer.allocateDirect(height * stride)
                .order(ByteOrder.nativeOrder());

            // Fill with a pretty "munching squares" pattern:
            for (int t = 0; t < height; t++) {
                byte red = (byte)(255-2*t);
                byte green = (byte)(2*t);
                byte blue = 0;
                for (int x = 0; x < width; x++) {
                    int y = x ^ t;
                    image.position(stride*y+x*3);
                    image.put(red);
                    image.put(green);
                    image.put(blue);
                }
            }
            image.position(0);
            return image;
        }
    }
    private GLSurfaceView mGLView;
}

 

分享到:
评论

相关推荐

    demo-0.0.1-SNAPSHOT.jar

    返回json数组的科技头条的api数据jar包

    spring4 mvc + jpa demo

    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;jdk.version&gt;1.7&lt;/jdk.version&gt; &lt;spring.version&gt;4.0.1.RELEASE&lt;/spring.version&gt; &lt;spring-data-jpa.version&gt;1.6.2.RELEASE&lt;/spring-...

    ProCamera2D

    - Camera Window &gt;&gt;DEMO - Cinematics &gt;&gt;DEMO - Content Fitter &gt;&gt;DEMO - Forward Focus &gt;&gt;DEMO - Geometry Boundaries - Limit Distance &gt;&gt;DEMO - Limit Speed &gt;&gt;DEMO - Numeric Boundaries &gt;&gt;DEMO - Pan ...

    Face-api.js静态页面版Demo

    JavaScript人脸识别库Face-api.js的示例,无需安装nodejs,iis本地直接看效果。注意调用摄像头不能用IP访问,只能localhost,远程预览需要HTTPS;iis无扩展名文件若出现404,需在mime类型中添加扩展名【.】类型...

    Api-dawn-api-demo.zip

    Api-dawn-api-demo.zip,道恩API解调API,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到对象中,api简化了编程。

    JS人脸识别(face-api)

    "Face-Detection-JavaScript-demo"和"Face-Detection-JavaScript-master"可能是两个示例项目,它们演示了如何在实际项目中应用face-api.js。通过阅读源代码和运行这些示例,你将更好地理解如何整合face-api.js到你的...

    海康威视web开发控件和demo

    本资源包含海康威视的Web开发控件及其示例(demo),旨在帮助开发者快速集成并实现监控画面在网页上的展示和操作。 1. **海康IE控件**:海康威视的Web开发控件主要针对Internet Explorer浏览器设计,用于在网页上...

    可keil编译sam3x_ek_bertos_http_demo

    根据atmel官方例程sam3x_ek_bertos_http_demo,自己...在keil-&gt;Project-&gt;Manage-&gt;Components,Environment,Books...-&gt;Folders/Externsions的 Use GCC的GNU-Tool Folder中指定arm-2012.03-56-arm-none-eabi.exe的安装目录

    Api-demo.zip

    Api-demo.zip,API平台的演示应用程序框架API平台演示,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到对象中,api简化了编程。

    autoconfig-demo.zip

    &lt;description&gt;Demo project for Spring Boot&lt;/description&gt; &lt;properties&gt; &lt;java.version&gt;1.8&lt;/java.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot...

    android-serialport-api 串口demo.zip

    这是很据android-serialport-api 自己简化的一个demo ,可以使用。原来android-serial-api的程序很多人反映都不能使用,所以自己写了这个,只有一个activity,可以做为你的学习参考。

    jquery日历控件

    &lt;div class="demo-description"&gt; &lt;p&gt;The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a ...

    service-api-demo.rar

    此工程集成了nexus私服,配合我的“nexus搭建和基于spring boot2.x的配置,centos7"博客,可以使用spring boot集成nexus

    mybatis-3.2.7.jar-lib-source code API settings Timeout MyBatisDemo 常用例子

    mybatis-3.2.7.jar source code API configuration.xml settings defaultStatementTimeout 的设置 MyBatisDemo 常用例子 使用3种方法,编写mapper,操作数据库

    API-Demo.rar_DEMO_epon_olt

    标题中的"API-Demo.rar_DEMO_epon_olt"表明这是一个关于EPON(以太网无源光网络)OLT(光线路终端)的API演示示例。这个压缩包可能包含了用于展示如何与PAS5001N OLT设备进行交互的API代码或文档。 描述中的"PMC-...

    open-api-sdk-2.0和jackson,京东宙斯开发jar包,宙斯API包

    open-api-sdk-2.0和jackson,京东宙斯开发jar包,宙斯API包,本人开发使用,open-api-sdk-2.0.jar,jackson-core-asl-1.9.8.jar,jackson-mapper-asl-1.9.8.jar,需要朋友可自行下载

    API-Demo.zip_DEMO_vb.net

    在压缩包内的文件"API-Demo-v1.0.6",很可能是一个包含源代码、编译后的可执行文件、库文件、或者相关配置文件的文件夹。这个版本号"v1.0.6"表示这是该API-Demo的第六个版本,说明项目已经经过了多次迭代和改进。 ...

    API-Demo.zip

    《支付宝小程序API-Demo深度解析》 支付宝小程序作为阿里巴巴生态系统的一部分,为开发者提供了一种便捷的构建轻量级应用的方式。API-Demo.zip文件包含了支付宝小程序的官方示例代码,旨在帮助开发者更好地理解和...

    MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar

    MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'error_view' of bean class [com.demo.controller.action.AuthorAction]: Bean property 'error_view' is not ...

Global site tag (gtag.js) - Google Analytics