您的位置:首页 > Web前端 > HTML

将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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OpenGL Html Texture