将Html的tag标签使用OpenGL渲染到Texture中实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxm317122667/article/details/79149316
/**
 * 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;
}

猜你喜欢

转载自blog.csdn.net/zxm317122667/article/details/79149316