2010-10-28 22 views
26

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

उत्तर

44

आपको कैनवास कक्षा की drawText विधि का उपयोग करना होगा।

Paint paint = new Paint(); 
canvas.drawPaint(paint); 
paint.setColor(Color.BLACK); 
paint.setTextSize(16); 
canvas.drawText("My Text", x, y, paint); 

यहाँ इसके बारे में प्रासंगिक दस्तावेज है:

http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String, float, float, android.graphics.Paint)

1

एक और (यकीनन बेहतर) एक कैनवास पर पाठ आकर्षित करने के लिए जिस तरह से एक StaticLayout उपयोग करने के लिए है। यह आवश्यक होने पर मल्टीलाइन टेक्स्ट को संभालता है।

String text = "This is some text."; 

TextPaint textPaint = new TextPaint(); 
textPaint.setAntiAlias(true); 
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density); 
textPaint.setColor(0xFF000000); 

int width = (int) textPaint.measureText(text); 
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
staticLayout.draw(canvas); 

TextPaint और StaticLayout यहीं चित्रण के लिए इस्तेमाल किया जा रहा से पहले instantiated कर रहे थे। onDraw में ऐसा करने से प्रदर्शन को नुकसान पहुंचाएगा। Here is a better example उन्हें एक कस्टम व्यू के संदर्भ में दिखा रहा है जो इसे अपना टेक्स्ट खींचता है।

6

यहां एक और जवाब होता था जो हटा दिया गया क्योंकि यह केवल एक लिंक था। मूल लिंक here है। कोड मूल रूप से वही है, लेकिन मैंने गैर टेक्स्ट ड्राइंग भागों को बाहर निकाला और आधुनिक स्क्रीन घनत्वों पर बेहतर काम करने के लिए आकारों को बढ़ाया।

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

public class MainActivity extends AppCompatActivity { 

    DemoView demoview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     demoview = new DemoView(this); 
     setContentView(demoview); 
    } 

    private class DemoView extends View { 
     public DemoView(Context context){ 
      super(context); 
     } 

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

      // custom drawing code here 
      // remember: y increases from top to bottom 
      // x increases from left to right 
      int x = 0; 
      int y = 0; 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 

      canvas.save(); 
      canvas.translate(100, 200); 

      // make the entire canvas white 
      canvas.drawColor(Color.WHITE); 

      // draw some text using STROKE style 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(1); 
      paint.setColor(Color.MAGENTA); 
      paint.setTextSize(100); 
      canvas.drawText("Style.STROKE", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some text using FILL style 
      paint.setStyle(Paint.Style.FILL); 
      //turn antialiasing on 
      paint.setAntiAlias(true); 
      //paint.setTextSize(30); 
      canvas.drawText("Style.FILL", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some rotated text 
      // get text width and height 
      // set desired drawing location 
      x = 75; 
      y = 185; 
      paint.setColor(Color.GRAY); 
      //paint.setTextSize(25); 
      String str2rotate = "Rotated!"; 

      // draw bounding rect before rotating text 
      Rect rect = new Rect(); 
      paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect); 
      canvas.translate(x, y); 
      paint.setStyle(Paint.Style.FILL); 
      // draw unrotated text 
      canvas.drawText("!Rotated", 0, 0, paint); 
      paint.setStyle(Paint.Style.STROKE); 
      canvas.drawRect(rect, paint); 
      // undo the translate 
      canvas.translate(-x, -y); 

      // rotate the canvas on center of the text to draw 
      canvas.rotate(-45, x + rect.exactCenterX(), 
        y + rect.exactCenterY()); 
      // draw the rotated text 
      paint.setStyle(Paint.Style.FILL); 
      canvas.drawText(str2rotate, x, y, paint); 

      //undo the translation and rotation 
      canvas.restore(); 
     } 
    } 
} 

कुछ drawing text along a path है:

enter image description here

यहाँ अद्यतन कोड है।

यह भी देखें this fuller answer here जो निम्न छवि देता है।

enter image description here

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