मैं एंड्रॉइड के लिए एक साधारण पाई चार्ट क्लास विकसित करने की कोशिश कर रहा हूं। अभी के लिए, यह लेबल और मूल्यों का नक्शा ले सकता है और पाई चार्ट खींच सकता है। मैं अभी तक पाई के लिए किंवदंतियों को जोड़ने के लिए नहीं हूं, जहां मुझे स्क्रीन कोने पर छोटे आयतों के पास ग्रंथों को रखने की आवश्यकता है। किसी भी मदद की सराहना की, क्योंकि मैं एंड्रॉइड देव के लिए नया हूँ।कैनवास पर टेक्स्ट कैसे आकर्षित करें?
उत्तर
आपको कैनवास कक्षा की drawText विधि का उपयोग करना होगा।
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(16);
canvas.drawText("My Text", x, y, paint);
यहाँ इसके बारे में प्रासंगिक दस्तावेज है:
एक और (यकीनन बेहतर) एक कैनवास पर पाठ आकर्षित करने के लिए जिस तरह से एक 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 उन्हें एक कस्टम व्यू के संदर्भ में दिखा रहा है जो इसे अपना टेक्स्ट खींचता है।
यहां एक और जवाब होता था जो हटा दिया गया क्योंकि यह केवल एक लिंक था। मूल लिंक 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 है:
यहाँ अद्यतन कोड है।
यह भी देखें this fuller answer here जो निम्न छवि देता है।
- 1. एंड्रॉइड में कैनवास पर टेक्स्ट व्यू कैसे आकर्षित करें ..?
- 2. पिक्चरबॉक्स पर टेक्स्ट कैसे आकर्षित करें?
- 3. कैनवास पर टेक्स्ट इनपुट कैसे प्राप्त करें?
- 4. एचटीएमएल 5 कैनवास पर सर्कल सेक्टर कैसे आकर्षित करें?
- 5. एंड्रॉइड में बिटमैप पर कैसे आकर्षित करें?
- 6. एंड्रॉइड प्रोग्रामिंग: आयताकार में मल्टीलाइन टेक्स्ट कैसे आकर्षित करें?
- 7. जेएलएबल पर कैसे आकर्षित करें?
- 8. एचटीएमएल कैनवास: क्लिपिंग और टेक्स्ट
- 9. खिड़की पर छवि कैसे आकर्षित करें?
- 10. सीधे डेस्कटॉप पर कैसे आकर्षित करें?
- 11. मानचित्रव्यू पर एमकेपोलीलाइन कैसे आकर्षित करें?
- 12. सी ++ में स्क्रीन पर कैसे आकर्षित करें?
- 13. कैनवास पर एक डिफ़ॉल्ट एंड्रॉइड फ़ॉन्ट के साथ कैसे आकर्षित करें?
- 14. एंड्रॉइड कैनवास पर त्रिकोण, स्टार, स्क्वायर, दिल को कैसे आकर्षित करें
- 15. एचटीएमएल 5 कैनवास - छवि पृष्ठभूमि पर एक रेखा कैसे आकर्षित करें?
- 16. केवल ओपनजीएल विधियों का उपयोग करके टेक्स्ट कैसे आकर्षित करें?
- 17. WPF में फ़ंक्शन प्लॉट कैसे आकर्षित करें?
- 18. 3 डी क्षेत्र कैसे आकर्षित करें?
- 19. आयताकार कैसे आकर्षित करें?
- 20. फ़ायरफ़ॉक्स को कैनवास टेक्स्ट को सीएसएस टेक्स्ट
- 21. कैसे कैनवास
- 22. एक QGraphicsScene पर एक बिंदु (माउसक्लिक पर) कैसे आकर्षित करें?
- 23. Android पर ImageView पर लाइनों को कैसे आकर्षित करें?
- 24. किवी कैनवास पर ड्राइंग एसवीजी
- 25. एक खाली एमएस चार्ट चार्ट एरिया पर टेक्स्ट कैसे आकर्षित करें?
- 26. किसी अन्य एप्लिकेशन के शीर्ष पर ग्राफिक्स/टेक्स्ट कैसे आकर्षित करें
- 27. धुंधला आकार कैसे आकर्षित करें?
- 28. एक कोण पर ड्राइंग टेक्स्ट
- 29. कैनवास
- 30. स्विट छवि कैसे आकर्षित करें?