Android使用Libgdx渲染Html标签
来源:互联网 发布:kingkong1024灯光编程 编辑:程序博客网 时间:2024/06/05 17:38
在使用Libgdx的过程中,有时候会遇到这样的需要, 后端返回给我们的是html tag,比如如下:
<p><i>italic </i><b>bold <i>italic+bold <u>italic+bold+un</u></i></b></p>
对于以上Html tag,如果使用android自带的控件TextView可以通过setText(Html.from(“”))的方式直接设置显示内容。效果如下:
但是在Libgdx中并没有提供相应的控件实现这种效果。所以需要转换一下思路, 具体的实现思路就是先将Html文本内容设置到TextView,然后对TextView截图获取Bitmap对象,然后将Bitmap对象通过OpenGL转化为Texture对象,有了Texture对象就可以在Libgdx中渲染了。
具体实现如下:
package com.ef.smallstar.libgdx.util;import android.graphics.Bitmap;import android.opengl.GLES20;import android.opengl.GLUtils;import android.text.Html;import android.view.Gravity;import android.view.View;import android.widget.TextView;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.Pixmap;import com.badlogic.gdx.graphics.Texture;import com.ef.smallstar.EFApplication;/** * Created by Danny 姜 on 17/8/10. */public class GdxHtmlUtils { public static Texture fromString(String string, int textSize) { return fromString(string, textSize, 0, 0); } public static Texture fromString(String string, int textSize, float textureWidth, float textureHeight) { return fromString(string, textSize, textureWidth, textureHeight, 0, 0, 0, 0); } public static Texture fromString(String string, int textSize, float textureWidth, float textureHeight, float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) { TextView tv = new TextView(EFApplication.getInstance()); tv.setGravity(Gravity.CENTER); tv.setText(Html.fromHtml(string)); tv.setTextSize(textSize); tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); float width = textureWidth <= 0 ? tv.getMeasuredWidth() : textureWidth; float height = textureHeight <= 0 ? tv.getMeasuredHeight() : textureHeight; tv.layout(((int) paddingLeft), ((int) (0 + paddingTop)), (int) (width - paddingRight), ((int) (height - paddingBottom))); tv.buildDrawingCache(); final Bitmap bitmap = tv.getDrawingCache(); Texture tex = new Texture(bitmap.getWidth(), bitmap.getHeight(), Pixmap.Format.RGBA8888); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex.getTextureObjectHandle()); GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); bitmap.recycle(); return tex; }}
阅读全文
6 0
- Android使用Libgdx渲染Html标签
- Android上使用libgdx
- 使用v-html解决Vue.js渲染过程中html标签不能被解析(html标签显示为字符串)
- 使用v-html解决Vue.js渲染过程中html标签不能被解析(html标签显示为字符串)
- android WebView渲染html
- Android string.xml中使用html标签
- Android string.xml中使用html标签
- Android string.xml中使用html标签
- Android string.xml中使用html标签
- Android游戏引擎libgdx使用教程1:搭建libgdx环境
- vue 动态渲染 html标签解决方案 v-html
- Android游戏框架Libgdx使用入门
- Android游戏框架Libgdx使用入门
- Android游戏框架Libgdx使用入门
- Android游戏开发框架libgdx的使用
- Android游戏开发框架libgdx的使用
- Android游戏引擎Libgdx使用入门
- Android游戏框架Libgdx使用入门
- 7-3 判断上三角矩阵
- 如何让PopupWindow 显示在屏幕的中央
- 【PAT】【Advanced Level】1071. Speech Patterns (25)
- LeetCode 16. 3Sum Closest--寻找数组中的三个元素和,该和与给定的值的差最小,输出这个和(三个元素可以不连续)
- CodeForces389D_Fox and Minimal path_二进制拆分
- Android使用Libgdx渲染Html标签
- android studio 导入eclipse项目 兼容性问题
- CDH5.11.0安装过程报错:Unable to verify database connection
- hue 服务占用hive service2 内存,需定时清除
- 笔记-js实现彩虹列表
- 【FFmpeg学习笔记005】 libx264和FFmpeg不同方式(YUV编码为H.264)
- 全链路数据监控
- BZOJ 1051 [HAOI2006]受欢迎的牛 tarjan
- Java IO流原理之常用字节流和字符流详解以及Buffered高效的原理