Android自定义控件:路径及文字

建立路径

canvas中绘制路径利用:canvas

void drawPath (Path path, Paint paint)
bash

直线路径

void moveTo (float x1, float y1):直线的开始点;将直线路径的绘制点定在(x1,y1)的位置;
void lineTo (float x2, float y2):直线的结束点,又是第二次绘制直线路径的开始点;lineTo()能够一直用;
void close ():若是连续画了几条直线,但没有造成闭环,调用Close()会将路径首尾点链接起来,造成闭环;
函数

1       Paint paint = new Paint();
 2
 3       paint.setColor(Color.BLUE);//设置画笔颜色
 4       paint.setStyle(Paint.Style.STROKE);//填充样式改成描边
 5       paint.setStrokeWidth(10); //设置画笔宽度
 6
 7        Path path = new Path();
 8
 9        path.moveTo(5,5);//设置起始点
10        path.lineTo(10,100);//第一条直线的终点,及第二条直线的起点
11        path.lineTo(200,100);//
12        path.lineTo(400,100);//第三条直线
13        path.close();//闭环
14
15        canvas.drawPath(path,paint);
复制代码


矩形路径

void addRect (float left, float top, float right, float bottom, Path.Direction dir)
void addRect (RectF rect, Path.Direction dir)字体

这里Path类建立矩形路径的参数与上篇canvas绘制矩形差很少,惟一不一样点是增长了Path.Direction参数;
Path.Direction有两个值:ui


Path.Direction.CCW:指建立逆时针方向的矩形路径;
Path.Direction.CW:指建立顺时针方向的矩形路径;
spa

1        Paint paint = new Paint();
 2        paint.setColor(Color.BLUE);//设置画笔颜色
 3        paint.setStyle(Paint.Style.STROKE);//填充样式改成描边
 4        paint.setStrokeWidth(10); //设置画笔宽度
 5   //先建立两个不同的路径
 6  //第一个逆向生成
 7
 8       Path CCWpath = new Path();
 9
10       RectF rectF = new RectF(50,50,250,200);
11       CCWpath.addRect(rectF, Path.Direction.CCW);
12
13    //第二个顺向生成
14       Path CWpath = new Path();
15       RectF rectF1 = new RectF(300,50,450,200) ;
16       CWpath.addRect(rectF1, Path.Direction.CW);
17
18
19    // 先画出这两个路径
20        canvas.drawPath(CCWpath,paint);
21        canvas.drawPath(CWpath,paint);
复制代码


1        Paint paint = new Paint();
 2        paint.setColor(Color.BLUE);//设置画笔颜色
 3        paint.setTextSize(20);
 4        paint.setStyle(Paint.Style.STROKE);//填充样式改成描边
 5       paint.setStrokeWidth(1); //设置画笔宽度
 6   //先建立两个不同的路径
 7  //第一个逆向生成
 8
 9       Path CCWpath = new Path();
10
11       RectF rectF = new RectF(50,50,250,200);
12       CCWpath.addRect(rectF, Path.Direction.CCW);
13
14    //第二个顺向生成
15       Path CWpath = new Path();
16       RectF rectF1 = new RectF(300,50,450,200) ;
17       CWpath.addRect(rectF1, Path.Direction.CW);
18
19
20    // 先画出这两个路径
21        canvas.drawPath(CCWpath,paint);
22        canvas.drawPath(CWpath,paint);
23
24        String text = "新手上路,多多努力,一块儿加油!";
25        paint.setColor(Color.RED);
26        canvas.drawTextOnPath(text,CCWpath,0,10,paint);//文字逆时针生成
27        canvas.drawTextOnPath(text,CWpath,0,10,paint);//文字顺时针生成
复制代码



文字

构造函数:
3d

void drawText (String text, float x, float y, Paint paint)code

void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)cdn

void drawText (String text, int start, int end, float x, float y, Paint paint)blog

void drawText (char[] text, int index, int count, float x, float y, Paint paint)

1    //普通设置
 2        Paint paint=new Paint();
 3        paint.setAntiAlias(true); //是否使用抗锯齿功能,若是使用,使绘图速度变慢
 4        paint.setStyle(Paint.Style.FILL);//绘图样式,对于设文字和几何图形有效
 5        //  Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
 6        paint.setTextAlign(Paint.Align.CENTER);//s设置文字对齐方式,取值:align.CENTER、align.LEFT或align.RIGHT
 7        paint.setTextSize(10);//设置文字大小
 8        paint.setStrokeWidth(10);//设置画笔宽度
 9
