canvas教程

Android2DGraphics学习(二)、Canvas篇2、Canvas裁剪和Region、(2)

字号+ 作者:H5之家 来源:H5之家 2017-12-18 18:50 我要评论( )

[java] viewplain Note that unlike clipRect() and clipPath() which transform their argumentsby the current matrix, clipRegion() assumes its argument isalready in the coordinate system of the current l

[java] view plain

  • Note that unlike
  • clipRect() and clipPath() which transform their arguments by the
  • current matrix, clipRegion() assumes its argument is already in the
  • coordinate system of the current layer's bitmap, and so not
  • transformation is performed.
  • Note that unlike clipRect() and clipPath() which transform their arguments by the current matrix, clipRegion() assumes its argument is already in the coordinate system of the current layer's bitmap, and so not transformation is performed. 与clipRect和clipPath要使用当前的matrix进行变换不同。clipRegion不会进行转换。也就是说canvas的matrix对clipRegion没有影响。

    [java] view plain

  • ), paint);//矩形实际大小为50*50
  • canvas.drawColor(Color.BLACK);
  • Paint paint=new Paint(); canvas.scale(0.5f, 0.5f); canvas.save(); canvas.clipRect(new Rect(100,100,200,200));//裁剪区域实际大小为50*50 canvas.drawColor(Color.RED); canvas.restore(); canvas.drawRect(new Rect(0,0,100,100), paint);//矩形实际大小为50*50 canvas.clipRegion(new Region(new Rect(300,300,400,400)));//裁剪区域实际大小为100*100 canvas.drawColor(Color.BLACK);

    可以看到,Canvas的变换 对clipRegion没有作用。


    ApiDemo中关于组合的例子:

    [java] view plain

  • Activity {
  • @Override
  • protected void onCreate(Bundle savedInstanceState) {
  • super.onCreate(savedInstanceState);
  • }
  • View {
  • private Paint mPaint;
  • private Path mPath;
  • public SampleView(Context context) {
  • super(context);
  • private void drawScene(Canvas canvas) {
  • , mPaint);
  • mPaint.setColor(Color.BLUE);
  • , mPaint);
  • }
  • @Override
  • protected void onDraw(Canvas canvas) {
  • canvas.clipPath(mPath); // makes the clip empty
  • , Path.Direction.CCW);
  • public class Clipping extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private static class SampleView extends View { private Paint mPaint; private Path mPath; public SampleView(Context context) { super(context); setFocusable(true); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(6); mPaint.setTextSize(16); mPaint.setTextAlign(Paint.Align.RIGHT); mPath = new Path(); } 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); } @Override 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.DIFFERENCE); 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); 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(); } } }
    效果图:

     

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

    相关文章
    网友点评