paint的基础用法

来源:互联网 发布:国外油画网站知乎 编辑:程序博客网 时间:2024/05/29 08:37

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

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);   // 绘制文字在右边

这里写图片描述

原创粉丝点击