10        //样式设置
11        paint.setFakeBoldText(true);//将水平方向拉伸,高度不变
12        paint.setUnderlineText(true);//设置下划线
13        paint.setTextSkewX((float)-0.5);//设置字体水平倾斜度,普通斜体字是-0.5
14        paint.setStrikeThruText(true);//设置带有删除线效果
15
16        //其余设置
17        paint.setTextScaleX(5);//将水平方向拉伸,高度不变
复制代码


 Typeface create(String familyName, int style) //直接经过指定字体名来加载系统中自带的文字样式
Typeface create(Typeface family, int style) //经过其它Typeface变量来构建文字样式
Typeface createFromAsset(AssetManager mgr, String path) //经过从Asset中获取外部字体来显示字体样式
Typeface createFromFile(String path)//直接从路径建立
Typeface createFromFile(File path)//从外部路径来建立字体样式
Typeface defaultFromStyle(int style)//建立默认字体

1        //普通设置
 2        Paint paint=new Paint();
 3        paint.setColor(Color.BLUE);
 4        paint.setAntiAlias(true); //是否使用抗锯齿功能,若是使用,使绘图速度变慢
 5
 6         paint.setTextSize(100);//设置文字大小
 7         paint.setStrokeWidth(5);//设置画笔宽度
 8        //绘图样式,设置为填充
 9        paint.setStyle(Paint.Style.FILL);
10        canvas.drawText("加油努力吧!",150,100,paint);
11
12        //绘图样式设置为描边
13        paint.setStyle(Paint.Style.STROKE);
14        canvas.drawText("加油努力吧!",150,200,paint);
15
16         //绘图样式设置为填充而且描边
17        paint.setStyle(Paint.Style.FILL_AND_STROKE);
18        canvas.drawText("加油努力吧!",150,300,paint);
复制代码



Style的枚举值以下:
Typeface.NORMAL //正常体
Typeface.BOLD //粗体
Typeface.ITALIC //斜体
Typeface.BOLD_ITALIC //粗斜体

1  //普通设置
 2        Paint paint=new Paint();
 3        paint.setColor(Color.BLUE);
 4        paint.setAntiAlias(true); //是否使用抗锯齿功能,若是使用,使绘图速度变慢
 5
 6         paint.setTextSize(100);//设置文字大小
 7         paint.setStrokeWidth(5);//设置画笔宽度
 8        //绘图样式,设置为填充
 9        paint.setStyle(Paint.Style.FILL);
10        canvas.drawText("加油努力吧!",150,100,paint);
11
12        paint.setFakeBoldText(true);//将水平方向拉伸,高度不变
13        paint.setUnderlineText(true);//设置下划线
14        paint.setStrikeThruText(true);//设置带有删除线效果
15
16        paint.setTextSkewX((float)-0.5);//设置字体水平倾斜度,普通斜体字是-0.5
17        canvas.drawText("加油努力吧!",150,200,paint);
18
19
20        paint.setTextSkewX((float)0.5);//设置字体水平倾斜度,普通斜体字是-0.5
21        canvas.drawText("加油努力吧!",150,300,paint);
复制代码


1   //普通设置
 2        Paint paint=new Paint();
 3        paint.setColor(Color.BLUE);
 4        paint.setAntiAlias(true); //是否使用抗锯齿功能,若是使用,使绘图速度变慢
 5
 6         paint.setTextSize(50);//设置文字大小
 7         paint.setStrokeWidth(5);//设置画笔宽度
 8        //绘图样式,设置为填充
 9        paint.setStyle(Paint.Style.FILL);
10       canvas.drawText("加油努力吧!",10,50,paint);
11
12        paint.setTextScaleX(2);//水平方向拉伸,高度不变
13        canvas.drawText("加油努力吧!",10,100,paint);
14
15
16   //写在同一位置,同颜色,看高度是否不变
17
18
19        paint.setTextScaleX(1);//先还原拉伸效果
20        canvas.drawText("加油努力吧!",10,150,paint);
21
22        paint.setColor(Color.RED);
23        paint.setTextScaleX(2);//从新设置拉伸效果
24        canvas.drawText("加油努力吧!",10,200,paint);
复制代码


使用系统中的字体

 Typeface defaultFromStyle(int style)//建立默认字体
 Typeface create(String familyName, int style) //直接经过指定字体名来加载系统中自带的文字样式

1   //普通设置
 2        Paint paint=new Paint();
 3        paint.setColor(Color.BLUE);
 4        paint.setAntiAlias(true); //是否使用抗锯齿功能,若是使用,使绘图速度变慢
 5
 6         paint.setTextSize(100);//设置文字大小
 7         paint.setStrokeWidth(5);//设置画笔宽度
 8        //绘图样式,设置为填充
 9        paint.setStyle(Paint.Style.FILL);
10
11        String familyName = "宋体";
12        Typeface font = Typeface.create(familyName,Typeface.NORMAL);
13        paint.setTypeface(font);
14        canvas.drawText("努力加油",10,100, paint);
复制代码

相关文章
相关标签/搜索