canvas教程

Android canvas画图操作之切割画布实现方法(clipRect)

字号+ 作者:H5之家 来源:H5之家 2017-03-11 08:04 我要评论( )

这篇文章主要介绍了Android canvas画图操作之切割画布实现方法,通过clipRect方法实现canvas画布的切割操作,需要的朋友可以参考下

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Region; import android.util.AttributeSet; import android.view.View; public class sBook extends View{ Context mContext; Paint mPaint; Path mPath; public sBook(Context context) { super(context); init(); } public sBook(Context context, AttributeSet attrs) { super(context, attrs); init(); } public sBook(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init(){ mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(6); mPaint.setTextSize(16); mPaint.setTextAlign(Paint.Align.RIGHT); mPath = new Path(); } protected void onDraw(Canvas canvas){ canvas.drawColor(Color.GRAY); canvas.save(); canvas.translate(10, 10); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160, 10); canvas.clipRect(10, 10, 90, 90); canvas.clipRect(30, 30, 70, 70, Region.Op.XOR); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10, 160); mPath.reset(); // canvas.clipPath(mPath); // makes the clip empty // mPath.addCircle(50, 50, 50, Path.Direction.CCW); mPath.cubicTo(0, 0, 100, 0, 100, 100); mPath.cubicTo(100, 100, 0, 100, 0, 0); canvas.clipPath(mPath, Region.Op.REPLACE); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160, 160); canvas.clipRect(0, 0, 60, 60); canvas.clipRect(40, 40, 100, 100, Region.Op.UNION); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10, 310); canvas.clipRect(0, 0, 60, 60); canvas.clipRect(40, 40, 100, 100, Region.Op.XOR); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(160, 310); canvas.clipRect(0, 0, 60, 60); canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE); drawScene(canvas); canvas.restore(); } private void drawScene(Canvas canvas) { canvas.clipRect(0, 0, 100, 100); canvas.drawColor(Color.WHITE); mPaint.setColor(Color.RED); canvas.drawLine(0, 0, 100, 100, mPaint); mPaint.setColor(Color.GREEN); canvas.drawCircle(30, 70, 30, mPaint); mPaint.setColor(Color.BLUE); canvas.drawText("Clipping", 100, 30, mPaint); } }

 

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

相关文章
  • HTML5 Canvas教程: 简介

    HTML5 Canvas教程: 简介

    2017-03-10 12:00

  • canvas画饼状图

    canvas画饼状图

    2017-03-10 11:01

  • HTML5 Canvas教程:简介

    HTML5 Canvas教程:简介

    2017-03-10 10:02

  • canvas 有没有大神会createjs,求教如何学习使用createjs

    canvas 有没有大神会createjs,求教如何学习使用createjs

    2017-03-10 08:01

网友点评
i