According to the previous response, the problem was mainly caused by the android bug. Uncle Li thought it was a cocos2dx bug and asked me to submit the bug. So I continued my research.
In the previous example, the java-layer function will be called to create an image and then set it as a texture to the sprite. Here the code is as follows:
public static void createTextBitmapShadowStroke(String pString,final String pFontName, final int pFontSize, final float fontTintR,final float fontTintG, final float fontTintB, final int pAlignment,final int pWidth, final int pHeight, final boolean shadow,final float shadowDX, final float shadowDY, final float shadowBlur,final boolean stroke, final float strokeR, final float strokeG,final float strokeB, final float strokeSize) { … … final TextProperty textProperty = Cocos2dxBitmap.computeTextProperty(pString, pWidth, pHeight, paint);final int bitmapTotalHeight = (pHeight == 0 ? textProperty.mTotalHeight: pHeight);… … final Bitmap bitmap = Bitmap.createBitmap(textProperty.mMaxWidth+ (int) bitmapPaddingX, bitmapTotalHeight+ (int) bitmapPaddingY, Bitmap.Config.ARGB_8888);… … Cocos2dxBitmap.initNativeObject(bitmap);}
The crash occurred in Bitmap. createBitmap. The first and second parameters are 0, and the two parameters are obtained by textProperty. Let's look at this textProperty again. As follows:
Private static TextProperty computeTextProperty (final String pString, final int pWidth, final int pHeight, final Paint pPaint) {final FontMetricsInt fm = pPaint. getFontMetricsInt (); final int h = (int) Math. ceil (fm. bottom-fm. top); int maxContentWidth = 0; final String [] lines = Cocos2dxBitmap. splitString (pString, pWidth, pHeight, pPaint); if (pWidth! = 0) {maxContentWidth = pWidth;} else {/* Compute the max width. */int temp = 0; for (final String line: lines) {temp = (int) FloatMath. ceil (pPaint. measureText (line, 0, line. length (); // It is here !! If (temp> maxContentWidth) {maxContentWidth = temp ;}} return new TextProperty (maxContentWidth, h, lines );}
Pay attention to the content in the for loop. To understand it, if there are many words, the width of the texture must be the widest line in the many words. As for how to obtain the width of this row, we can see how big it is when we use ppainting. Therefore, there is a measure function that can calculate the length.
This paint and this measure are functions provided by android. If the width and height cannot be 0, an exception should be thrown, but android didn't do this, so he asked him to crash directly,So this must be an android bug !!!
However, cocos2dx should also be avoided during design. You can manually determine whether the value is 0.
But who will find this problem as boring as I do ....