Android 上的 定位字元(tab) —— 一個神奇的字元 (二)

來源:互聯網
上載者:User

標籤: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。

     不過誰會像我這麼無聊恰好發現這個問題呢。。。。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.