android canvas paint绘制相关图形

来源:互联网 发布:山人太极服淘宝 编辑:程序博客网 时间:2024/05/20 11:36


首先看canvas 从android doc 可以看到:

      The Canvas class holds the "draw" calls.

       To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

如果我们把Canvas当做绘画师来看,那么Paint就是我们绘画的工具,比如画笔、画刷、颜料等等。

之后看paint ,来自于http://emmet1988.iteye.com/blog/1058526


[java] view plaincopy
  1. /** 
  2.      * Paint类介绍 
  3.      *  
  4.      * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, 
  5.      * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法, 
  6.      * 大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关。        
  7.      *  
  8.      * 1.图形绘制 
  9.      * setARGB(int a,int r,int g,int b); 
  10.      * 设置绘制的颜色,a代表透明度,r,g,b代表颜色值。 
  11.      *  
  12.      * setAlpha(int a); 
  13.      * 设置绘制图形的透明度。 
  14.      *  
  15.      * setColor(int color); 
  16.      * 设置绘制的颜色,使用颜色值来表示,该颜色值包括透明度和RGB颜色。 
  17.      *  
  18.      * setAntiAlias(boolean aa); 
  19.      * 设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。 
  20.      *  
  21.      * setDither(boolean dither); 
  22.      * 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰 
  23.      *  
  24.      * setFilterBitmap(boolean filter); 
  25.      * 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示 
  26.      * 速度,本设置项依赖于dither和xfermode的设置 
  27.      *  
  28.      * setMaskFilter(MaskFilter maskfilter); 
  29.      * 设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等 
  30.      *  
  31.      * setColorFilter(ColorFilter colorfilter); 
  32.      * 设置颜色过滤器,可以在绘制颜色时实现不用颜色的变换效果 
  33.      *  
  34.      * setPathEffect(PathEffect effect); 
  35.      * 设置绘制路径的效果,如点画线等 
  36.      *  
  37.      * setShader(Shader shader); 
  38.      * 设置图像效果,使用Shader可以绘制出各种渐变效果 
  39.      *  
  40.      * setShadowLayer(float radius ,float dx,float dy,int color); 
  41.      * 在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色 
  42.      *  
  43.      * setStyle(Paint.Style style); 
  44.      * 设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE 
  45.      *  
  46.      * setStrokeCap(Paint.Cap cap); 
  47.      * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式 
  48.      * Cap.ROUND,或方形样式Cap.SQUARE 
  49.      *  
  50.      * setSrokeJoin(Paint.Join join); 
  51.      * 设置绘制时各图形的结合方式,如平滑效果等 
  52.      *  
  53.      * setStrokeWidth(float width); 
  54.      * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度 
  55.      *  
  56.      * setXfermode(Xfermode xfermode); 
  57.      * 设置图形重叠时的处理方式,如合并,取交集或并集,经常用来制作橡皮的擦除效果 
  58.      *  
  59.      * 2.文本绘制 
  60.      * setFakeBoldText(boolean fakeBoldText); 
  61.      * 模拟实现粗体文字,设置在小字体上效果会非常差 
  62.      *  
  63.      * setSubpixelText(boolean subpixelText); 
  64.      * 设置该项为true,将有助于文本在LCD屏幕上的显示效果 
  65.      *  
  66.      * setTextAlign(Paint.Align align); 
  67.      * 设置绘制文字的对齐方向 
  68.      *  
  69.      * setTextScaleX(float scaleX); 
  70.      * 设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果 
  71.      *  
  72.      * setTextSize(float textSize); 
  73.      * 设置绘制文字的字号大小 
  74.      *  
  75.      * setTextSkewX(float skewX); 
  76.      * 设置斜体文字,skewX为倾斜弧度 
  77.      *  
  78.      * setTypeface(Typeface typeface); 
  79.      * 设置Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等 
  80.      *  
  81.      * setUnderlineText(boolean underlineText); 
  82.      * 设置带有下划线的文字效果 
  83.      *  
  84.      * setStrikeThruText(boolean strikeThruText); 
  85.      * 设置带有删除线的效果 
  86.      *  
  87.      */  

例子:

[java] view plaincopy
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.graphics.Path;  
  6. import android.graphics.RectF;  
  7. import android.graphics.Xfermode;  
  8. import android.graphics.drawable.ShapeDrawable;  
  9. import android.graphics.drawable.shapes.OvalShape;  
  10. import android.view.View;  
  11.   
  12. public class LineView extends View {  
  13.   
  14.     // private ShapeDrawable mDrawable;  
  15.   
  16.     public LineView(Context context) {  
  17.         super(context);  
  18.         // mDrawable = new ShapeDrawable();  
  19.     }  
  20.     protected void onDraw(Canvas canvas) {  
  21.         canvas.drawColor(Color.WHITE);  
  22.         Paint mPaint=new Paint();  
  23.         mPaint.setColor(Color.GREEN);  
  24.         mPaint.setStrokeWidth(3.0f);  
  25.         canvas.drawLine(1010200200, mPaint);  
  26. //      画个圆  
  27.         mPaint.setColor(Color.MAGENTA);  
  28.           
  29.         mPaint.setAntiAlias(true);  
  30. //            设置实心 还是 空心 stroke  
  31. //      mPaint.setStyle(Paint.Style.STROKE);    
  32.         canvas.drawCircle(300160100, mPaint);  
  33. //      画一个空心长方形  
  34.         mPaint.setColor(Color.RED);  
  35.         canvas.drawRect(1019020290, mPaint);  
  36. //      画一个空心椭圆形  
  37.         mPaint.setColor(Color.YELLOW);  
  38.         RectF re = new RectF(100,10,180,40);  
  39.         canvas.drawOval(re, mPaint);  
  40. //      画一个空心三角形  
  41.         mPaint.setColor(Color.BLUE);  
  42.         Path path= new Path();  
  43.         path.moveTo(30010);  
  44.         path.lineTo(35020);  
  45.         path.lineTo(28040);  
  46.         path.close();  
  47.         canvas.drawPath(path, mPaint);  
  48.     }  
  49. }  
效果:

也可以通过以下代码实现 颜色的渐变

[java] view plaincopy
  1. Shader mShader = new LinearGradient(00100100new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);  
  2.         mPaint.setShader(mShader);  
0 0
原创粉丝点击