Android Canvas Paint Paint text

Source: Internet
Author: User

There are three types of fonts: 1, built-in basic font 2, the creation of new typeface 3 through basic fonts and styles, and the introduction of 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;
. import Android.graphics.Paint.Style;
One. Import Android.graphics.Path;
Import Android.graphics.RectF;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
. import Android.view.ViewGroup.LayoutParams;
. import Android.widget.ImageView;
. import Android.widget.LinearLayout;
18.
public class Drawdemoactivity extends Activity {
/** called when the activity is first created. */
@Override.
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
24.
25./*
26. * Configuration Type: 1,alpha_8: Bitmap for Alpha mask, only 8 bits assigned to alpha channel, no other color
* 2,argb_4444,
* 3,argb_8888,
* 4,RGB565 (with the same high quality as the argb_8888, but consumes less memory)
30. *
* argb_8888:8 bits per color, creating bitmaps
* Param:a:alpha Channel: Grayscale channel, used to indicate transparency information, a total of 256 levels
* r:red. *
* G:green. *
* B:blue
36. *
37. *
* Note: Mask: Masks are to be used in conjunction with layers. Masks are useful for drawing, creating selections, and so on.
39. * Because I have not used PS for a long time, so the good function of the mask here can not be finished at once.
40. Take a popular example: a mask is a piece of cloth, and after you add a mask on a layer, you cover a piece of cloth on that layer,
41. The use of the cloth can achieve a variety of effects, with black on the mask to see the mask is not the layer underneath the thing,
42. Fill it with white to see what's underneath the mask layer, and use the mask to make gradients.
43. In short, the mask of this function is complex, it is complex, saying it is simple, it is simple, this guy function is not small.
44. *
45. */
46.
47.
48.
49./*
50. * 1, built-in fonts:
51. *
* A,typeface.monospace: equal-width words instead, with each letter spaced equally
* b, Typeface.sans_serif: Sans serif
* C, Typeface.serif: Lined
* D, Typeface.default: = Typeface.sans_serif
* E, typeface.default_bold sans serif bold
57. *
58. *
59. *
60. * 2, font style created by basic fonts and styles new typeface
* A,typeface.bold Bold
* B,typeface.italic Italian body
* C,typeface.normal Ordinary
* D,typeface.bold_italic Coarse Italian body
65. *
66. * 3, external font, introduction of TTF file via asset
67. */
68.
69.
70.
71.
72.
73.
Bitmap Bitmap = Bitmap.createbitmap (Getwindowmanager (). Getdefaultdisplay (). GetWidth (),
Getwindowmanager (). Getdefaultdisplay (). GetHeight (),
Bitmap.Config.ARGB_8888);//Configuration
77.
The canvas canvas = new canvas (bitmap); Canvas
79.
Paint paint = new paint ();
Bayi. Paint.setcolor (Color.White);//You can set the color directly, or you can set the exact color by Argb method.
//int MyColor = Color.argb (alpha, red, green, blue);
Paint.setstrokewidth (50);//Setting this size does not change textsize
84.
85.//1, using built-in fonts
Paint.settypeface (Typeface.default);
87.//2, create fonts by style
Typeface Typeface = typeface.create (Typeface.default, typeface.italic);
89.//3, create fonts from external files
//Typeface.createfromasset (Getassets (), "Filename.ttf");
91.
Paint.settextsize (//size); higher priority
Canvas.drawtext ("Hello", [+], paint);//Normal drawing text
94.
95.
96./*
97. * Follow the specified path stooped
98. */
. path PATH = new Path ();
Path.moveto (10, 10);
101. Path.lineto (50, 50);
102. Path.lineto (150, 250);
103. Canvas.drawtextonpath ("Hello", Path, ten, ten, 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);
Imageview.setbackgrounddrawable (New bitmapdrawable (bitmap));
111.
Setcontentview (ImageView);
113.
114.
115.}
116.}

Android Canvas Paint Paint text

Related Article

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.