標籤:android style java color strong width
接到上回的說,主要是上回那個問題,我覺得是android的bug,黎叔覺得是cocos2dx的bug,叫我去提交bug。所以我又繼續研究了下。
上回說到會調用java層的函數去建立一個image,然後作為紋理設定給精靈,在那個地方crash了,此處代碼如下:
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);} 崩潰的地方是在 Bitmap.createBitmap那裡,第一個參數和第二個參數為0了,那2個參數是由textProperty擷取的。再看這個textProperty。如下:
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())); //就在這裡!!if (temp > maxContentWidth) {maxContentWidth = temp;}}}return new TextProperty(maxContentWidth, h, lines);} 注意那個for迴圈裡面的東西,理解起來就是,假設有很多字,那麼這個紋理的寬度,肯定是這很多字裡面最寬的一行。至於這一行的寬度怎麼取,就是靠pPaint去繪製的時候,看畫出來是多大了,所以有一個measure的函數,可以去計算這個長度。
這個paint和這個measure都是android提供給我們的函數。如果這個寬度和高度不能為0,應該拋一個異常出來,但是android並沒這麼做,他就讓他直接crash了,所以這一定是android的bug!!!
不過cocos2dx在設計的時候應該也是可以避免的,可以手動判斷一下這個值是否為0。
不過誰會像我這麼無聊恰好發現這個問題呢。。。。