Android Canvas Paint draws text

Source: Internet
Author: User

Android Canvas Paint draws text
There are three types of fonts: 1, built-in basic font 2, through the basic font and style, create a new Typeface 3, introduce external ttf files.
The sample code is as follows:

1. package com. mike. activity;
2.
3. import android. R;
4. import android. app. Activity;
5. import android. graphics. Bitmap;
6. import android. graphics. Canvas;
7. import android. graphics. Color;
8. import android. graphics. Paint;
9. import android. graphics. Typeface;
10. import android. graphics. Paint. Style;
11. import android. graphics. Path;
12. import android. graphics. RectF;
13. import android. graphics. drawable. BitmapDrawable;
14. import android. OS. Bundle;
15. import android. view. ViewGroup. LayoutParams;
16. import android. widget. ImageView;
17. import android. widget. LinearLayout;
18.
19. public class DrawDemoActivity extends Activity {
20./** Called when the activity is first created .*/
21. @ Override
22. public void onCreate (Bundle savedInstanceState ){
23. super. onCreate (savedInstanceState );
24.
25 ./*
26. * configuration type: 1, ALPHA_8: the bitmap used for the Alpha mask. Only eight bits are allocated for the alpha channel, with no other colors.
27. * 2, ARGB_4444,
28. * 3, ARGB_8888,
29. * 4, RGB565 (same quality as ARGB_8888, but less memory occupied)
30 .*
31. * ARGB_8888: each color occupies 8 digits and creates a bitmap.
32. * param: A: alpha channel: grayscale channel, used to indicate transparency information, totaling 256 levels
33. * R: red
34. * G: green
35. * B: blue
36 .*
37 .*
38. * Note: mask: The mask must be used with layers. Masks are useful in many ways and can be used to deduct graphs and create constituency,
39. * since I have not used PS for a long time, I cannot finish all the functions of the Board.
40. For example, a mask is a piece of cloth. After a mask is added to a layer, it is a piece of cloth on that layer,
41. You can use the cloth to achieve a variety of effects. You can't see anything under the layer where the mask is located when you fill it with black,
42. Fill it with white to see what is under the mask layer, you can also use the mask to make a gradient.
43. In short, the Board function is complicated. It is simple and simple. This guy has a great function.
44 .*
45 .*/
46.
47.
48.
49 ./*
50. * 1, built-in Font:
51 .*
52. * a, Typeface. MONOSPACE: equal to the width of the word, the interval between each letter is equal.
53. * B, Typeface. SANS_SERIF: unlined
54. * c, Typeface. SERIF: lined
55. * d, Typeface. DEFAULT: = Typeface. SANS_SERIF
56. * e, Typeface. DEFAULT_BOLD unlined bold
57 .*
58 .*
59 .*
60.*2. create a New Typeface from the basic font and style.
61. * a, Typeface. BOLD
62. * B, Typeface. ITALIC Italian
63. * c, Typeface. NORMAL
64. * d, Typeface. BOLD_ITALIC coarse Italian
65 .*
66.*3. External font. Use asset to introduce the ttf file.
67 .*/
68.
69.
70.
71.
72.
73.
74. Bitmap bitmap = Bitmap. createBitmap (getWindowManager (). getDefaultDisplay (). getWidth (),
75. getWindowManager (). getdefadisplay display (). getHeight (),
76. Bitmap. Config. ARGB_8888); // configure
77.
78. Canvas canvas = new Canvas (bitmap); // Canvas
79.
80. Paint paint = new Paint ();
81. paint. setColor (Color. WHITE); // you can set the Color directly or use the Argb method to set the exact Color.
82. // int myColor = Color. argb (alpha, red, green, blue );
83. paint. setStrokeWidth (50); // setting this size does not change TextSize.
84.
85. // 1. Use the built-in font
86. paint. setTypeface (Typeface. DEFAULT );
87. // 2. Create a font Based on the style.
88. Typeface typeface = Typeface. create (Typeface. DEFAULT, Typeface. ITALIC );
89. // 3. Create a font from an external file
90. // Typeface. createFromAsset (getAssets (), "filename. ttf ");
91.
92. paint. setTextSize (50); // The Size has a higher priority.
93. // canvas. drawText ("hello", 100,100, paint); // draw text
94.
95.
96 ./*
97. * draw words based on the specified path
98 .*/
99. Path path = new Path ();
100. path. moveTo (10, 10 );
101. path. lineTo (50, 50 );
102. path. lineTo (150,250 );
103. canvas. drawTextOnPath ("hello", path, 10, 10, paint );
104.
105.
106. ImageView imageView = new ImageView (this );
107.
108. LayoutParams p = new LayoutParams (LinearLayout. LayoutParams. FILL_PARENT, LinearLayout. LayoutParams. FILL_PARENT );
109. imageView. setLayoutParams (p );
110. imageView. setBackgroundDrawable (new BitmapDrawable (bitmap ));
111.
112. setContentView (imageView );
113.
114.
115 .}
116 .}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.