Android自定义控件之画笔(Paint)

初始化画笔:
Paint paint = new Paint();
设置画笔颜色
paint.setColor(Color.RED);
paint.setColor(0xffffffff);
设置填充样式
paint.setStyle(Paint.Style.STROKE);
//设置画笔宽度
paint.setStrokeWidth(50);
设置抗锯齿
paint.setAntiAlias(true);

Android在用画笔的时候有三种Style,分别是 :

Paint.Style.STROKE 仅描边
Paint.Style.FILL 仅填充内部
Paint.Style.FILL_AND_STROKE 填充内部和描边
例如:paint.setStyle(Paint.Style.STROKE);

效果图如下:

效果可见:FILL +STROKE = FILL_AND_STROKE

Paint的文字设置


三种字体风格:

三种对齐方式:

文字显示效果

设置文字粗体public void setFakeBoldText(boolean fakeBoldText)(true为粗体)
设置文字是否下划线public void setUnderlineText(boolean underlineText)(true为下划线)
设置文字中间是否有删除线public void setStrikeThruText(boolean strikeThruText)(ture为删除线)
三种效果合体演示:

设置倾斜角度:public void setTextSkewX(float skewX)
效果图:

水平拉伸public void setTextScaleX(float scaleX)
拉伸两倍,效果图

使用自定义字体

自定义字体创建方式有如下三种:

  • public static Typeface createFromAsset(AssetManager mgr, String path)
  • public static Typeface createFromFile(@Nullable File file)
  • public static Typeface createFromFile(@Nullable String path)1

createFromAsset示例代码:

AssetManager mgr= mContext.getAssets();//得到AssetManager
//根据路径得到Typeface
Typeface typeface=Typeface.createFromAsset(mgr, "fonts/jian_luobo.ttf");

获得字体后,设置字体即可:paint.setTypeface(typeface);

猜你喜欢

转载自blog.csdn.net/lijianbiao0/article/details/86519114