2012-06-01 19 views
13

मैं एक बहुभाषी पाठ अनुच्छेद की चौड़ाई की गणना करने की कोशिश कर रहा हूं। मेरे ज्ञान के लिए, एंड्रॉइड में ऐसा करने वाला एकमात्र वर्ग स्टेटिकलाउट (या डायनेमिकलाइट) वर्ग है। इस कक्षा का उपयोग करते समय मुझे अपने टेक्स्ट स्निपेट की उचित लंबाई नहीं मिलती है, बल्कि मापा गया आयाम कभी-कभी छोटे आकार के होते हैं और कभी-कभी पाठ आकार के आधार पर अधिक होते हैं।एक बहु लाइन स्ट्रिंग की चौड़ाई को विश्वसनीय रूप से निर्धारित करने के लिए कैसे?

तो मैं मूल रूप से एक मल्टीलाइन टेक्स्ट स्ट्रिंग की चौड़ाई को मापने के लिए एक तरीका ढूंढ रहा हूं।

निम्न छवि दिखाती है कि मापित चौड़ाई विभिन्न पाठ आकारों में वास्तविक पाठ लंबाई से कैसे अलग हो जाती है। Diverging text and measure

स्क्रीनशॉट एक कस्टम दृश्य में निम्न कोड चल बनाया जाता है:

@Override 
protected void onDraw(Canvas canvas) { 
    for(int i = 0; i < 15; i++) { 
    int startSize = 10; 
    int curSize = i + startSize; 
    paint.setTextSize(curSize); 
    String text = i + startSize + " - " + TEXT_SNIPPET; 
    layout = new StaticLayout(text, 
           paint, 
           Integer.MAX_VALUE, 
           Alignment.ALIGN_NORMAL, 
           1.0f, 
           0.0f, 
           true); 

    float top = STEP_DISTANCE * i; 
    float measuredWidth = layout.getLineMax(0); 
    canvas.drawRect(0, top, measuredWidth, top + curSize, bgPaint); 
    canvas.drawText(text, 0, STEP_DISTANCE * i + curSize, paint); 
    } 
} 
+2

क्या आपने 'Paint.measureText()' की कोशिश की है? – neevek

+0

http://stackoverflow.com/questions/7549182/android-paint-measuretext-vs-gettextbounds – nullpotent

+0

@ नीवेक यह एक ही परिणाम उत्पन्न करता है लेकिन शीर्ष पर यह बहु रेखा पाठ को मापने में सक्षम नहीं है। – Moritz

उत्तर

1

आप पाठ प्राप्त सीमा का उपयोग कर की कोशिश कर सकते।

private int calculateWidthFromFontSize(String testString, int currentSize) 
{ 
    Rect bounds = new Rect(); 
    Paint paint = new Paint(); 
    paint.setTextSize(currentSize); 
    paint.getTextBounds(testString, 0, testString.length(), bounds); 

    return (int) Math.ceil(bounds.width()); 
} 

private int calculateHeightFromFontSize(String testString, int currentSize) 
{ 
    Rect bounds = new Rect(); 
    Paint paint = new Paint(); 
    paint.setTextSize(currentSize); 
    paint.getTextBounds(testString, 0, testString.length(), bounds); 

    return (int) Math.ceil(bounds.height()); 
} 
1

मेरे पास एक समान समस्या थी जहां मापित पाठ चौड़ाई थोड़ी देर से बंद हो गई थी, एक बार जब आप पेंट में टाइपफ़ेस सेट करते हैं तो यह दूर हो जाएगा। तो इससे पहले कि आप StaticLayout में paint चीज़ का इस्तेमाल सिर्फ टाइपफ़ेस सेट:

textPaint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL)) 

या जो कुछ भी टाइपफेस आप चाहते हैं।

0

यहां एक तरीका है जहां आप किसी भी पाठ की लंबाई की बहुमुखी चौड़ाई प्राप्त कर सकते हैं। usableButtonWidth एक दी गई टेक्स्ट स्पेस हो सकती है। मैंने usableButtonWidth = बटन की चौड़ाई - पैडिंग का उपयोग किया। लेकिन किसी भी डिफ़ॉल्ट मूल्य का उपयोग किया जा सकता है।

स्टेटिकलाउट का उपयोग करके, आप usableButtonWidth के लिए टेक्स्ट की ऊंचाई प्राप्त कर सकते हैं। फिर मैं लूप में 1px द्वारा इसे कम करके उपलब्ध चौड़ाई को कम करने का प्रयास करता हूं। यदि ऊंचाई बढ़ जाती है तो हमें पता चला है कि पहले इस्तेमाल की गई चौड़ाई अधिकतम अनुमत थी।

इस मान को मल्टीलाइन टेक्स्ट की चौड़ाई के रूप में वापस करें।

private int getTextWidth(){ 
    if(this.getText().length() != 0){ 
     Paint textPaint = new Paint(); 
     Rect bounds = new Rect(); 
     textPaint.setTextSize(this.getTextSize()); 
     if(mTypeface != null){ 
      textPaint.setTypeface(mTypeface); 
     } 
     String buttonText = this.getText().toString(); 
     textPaint.getTextBounds(buttonText, 0, buttonText.length(), bounds); 

     if(bounds.width() > usableButtonWidth){ 
      TextPaint longTextPaint = new TextPaint(); 
      longTextPaint.setTextSize(this.getTextSize()); 
      if(mTypeface != null){ 
       longTextPaint.setTypeface(mTypeface); 
      } 
      StaticLayout staticLayout = new StaticLayout(this.getText(), longTextPaint, usableButtonWidth, 
        Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); 
      int textLineCount = (int)Math.ceil(staticLayout.getHeight()/bounds.height()); 
      int multiLineTextWidth = usableButtonWidth; 
      while ((int)Math.ceil(staticLayout.getHeight()/bounds.height()) == textLineCount && multiLineTextWidth > 1){ 
       multiLineTextWidth--; 
       staticLayout = new StaticLayout(this.getText(), longTextPaint, multiLineTextWidth, 
         Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); 
      } 
      return multiLineTextWidth+1; 
     } else { 
      return bounds.width(); 
     } 
    } else { 
     return 0; 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे