将Html的tag标签使用OpenGL渲染到Texture中实例
2018-01-24 12:21
411 查看
/** * transform a Html tag String to Texture * * @param htmlString the html tag String like * "<p><i>italic </i><b>bold <i>italic+bold <u>italic+bold+un</u></i></b></p>" * @param textSize define text size * @param textureWidth the width of return Texture * if <= 0, then use measured width instead * @param textureHeight the height of return Texture * if <= 0, then use measured height instead * * @return */ public static Texture fromString(String htmlString, float textSize, float textureWidth, float textureHeight, int textColor,int gravity) { // create TextView, and use html String, text size, text font TextView tv = new TextView(YourApplication.getInstance()); tv.setGravity(gravity); tv.setText(Html.fromHtml(htmlString)); tv.setTextColor(textColor); Typeface typeface = Typeface.createFromAsset(getAssets(),"font.ttf"); tv.setTypeface(typeface); tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); tv.setSingleLine(false); tv.setMaxWidth((int) textureWidth); /*tv.setMaxWidth(tv.getTextSize()*tv.length() < textureWidth? (int) (tv.getTextSize() * tv.length()) : (textureWidth));*/ //tv.setLines(2); // measure the TextView size : width & height tv.measure(makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); TextPaint paint = tv.getPaint(); float v = paint.measureText(tv.getText().toString()); // after measured, get the real width & height /*int width = textureWidth <= 0 ? tv.getMeasuredWidth() : textureWidth; int height = textureHeight <= 0 ? tv.getMeasuredHeight() : textureHeight;*/ // make TextView layout, this will get the real TextView appearance // DANNY ADD : don't draw padding bottom if (tv.getLineCount() > 3){ tv.layout(0,0, (int) textureWidth, (int) (tv.getTextSize() * (tv.getLineCount()-2) + 15)); }else { tv.layout(0, 0, (int) v, (int) (tv.getTextSize() + 15)); } //tv.layout(0, 0, width, height); // get the Bitmap of processed TextView tv.buildDrawingCache(); final Bitmap bitmap = tv.getDrawingCache(); Texture tex = new Texture(bitmap.getWidth(), bitmap.getHeight(), Pixmap.Format.RGBA8888); glBindTexture(GLES20.GL_TEXTURE_2D, tex.getTextureObjectHandle()); /** * https://jira.englishtown.cn/secure/RapidBoard.jspa?rapidView=738&projectKey=SAA&view=detail&selectedIssue=SAA-601 * * in some devices, the font of Html tag looks so terrible */ // scale linearly when image smalled than texture GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR); // Scale up if the texture if smaller. GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); // OPTIONAL GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); // this texImage2D method must be called after glBindTexture, it needs to know which texture's ID texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap,0); // this method must be called, IMPORTANT! GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D); glBindTexture(GLES20.GL_TEXTURE_2D, 0); bitmap.recycle(); return tex; }
相关文章推荐
- Html 标签的使用小结(实例代码)
- js正则:匹配一个html的tagname的开始标签,主要是解决包含&lt;&gt;(正则的效率问题建议不使用)
- python正则表达式使用实例-替换字符串HTML标签
- PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
- 使用v-html解决Vue.js渲染过程中html标签不能被解析(html标签显示为字符串)
- 关于使用strip_tag过滤字符串中的html标签
- PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
- Html标签使用实例
- 使用v-html解决Vue.js渲染过程中html标签不能被解析(html标签显示为字符串)
- html语言的标签的使用
- asp.net控件开发技巧(1)使用HtmlTextWriter类规范输出标签
- asp.net控件开发技巧(1)使用HtmlTextWriter类规范输出标签
- 使用FreeMarker生成Html静态文件(实例)
- struts标签bean:cookie,bean:write,logic:page,logic:present,logic:iterate使用实例
- HTML自定义标签开发入门实例
- html label标签的使用教程
- html label标签的使用教程
- 使用 Struts 提供的 HTML 标签库
- 使用php 的缓冲区功能和apache的url重写功能实现静态html生成实例
- 使用 Struts 提供的 HTML 标签库