jetpack Compose 使用 GLSurfaceView的例子

2023-10-12 18:28

本文主要是介绍jetpack Compose 使用 GLSurfaceView的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现了一个 compose 测试 GL纹理的例子;

页面中包含两个按钮, 点击按钮1 ,GL中新增纹理,
点击按钮2 ,GL中释放纹理

package com.example.gltestimport android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.ShortBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10const val TAG = "MainActivity"class MainActivity : ComponentActivity() {private val glView by lazy { MyGLView(this) }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {
//            var count by remember { mutableStateOf(0) }
//
//
//            var textureList by remember {
//                mutableStateOf(0)
//            }var scope = rememberCoroutineScope()Box {AndroidView(factory = { glView }, modifier = Modifier.fillMaxSize())Column {Button(onClick = {// 点击按钮1 - 在GL中新增一张图片纹理val bitmap = BitmapFactory.decodeResource(resources, com.example.gltest.R.drawable.a) // 替换为您的大图片资源glView.addTexture(bitmap)}) {Text("Add Texture")}Button(onClick = {// 点击按钮2 - 释放GL中一张图片纹理glView.releaseTexture()}) {Text("Release Texture")}TextureInfoView(glView)}}}}
}@Composable
fun TextureInfoView(glView: MyGLView) {val currentTextureId by remember(glView) { derivedStateOf { glView.textureList() } }Text(text = "生成的纹理礼拜: ")Text("Current Texture ID: ${currentTextureId.joinToString()}")
}class MyGLView(activity: MainActivity) : GLSurfaceView(activity) {private val renderer: MyRenderer = MyRenderer()init {setEGLContextClientVersion(2)setRenderer(renderer)}fun addTexture(bitmap: Bitmap) {queueEvent {renderer.addTexture(bitmap)bitmap.recycle() // 确保释放位图资源}}fun releaseTexture() {queueEvent {renderer.releaseTexture()}}fun textureList(): MutableList<Int> {return renderer.textureList}private inner class MyRenderer : Renderer {val textureList = mutableListOf<Int>()override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)}override fun onDrawFrame(gl: GL10) {GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)}override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {GLES20.glViewport(0, 0, width, height)}fun addTexture(bitmap: Bitmap) {val textureIds = IntArray(1)GLES20.glGenTextures(1, textureIds, 0)// 绑定纹理GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0])// 设置纹理参数GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR.toFloat())GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR.toFloat())// 加载位图数据到纹理GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)// 解绑纹理GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)// 现在 `textureIds[0]` 包含了加载的纹理的IDtextureList.add(textureIds[0])Log.e(TAG, "addTexture: ${textureIds[0]}")//画图片无效需要联调
//            drawImage(textureIds[0])}fun releaseTexture() {val textureId = textureList.removeLastOrNull() ?: returnLog.e(TAG, "releaseTexture: $textureId")// 在这里执行释放纹理的操作,可以使用OpenGL的APIval texturesToDelete = intArrayOf(textureId)GLES20.glDeleteTextures(1, texturesToDelete, 0)}}private fun drawImage(textureId:Int) {val vertices = floatArrayOf(-1.0f, 1.0f, 0.0f,  // 左上角-1.0f, -1.0f, 0.0f,  // 左下角1.0f, -1.0f, 0.0f,  // 右下角1.0f, 1.0f, 0.0f // 右上角)val textureCoordinates = floatArrayOf(0.0f, 0.0f,  // 左上角0.0f, 1.0f,  // 左下角1.0f, 1.0f,  // 右下角1.0f, 0.0f // 右上角)// Create an float array in opengles runtime (native) and put vertex data.// Create an float array in opengles runtime (native) and put vertex data.val mVertexBuffer = ByteBuffer.allocateDirect(vertices.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer()mVertexBuffer.put(vertices)mVertexBuffer.position(0)val indices = shortArrayOf(0, 1, 2, 0, 2, 3)// Create an float array in opengles runtime (native) and put texture data.// Create an float array in opengles runtime (native) and put texture data.val mTextureBuffer = ByteBuffer.allocateDirect(textureCoordinates.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer()mTextureBuffer.put(textureCoordinates)mTextureBuffer.position(0)val dlb = ByteBuffer.allocateDirect(indices.size * 2)dlb.order(ByteOrder.nativeOrder())val drawListBuffer = dlb.asShortBuffer()drawListBuffer.put(indices)drawListBuffer.position(0)val program = GLES20.glCreateProgram()// 使用顶点着色器和片段着色器进行绘制GLES20.glUseProgram(program)// 获取着色器中的属性和统一变量位置val positionHandle = GLES20.glGetAttribLocation(program, "aPosition")val textureCoordinateHandle = GLES20.glGetAttribLocation(program, "aTextureCoordinate")val mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix")val textureHandle = GLES20.glGetUniformLocation(program, "uTexture")// 启用属性数组GLES20.glEnableVertexAttribArray(positionHandle)GLES20.glEnableVertexAttribArray(textureCoordinateHandle)// 设置顶点坐标数据GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, mVertexBuffer)// 设置纹理坐标数据GLES20.glVertexAttribPointer(textureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 8, mTextureBuffer)// 设置纹理单元GLES20.glActiveTexture(GLES20.GL_TEXTURE0)GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)GLES20.glUniform1i(textureHandle, 0)// 设置模型视图投影矩阵// 设置模型视图投影矩阵val mvpMatrix = FloatArray(16)// 设置模型视图投影矩阵GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)// 绘制图形GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.size, GLES20.GL_UNSIGNED_SHORT, drawListBuffer)// 禁用属性数组GLES20.glDisableVertexAttribArray(positionHandle)GLES20.glDisableVertexAttribArray(textureCoordinateHandle)}}

这篇关于jetpack Compose 使用 GLSurfaceView的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/197632

相关文章

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

如何合理使用Spring的事务方式

《如何合理使用Spring的事务方式》:本文主要介绍如何合理使用Spring的事务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、底层构造1.1.事务管理器1.2.事务定义信息1.3.事务状态1.4.联系1.2、特点1.3、原理2. Sprin

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

PyQt5 QDate类的具体使用

《PyQt5QDate类的具体使用》QDate是PyQt5中处理日期的核心类,本文主要介绍了PyQt5QDate类的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录核心功能常用方法及代码示例​1. 创建日期对象​2. 获取日期信息​3. 日期计算与比较​4. 日

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs