2015-04-16 7 views
16

में बेजियर वक्र के बाहर लपेटें पाठ मैं एंड्रॉइड में वक्र के बाहर बेजियर वक्र के आकार में टेक्स्ट लपेटना चाहता हूं।एंड्रॉइड

मैं क्या प्रयास किया है:

Path path = new Path(); 
path.addCircle(x, y, radius, Path.Direction.CW); 
myCanvas.drawTextOnPath(myText, path, offset, 0, myPaint); 

मैं क्या प्राप्त करने के लिए कोशिश कर रहा हूँ:

लेकिन इस कोड curve..I पर पाठ ड्रॉ वक्र पर पाठ लिखने के लिए नहीं करना चाहती। मैं वक्र के अनुसार पाठ लपेटना चाहता हूं और इसे अगली पंक्ति पर लिखना चाहता हूं।

इसे स्पष्ट रूप से समझने के लिए कृपया baconforme.com देखें .. मैं वेबब्रोसर का उपयोग किए बिना एंड्रॉइड में व्यवहार की तरह इस jquery को बनाना चाहता हूं।

और इस लिंक On Android how do I wrapping text inside in a bezier path

प्रश्न देखा:

  1. यह इस लक्ष्य को हासिल करना संभव है?
  2. यदि हां, तो कृपया मुझे मार्गदर्शन करें।

उत्तर

9

मैंने एक मूल दृश्य लागू किया है जो आप जो करने की कोशिश कर रहे हैं वह करता है। यहां दिया गया विचार आपके द्वारा अनुरोधित पथ से बिटमैप बनाना है। पथ के बाहर प्रत्येक पिक्सेल में 0 मान होगा, और पथ के अंदर प्रत्येक पिक्सेल के पास कुछ अन्य मूल्य होगा।

इसके साथ, आप जानते हैं कि पॉलीगॉन के भीतर कोई बिंदु कब है या नहीं। अब हमें यह निर्धारित करने की आवश्यकता है कि हम टेक्स्ट कहां खींचते हैं।

मैं उत्पन्न बिटमैप को घुमाने के द्वारा आयताकारों की एक सूची उत्पन्न करूंगा। प्रत्येक आयताकार बहुभुज के अंदर शुरू होने और समाप्त होने वाली पंक्तियों को परिभाषित करेगा।

प्रत्येक आयताकार के साथ, जब तक आयताकार अधिक टेक्स्ट को समायोजित नहीं कर सकता तब तक मैं पाठ को पॉप्युलेट करना शुरू कर देता हूं, इस मामले में मैं अगले आयत में जाता हूं। एक बार आयताकार नहीं होने के बाद, या मैं पाठ से बाहर हूं, मैं ड्राइंग बंद कर देता हूं।

इस कार्यान्वयन में, मैंने फ़ॉन्ट आकार, टेक्स्ट रंग और रैपिंग मोड जैसे कुछ अनुकूलन जोड़े।

यहाँ

यह है:

PolygonWrapView.java

public class PolygonWrapView extends View 
{ 
    public enum WrapMode 
    { 
     Letters, 
     Words 
    } 

    private Path mPath; 
    private String mText; 
    private float mFontSize; 
    private int mTextColor; 

    private Paint mPaint; 
    private Bitmap mPathMap; 

    private WrapMode mWrapMode = WrapMode.Words; 

    public PolygonWrapView(Context context) 
    { 
     super(context); 
     init(); 
    } 

    public PolygonWrapView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(); 
    } 

    public PolygonWrapView(Context context, AttributeSet attrs, int defStyleAttr) 
    { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private void init() 
    { 
     mPaint = new Paint(); 
     mFontSize = 20; 
     mTextColor = 0xFF000000; 
    } 

    public void setPath(Path path) 
    { 
     mPath = path; 

     // invalidate the path map. 
     mPathMap = null; 
    } 

    // This method converts the path into a bitmap which will be used to determine if a point is within the path 
    private void generatePathMap() 
    { 
     if (mPath != null) 
     { 
      // the path map bitmap can have poor quality, we're only checking for color or no color in each pixel. 
      mPathMap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_4444); 

      Canvas canvas = new Canvas(mPathMap); 

      Paint pathPaint = new Paint(); 
      pathPaint.setStyle(Paint.Style.FILL); 
      pathPaint.setColor(0xFFFFFFFF); 

      // draw the path. 
      canvas.drawPath(mPath, pathPaint); 
     } 
    } 

    public void setText(String text) 
    { 
     mText = text; 
    } 

    public void setFontSize(float fontSize) 
    { 
     mFontSize = fontSize; 
    } 

    public void setTextColor(int textColor) 
    { 
     mTextColor = textColor; 
    } 

    public void setWrapMode(WrapMode wrapMode) 
    { 
     mWrapMode = wrapMode; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 

     // make sure we have enough data to begin drawing text. 
     if (mPath == null || mText == null || getMeasuredWidth() == 0 || getMeasuredHeight() == 0) 
      return; 

     // if the path map hasn't been generated, generate it now. 
     if (mPathMap == null) 
      generatePathMap(); 

     final List<Rect> writableRects = getTextRects(); 
     final List<String> textFragments = getTextFragments(); 

     mPaint.setColor(mTextColor); 
     mPaint.setTextSize(mFontSize); 

     int rectIndex = 0; 
     int fragmentIndex = 0; 
     Rect rect = null; 
     String textFragment = null; 
     float textWidth; 

     // maybe find a better way to limit this loop? 
     while (true) 
     { 
      // we don't have a rectangle. Get the next 1 in the list. 
      if (rect == null) 
      { 
       // no more rectangles to draw text on. Finish. 
       if (rectIndex >= writableRects.size()) 
        return; 

       rect = new Rect(writableRects.get(rectIndex)); 
       rectIndex++; 
      } 

      // we don't have text to print. Get the next word in the list. 
      if (textFragment == null) 
      { 
       // no more text to draw. Finish. 
       if (fragmentIndex >= textFragments.size()) 
        return; 

       textFragment = textFragments.get(fragmentIndex); 
       fragmentIndex++; 
      } 

      // find how much width this text wants. 
      textWidth = mPaint.measureText(textFragment); 

      // if the rectangle doesn't have enough width, set it to null, indicating its "used up" and we need to next rect. Don't continue drawing text, find a new rect first. 
      if (textWidth > rect.width()) 
      { 
       rect = null; 
       continue; 
      } 

      // draw the text. 
      canvas.drawText(textFragment, rect.left, rect.centerY(), mPaint); 

      // the word has been drawn. Set it null indicating a new 1 is needed in the next iteration. 
      textFragment = null; 

      // remove the used width from the rect and continue. 
      rect.left += textWidth; 

      // In word mode, account for the space that was removed. 
      if (mWrapMode == WrapMode.Words) 
      { 
       rect.left += mPaint.measureText(" "); 
      } 
     } 
    } 

    // get each String fragment as a list. For letters mode, each element will be a letter or char. For words mode, each element will be a word. 
    private List<String> getTextFragments() 
    { 
     List<String> result = new ArrayList<String>(); 

     if (mWrapMode == WrapMode.Letters) 
     { 
      for (int i = 0; i < mText.length(); i++) 
      { 
       result.add("" + mText.charAt(i)); 
      } 
     } 
     else if (mWrapMode == WrapMode.Words) 
     { 
      String[] words = mText.split("\\s+"); 

      for (String word : words) 
       result.add(word); 
     } 


     return result; 
    } 

    private List<Rect> getTextRects() 
    { 
     final List<Rect> result = new ArrayList<Rect>(); 

     boolean isInPolygon = false; 
     Rect rect = null; 

     // place y in the center of the text, jump in fontsize steps. 
     for (int y = (int)(mFontSize/2); y < getMeasuredHeight(); y += mFontSize) 
     { 
      // place x at 0, jump with 5 px steps. This can be adjusted for better accuracy/performance. 
      for (int x = 0; x < getMeasuredWidth(); x += 5) 
      { 
       // Havent found a point within the polygon yet, but now I have! 
       if (!isInPolygon && mPathMap.getPixel(x, y) != 0) 
       { 
        isInPolygon = true; 
        rect = new Rect(x, y - (int)(mFontSize/2), x, y + (int)(mFontSize/2)); 
       } 
       // We found where the polygon started in this row, and now we found where it ends. 
       else if (isInPolygon && mPathMap.getPixel(x, y) == 0) 
       { 
        isInPolygon = false; 
        rect.right = x; 

        result.add(rect); 
       } 
      } 

      // If the edge is in the ploygon, limit the rect to the right side of the view. 
      if (isInPolygon) 
      { 
       rect.right = getMeasuredWidth(); 
       result.add(rect); 
      } 
     } 

     return result; 
    } 
} 

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 


    <com.gil.polygonwrap.PolygonWrapView 
     android:id="@+id/polygonWrap" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 

MainActivity.java: (उपयोग के उदाहरण)

public class MainActivity extends ActionBarActivity 
{ 
    private PolygonWrapView mPolygonWrapView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mPolygonWrapView = (PolygonWrapView)findViewById(R.id.polygonWrap); 

     final String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; 

     mPolygonWrapView.setText(text); 

     Path path = new Path(); 

     // sample of adding a path with a bezier curve element 
     path.moveTo(0, 0); 
     path.lineTo(500, 0); 
     path.cubicTo(700, 300, 400, 600, 800, 1000); 
     path.lineTo(0, 1000); 
     path.lineTo(0, 0); 

     // only needed when you don't close the path. 
     path.close(); 

     mPolygonWrapView.setPath(path); 
     mPolygonWrapView.setFontSize(30); 

     mPolygonWrapView.setBackgroundColor(0xFFFFFFFF); 

     mPolygonWrapView.invalidate(); 
    } 
} 

मैंने इसे यहां परीक्षण किया है और यह आपको शुरू करने के लिए कम से कम पर्याप्त काम करता है।

आप कुछ सुधार जोड़ना चाहते हैं, जैसे कि सुनिश्चित करना कि पूरी पंक्ति की ऊंचाई बहुभुज के भीतर है, न केवल पंक्ति का केंद्र।

इसके अलावा, आप केवल 1 पथ की बजाय पथों की एक सूची का समर्थन करना चाह सकते हैं - इस तरह आप x/y बॉक्स भरने की बजाए पथ खंडों के बीच पाठ को वितरित करने के तरीके को नियंत्रित कर सकते हैं जैसे कि मैं यहां कर रहा हूं ।

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

यदि आपके कोई और प्रश्न हैं तो मुझे बताएं।

संपादित करें: मैंने यह उदाहरण दिखाने के लिए उपयोग उदाहरण संपादित किया है कि इस पथ को बेजियर वक्र के साथ कैसे कार्यान्वित किया जाए। Here अधिक जानकारी के लिए पथ के साथ एक बेजियर वक्र बनाने का संदर्भ है।

+0

इसका उत्तर देने के लिए समय निकालने के लिए धन्यवाद .. मैं आपको कोशिश करूँगा – Shruti

+0

क्या यह उत्तर आपकी समस्या का समाधान करता है? –

+0

मैं अभी भी स्पष्ट नहीं हूं कि मैं पथ में एक बेजियर वक्र कैसे जोड़ सकता हूं, कृपया मुझे इस पर कुछ सुझाव दें – Shruti