Canvas剪裁
来源:互联网 发布:域名怎么绑定到ip端口 编辑:程序博客网 时间:2024/05/21 22:40
http://my.eoe.cn/37547/archive/3712.html
剪裁是Canvas的重要功能之一。可以剪切画裁的指定区域。并且可以对多次剪切的结果进行组合,实现组合效果。android画布类提供了如下函数实现剪切功能。
boolean clipPath(Path path)。用指定路径剪切画布。
boolean clipPath(Path path, Region.Op op)。用指定路径剪裁画布,并根据Region.Op标记对多次剪裁结果进行组合。
boolean clipRect(Rect rect, Region.Op op)。用指定矩形剪裁画布,并根据Region.Op标记对多次剪裁结果进行组合。
boolean clipRect(RectF rect, Region.Op op)。用指定矩形剪裁画布,并根据Region.Op标记对多次剪裁结果进行组合。
boolean clipRect(int left, int top, int right, int bottom)。用指定矩形剪裁画布。
boolean clipRect(float left, float top, float right, float bottom) 。用指定矩形剪裁画布。
boolean clipRect(RectF rect)。用指定矩形剪裁画布。
boolean clipRect(float left, float top, float right, float bottom, Region.Op op)。用指定举行剪裁画布。并根据Region.Op标记对多次剪裁结果进行组合。
boolean clipRect(Rect rect)。用指定矩形剪裁画布。
boolean clipRegion(Region region)。用指定区域剪裁画布。
boolean clipRegion(Region region, Region.Op op)。用指定区域剪裁画布。并根据Region.Op标记对多次剪裁结果进行组合。
Region.Op 表示多次裁剪的组合方式,有如下选择:
假设有两次clip操作,结果分别为集合A 和 B,那么下面的操作方式分别表示:
Region.Op DIFFERENCE A - B
Region.Op INTERSECT A∩B
Region.Op REPLACE B
Region.Op REVERSE_DIFFERENCE B - A
Region.Op UNION A ∪ B
Region.Op XOR (A∪B) - (A∩B)
效果图如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117
代码如下:package com.example.androidgraphicsprogramm;import android.app.Activity;import android.content.Context;import android.graphics.*;import android.os.Bundle;import android.util.Log;import android.view.View;public class Clipping extends Activity { private static final String TAG = "Clipping"; @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, 160, 160); canvas.drawColor(Color.WHITE); mPaint.setColor(Color.RED); canvas.drawLine(0, 0, 160, 160, mPaint); mPaint.setColor(Color.GREEN); canvas.drawCircle(47, 113, 47, mPaint); mPaint.setColor(Color.BLUE); canvas.drawText("Clipping", 140, 30, mPaint); } @Override protected void onDraw(Canvas canvas) { boolean clipresult = false; canvas.drawColor(Color.GRAY); canvas.save(); canvas.translate(10, 10); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(180, 10); clipresult = canvas.clipRect(20, 20, 140, 140); Log.d(TAG,"clip result is: " + clipresult); clipresult = canvas.clipRect(50, 50, 110, 110, Region.Op.DIFFERENCE); Log.d(TAG,"clip result with op is: " + clipresult); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10, 200); canvas.clipRect(20, 20, 140, 140); mPath.reset(); canvas.clipPath(mPath); // makes the clip empty mPath.addCircle(80, 80, 80, Path.Direction.CCW); canvas.clipPath(mPath, Region.Op.REPLACE); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(180, 200); canvas.clipRect(10, 10, 100, 100); canvas.clipRect(80, 80, 170, 170, Region.Op.UNION); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10, 390); canvas.clipRect(0, 0, 80, 80); canvas.clipRect(40, 40, 120, 120, Region.Op.XOR); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(180, 390); canvas.clipRect(0, 0, 80, 80); canvas.clipRect(40, 40, 120, 120, Region.Op.REVERSE_DIFFERENCE); drawScene(canvas); canvas.restore(); canvas.save(); canvas.translate(10, 580); canvas.clipRect(0, 0, 80, 80); canvas.clipRect(40, 40, 120, 120, Region.Op.INTERSECT); drawScene(canvas); canvas.restore(); } }}
- Canvas剪裁
- canvas剪裁(一)
- canvas图片上传剪裁
- 使用HTML5的Canvas画布来剪裁用户头像
- 原生js canvas画图(剪裁)后上传到服务器
- 区域剪裁
- 直线剪裁:
- windows剪裁
- 照片剪裁
- 图片剪裁
- imx6smart剪裁
- 图片剪裁
- unity3D剪裁
- 剪裁图片
- 图片剪裁
- 图片剪裁
- 图片剪裁
- 头像剪裁
- coursera week5 notification
- 3月培训季
- 程序员浪费生命的几种方式
- 技术名言
- 算法起步之kmp算法
- Canvas剪裁
- C++的变量类型汇总(持续更新中)
- Android属性动画深入分析:让你成为动画牛人
- java文件下载的中文文件名乱码问题的解决
- 异常信息ErrorMessage.properties
- Oracle当前数据库用户访问另一个用户的表
- 招人的一点感悟
- 我对面向对象的理解
- 采用CAS原理构建单点登录