paint的基础用法

开发中我们经常需要自定义控件,这时候就离不开画笔与画布,今天我们先来说说画笔的用法。

Paint crilepaint= new Paint();  // 初始化一个画笔

crilepaint.reset(); // 重置画笔
        crilepaint.setColor(Color.parseColor("#9a9a9a"));
        crilepaint.setAntiAlias(true);  // 抗锯齿,损失性能
        crilepaint.setAlpha(255);  // //设置画笔的透明度[0-255],0是完全透明,255是完全不透明
        crilepaint.setStyle(Paint.Style.FILL); // 填充内容,如果画圆表示实心
        crilepaint.setStyle(Paint.Style.FILL_AND_STROKE); // 填充内容并描边
        crilepaint.setStyle(Paint.Style.STROKE); // 描边,如果画圆表示空心
        crilepaint.setStrokeWidth(50); // 设置画笔的宽度
        crilepaint.setDither(true); // 设置是否使用图像抖动处理,会使绘制的图片等颜色更加清晰饱满。 损失性能

        //设置着色器,用来给图像着色的,绘制出各种渐变效果,有BitmapShader,ComposeShader,LinearGradient,RadialGradient,SweepGradient几种
setShader(Shader shader)
//设置画笔颜色过滤器,有ColorMatrixColorFilter,LightingColorFilter,PorterDuffColorFilter几种
setColorFilter(ColorFilter filter)
//设置图形重叠时的显示方式,下面来演示一下
setXfermode(Xfermode xfermode)


//设置绘制路径的效果,有ComposePathEffect,CornerPathEffect,DashPathEffect,DiscretePathEffect,PathDashPathEffect,SumPathEffect几种
setPathEffect(PathEffect effect)
//对图像进行一定的处理,实现滤镜的效果,如滤化,立体等,有BlurMaskFilter,EmbossMaskFilter几种
setMaskFilter(MaskFilter maskfilter)
//设置字体样式,可以是Typeface设置的样式,也可以通过Typeface的createFromAsset(AssetManager mgr, String path)方法加载样式
setTypeface(Typeface typeface)
//设置阴影效果,radius为阴影角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色 ,看一下演示效果,其中第一个是没有阴影的,第二个设置了黑色的阴影
setShadowLayer(float radius, float dx, float dy, int shadowColor)

//这个是文本缓存,设置线性文本,如果设置为true就不需要缓存,
setLinearText(boolean linearText)
//设置亚像素,是对文本的一种优化设置,可以让文字看起来更加清晰明显,可以参考一下PC端的控制面板-外观和个性化-调整ClearType文本
setSubpixelText(boolean subpixelText)
//设置文本的下划线
setUnderlineText(boolean underlineText)
//设置文本的删除线
setStrikeThruText(boolean strikeThruText)
//设置文本粗体
setFakeBoldText(boolean fakeBoldText)
//对位图进行滤波处理,如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示 
setFilterBitmap(boolean filter)
//下面这几个就不用说了,上面已经演示过
setStyle(Style style),setStrokeCap(Cap cap),setStrokeJoin(Join join),setTextAlign(Align align),
//设置画笔颜色
setColor(int color)
//设置画笔的透明度[0-255],0是完全透明,255是完全不透明
setAlpha(int a)
//设置画笔颜色,argb形式alpha,red,green,blue每个范围都是[0-255],
setARGB(int a, int r, int g, int b)
//画笔样式为空心时,设置空心画笔的宽度
setStrokeWidth(float width)
//当style为Stroke或StrokeAndFill时设置连接处的倾斜度,这个值必须大于0,看一下演示结果
setStrokeMiter(float miter)
//设置地理位置,比如显示中文,日文,韩文等,默认的显示Locale.getDefault()即可,
setTextLocale(Locale locale)
//设置优雅的文字高度,这个设置可能会对FontMetrics产生影响
setElegantTextHeight(boolean elegant)
//设置字体大小
setTextSize(float textSize)
//设置字体的水平方向的缩放因子,默认值为1,大于1时会沿X轴水平放大,小于1时会沿X轴水平缩小
setTextScaleX(float scaleX)
//设置文本在水平方向上的倾斜,默认值为0,推荐的值为-0.25,
setTextSkewX(float skewX)
//设置行的间距,默认值是0,负值行间距会收缩
setLetterSpacing(float letterSpacing)
//设置字体样式,可以设置CSS样式
setFontFeatureSettings(String settings)
//这个Paint的静态内部类,主要用于字体的高度,以后再分析
FontMetrics
//下面几个就是测量字体的长度了
measureText(char[] text, int index, int count),measureText(String text, int start, int end),measureText(String text),measureText(CharSequence text, int start, int end)
//下面这几个就是剪切显示,就是大于maxWidth的时候只截取指定长度的显示
breakText(char[] text, int index, int count,float maxWidth, float[] measuredWidth),breakText(CharSequence text, int start, int end,boolean measureForwards,  floatmaxWidth, float[] measuredWidth),breakText(String text, boolean measureForwards,float maxWidth, float[] measuredWidth)
//提取指定范围内的字符串,保存到widths中,
getTextWidths(char[] text, int index, int count,float[] widths),getTextWidths(CharSequence text, int start, int end, float[] widths),getTextWidths(String text, int start, int end, float[] widths),getTextWidths(String text, float[] widths)
//获取文本绘制的路径,提取到Path中,
getTextPath(char[] text, int index, int count, float x, float y, Path path),getTextPath(String text, int start, int end, float x, float y, Path path)
//得到文本的边界,上下左右,提取到bounds中,可以通过这计算文本的宽和高
getTextBounds(String text, int start, int end, Rect bounds) ,getTextBounds(char[] text, int index, int count, Rect bounds)
// 设置线帽,比如画一个圆形进度条,开始的时候画出的是圆的还是放的起始点
        crilepaint.setStrokeCap(Paint.Cap.BUTT);  // 没有线帽
        crilepaint.setStrokeCap(Paint.Cap.ROUND);  // 圆形线帽
        crilepaint.setStrokeCap(Paint.Cap.SQUARE);  //方形线帽

这里写图片描述

crilepaint.setStrokeJoin(Paint.Join.ROUND);  // 两条直线的连接处是圆角(圆弧)的
        crilepaint.setStrokeJoin(Paint.Join.BEVEL);  // 两条直线的连接处是直角(直线)的
        crilepaint.setStrokeJoin(Paint.Join.MITER);  // 两条直线的连接处是锐角的

这里写图片描述

crilepaint.setTextAlign(Paint.Align.CENTER);  // 绘制文字居中
        crilepaint.setTextAlign(Paint.Align.LEFT);    // 绘制文字在左边
        crilepaint.setTextAlign(Paint.Align.RIGHT);   // 绘制文字在右边

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值