canvas教程

Android基础入门教程——8.3.1 三个绘图工具类详解(2)

字号+ 作者:H5之家 来源:H5之家 2015-10-31 09:01 我要评论( )

2.clipXXX()方法族:在当前的画图区域裁剪(clip)出一个新的画图区域,这个画图区域就是canvas 对象的当前画图区域了。比如:clipRect(new Rect()),那么该矩形区域就是canvas的当前画图区域 3.save()和restore()方法

2.clipXXX()方法族:在当前的画图区域裁剪(clip)出一个新的画图区域,这个画图区域就是canvas
对象的当前画图区域了。比如:clipRect(new Rect()),那么该矩形区域就是canvas的当前画图区域
3.save()和restore()方法:
save( ):用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作!
restore():用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。
save()和restore()要配对使用(restore可以比save少,但不能多),若restore调用次数比save多,会报错!
4.translate(float dx, float dy):
平移,将画布的坐标原点向左右方向移动x,向上下方向移动y.canvas的默认位置是在(0,0)
5.scale(float sx, float sy):扩大,x为水平方向的放大倍数,y为竖直方向的放大倍数
6.rotate(float degrees):旋转,angle指旋转的角度,顺时针旋转

3)Path(路径)

简单点说就是描点,连线~在创建好我们的Path路径后,可以调用Canvas的drawPath(path,paint)
将图形绘制出来~常用方法如下:

addArc(RectF oval, float startAngle, float sweepAngle:为路径添加一个多边形 addCircle(float x, float y, float radius, Path.Direction dir):给path添加圆圈 addOval(RectF oval, Path.Direction dir):添加椭圆形 addRect(RectF rect, Path.Direction dir):添加一个区域 addRoundRect(RectF rect, float[] radii, Path.Direction dir):添加一个圆角区域 isEmpty():判断路径是否为空 transform(Matrix matrix):应用矩阵变换 transform(Matrix matrix, Path dst):应用矩阵变换并将结果放到新的路径中,即第二个参数。

更高级的效果可以使用PathEffect类!
几个To:

moveTo(float x, float y):不会进行绘制,只用于移动移动画笔 lineTo(float x, float y):用于直线绘制,默认从(0,0)开始绘制,用moveTo移动!
比如
mPath.lineTo(300, 300);
canvas.drawPath(mPath, mPaint); quadTo(float x1, float y1, float x2, float y2):
用于绘制圆滑曲线,即贝塞尔曲线,同样可以结合moveTo使用!

title=

rCubicTo(float x1, float y1, float x2, float y2, float x3, float y3)
同样是用来实现贝塞尔曲线的。 (x1,y1) 为控制点,(x2,y2)为控制点,(x3,y3) 为结束点。
Same as cubicTo, but the coordinates are considered relative to the current point
on this contour.就是多一个控制点而已~
绘制上述的曲线:
mPath.moveTo(100, 500);
mPath.cubicTo(100, 500, 300, 100, 600, 500);
如果不加上面的那个moveTo的话:则以(0,0)为起点,(100,500)和(300,100)为控制点绘制贝塞尔曲线

title=

arcTo(RectF oval, float startAngle, float sweepAngle):
绘制弧线(实际是截取圆或椭圆的一部分)ovalRectF为椭圆的矩形,startAngle 为开始角度,
sweepAngle 为结束角度。 2.动手试试:

属性那么多,肯定要手把手的撸一下,才能加深我们的映像是吧~
嘿嘿,画图要么在View上画,要么在SurfaceView上画,这里我们在View上画吧,
我们定义一个View类,然后再onDraw()里完成绘制工作!

/** * Created by Jay on 2015/10/15 0015. */ public class MyView extends View{ private Paint mPaint; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ mPaint = new Paint(); mPaint.setAntiAlias(true); //抗锯齿 mPaint.setColor(getResources().getColor(R.color.puple));//画笔颜色 mPaint.setStyle(Paint.Style.FILL); //画笔风格 mPaint.setTextSize(36); //绘制文字大小,单位px mPaint.setStrokeWidth(5); //画笔粗细 } //重写该方法,在这里绘图 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } }

然后布局那里设置下这个View就好,下述代码都写在onDrawable中~

1)设置画布颜色: canvas.drawColor(getResources().getColor(R.color.yellow)); //设置画布背景颜色 2)绘制圆形: canvas.drawCircle(200, 200, 100, mPaint); //画实心圆

title=

3)绘制矩形: canvas.drawRect(0, 0, 200, 100, mPaint); //画矩形

title=

4)绘制Bitmap: canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), 0, 0, mPaint);

5)绘制弧形区域: canvas.drawArc(new RectF(0, 0, 100, 100),0,90,true,mPaint); //绘制弧形区域

假如true改为false:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Canvas与ValueAnimator

    Canvas与ValueAnimator

    2017-04-28 18:00

  • Canvas学习:绘制矩形

    Canvas学习:绘制矩形

    2017-04-24 17:02

  • Windows GDI和GDI+编程实例剖析

    Windows GDI和GDI+编程实例剖析

    2017-04-24 13:00

  • JavaScript Canvas绘制圆形时钟效果

    JavaScript Canvas绘制圆形时钟效果

    2017-04-21 09:04

网友点评
i