Android 绘图笔记(一)Region

来源:互联网 发布:淘宝小号信誉查询 编辑:程序博客网 时间:2024/04/30 10:01

Android Region 介绍

Region 顾名思义表示一片区域,可惜的是Canvas 内并没有直接drawRegion 这个方法。它的主要作用有两点。

  • 区域和Path 之间的转换。
  • Region.Op 枚举参数,表示碰撞的生成规则。(碰撞是不是更有力度)

Region构造

/** Create an empty region*/public Region() {    this(nativeConstructor());}/** Return a copy of the specified region*/public Region(Region region) {    this(nativeConstructor());    nativeSetRegion(mNativeRegion, region.mNativeRegion);}/** Return a region set to the specified rectangle*/public Region(Rect r) {    mNativeRegion = nativeConstructor();    nativeSetRect(mNativeRegion, r.left, r.top, r.right, r.bottom);}/** Return a region set to the specified rectangle*/public Region(int left, int top, int right, int bottom) {    mNativeRegion = nativeConstructor();    nativeSetRect(mNativeRegion, left, top, right, bottom);}

当然也少不了各种set 方法。

/** Set the region to the empty region*/public void setEmpty() {    nativeSetRect(mNativeRegion, 0, 0, 0, 0);}/** Set the region to the specified region.*/public boolean set(Region region) {    nativeSetRegion(mNativeRegion, region.mNativeRegion);    return true;}/** Set the region to the specified rectangle*/public boolean set(Rect r) {    return nativeSetRect(mNativeRegion, r.left, r.top, r.right, r.bottom);}/** Set the region to the specified rectangle*/public boolean set(int left, int top, int right, int bottom) {    return nativeSetRect(mNativeRegion, left, top, right, bottom);}/** * Set the region to the area described by the path and clip. * Return true if the resulting region is non-empty. This produces a region * that is identical to the pixels that would be drawn by the path * (with no antialiasing). */public boolean setPath(Path path, Region clip) {    return nativeSetPath(mNativeRegion, path.ni(), clip.mNativeRegion);}

重点说一下 public boolean setPath(Path path, Region clip)这个方法。 就是在Region 上利用path 进行裁剪,也就是取两者交集。不过不抗锯齿。。。。。

Region和Path 的转换

/** * Return the boundary of the region as a new Path. If the region is empty, * the path will also be empty. */public Path getBoundaryPath() {    Path path = new Path();    nativeGetBoundaryPath(mNativeRegion, path.ni());    return path;}/** * Set the path to the boundary of the region. If the region is empty, the * path will also be empty. */public boolean getBoundaryPath(Path path) {    return nativeGetBoundaryPath(mNativeRegion, path.ni());}

得到一个区域的path 以后。可以clip 也可以draw 。。。。

区域的碰撞

    /** * Perform the specified Op on this region and the specified rect. Return * true if the result of the op is not empty. */public boolean op(Rect r, Op op) {    return nativeOp(mNativeRegion, r.left, r.top, r.right, r.bottom,                    op.nativeInt);}/** * Perform the specified Op on this region and the specified rect. Return * true if the result of the op is not empty. */public boolean op(int left, int top, int right, int bottom, Op op) {    return nativeOp(mNativeRegion, left, top, right, bottom,                    op.nativeInt);}/** * Perform the specified Op on this region and the specified region. Return * true if the result of the op is not empty. */public boolean op(Region region, Op op) {    return op(this, region, op);}/** * Set this region to the result of performing the Op on the specified rect * and region. Return true if the result is not empty. */public boolean op(Rect rect, Region region, Op op) {    return nativeOp(mNativeRegion, rect, region.mNativeRegion,                    op.nativeInt);}/** * Set this region to the result of performing the Op on the specified * regions. Return true if the result is not empty. */public boolean op(Region region1, Region region2, Op op) {    return nativeOp(mNativeRegion, region1.mNativeRegion,                    region2.mNativeRegion, op.nativeInt);}

可以看到可以和Rect 碰撞,也可以和Region 碰撞。
如下是官方ApiDemo 给出的示例。

    public enum Op {    DIFFERENCE(0),    INTERSECT(1),    UNION(2),    XOR(3),    REVERSE_DIFFERENCE(4),    REPLACE(5);    Op(int nativeInt) {        this.nativeInt = nativeInt;    }    /**     * @hide     */    public final int nativeInt;}

不同OP模式下,两个矩形碰撞

0 0
原创粉丝点击