美文网首页
自定义View - Region

自定义View - Region

作者: 改名_f64e | 来源:发表于2021-04-15 18:27 被阅读0次
    构造函数 : 
    Region(region:Region)
    Region(rect:Rect)
    Region(left:Int, top:Int, right:Int, bottom:Int)
    
    设置区域范围,注意 : set()会覆盖原来的数据
    public boolean set(@NonNull Region region)
    public boolean set(@NonNull Rect r)
    public boolean set(int left, int top, int right, int bottom)
    
    测试代码 : 
        private val mRegion: Region = Region(Rect(100, 100, 400, 400)).also {
            it.set(Rect(100, 500, 400, 800)) // 使用后覆盖原来构造函数传的Rect()值
        }
        private val mRegionIterator: RegionIterator = RegionIterator(mRegion)
        private val mRect: Rect = Rect()
        private val mPaint: Paint = Paint().also {
            it.color = Color.BLACK
        }
    
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            while (mRegionIterator.next(mRect)) {
                canvas.drawRect(mRect, mPaint)
            }
        }
    
    1618460196(1).png
    对一个路径进行区域设置
    path : path路径
    region : 裁剪的范围(通过设置矩形范围,在矩形范围内的path路径会显示出来,超出部分不显示)
    public boolean setPath(@NonNull Path path, @NonNull Region clip)
    测试代码 : 
        private val mPath: Path = Path().also {
            it.addCircle(300f, 300f, 200f, Path.Direction.CW)
        }
        private val mClipRegion: Region = Region().also {
            it.set(Rect(100, 100, 500, 300))
        }
        private val mRegion: Region = Region().also {
            it.setPath(mPath, mClipRegion)
        }
        private val mRegionIterator: RegionIterator = RegionIterator(mRegion)
        private val mRect: Rect = Rect()
        private val mPaint: Paint = Paint().also {
            it.color = Color.BLACK
        }
    
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            while (mRegionIterator.next(mRect)) {
                canvas.drawRect(mRect, mPaint)
            }
        }
    
    1618461073(1).png

    为什么在onDraw()中绘制Region区域的时候需要使用RegionIterator

    Region 在绘制的时候不是一次性绘制完成,
    而是通过一个Rect()(next()中传入的Rect)区域一部分一部分的绘制
    测试代码 : 上面path()的代码修改 
        private val mPaint: Paint = Paint().also {
            it.color = Color.BLACK
            it.style = Paint.Style.STROKE //不使用填充模式
        }
    
    1618461372(1).png
    /*
      获取path,如果没有设置过path,返回一个空的path
    */
    public Path getBoundaryPath()
    
    /*
      如果区域包含多个矩形,则返回true
    */
    public native boolean isComplex()
    例子 : 
        private val mRegion: Region = Region(Rect(100, 100, 500, 300))
        return false //因为这里只有一个矩形,一次性画完
    
        private val mRegion: Region = Region().also {
            it.setPath(mPath, mClipRegion)
        }
        return true //这里是使用多个矩形最后拼接成一个形状,使用Paint.Style.STROKE可以看到
    
    /*
     清空 Region
    */
    public void setEmpty()
    
    /*
      返回Region的范围,如果为空,返回[0,0 - 0,0]
      如果是通过path设置,返回的是clipRegion的范围
    */
    public boolean getBounds(@NonNull Rect r)
    
    /*
      判断某个点或者某个范围是否在Region上
      (如果style = Paint.Style.STROKE,那就只有在边上才返回true)
    */
    public native boolean contains(int x, int y)
    public boolean quickContains(@NonNull Rect r)
    public native boolean quickContains(int left, int top, int right, int bottom)
    
    /*
      裁剪
    */
    public boolean op(@NonNull Rect r, @NonNull Op op)
    public boolean op(int left, int top, int right, int bottom, @NonNull Op op)
    public boolean op(@NonNull Region region, @NonNull Op op)
    public boolean op(@NonNull Rect rect, @NonNull Region region, @NonNull Op op)
    public boolean op(@NonNull Region region1, @NonNull Region region2, @NonNull Op op)
    测试代码 : 
        private val mRectSource:Rect = Rect(100, 100, 500, 500)
        private val mRectDst: Rect = Rect(60, 60, 300, 300)
    
    
        private val mRectSourceN:Rect = Rect(100, 800, 500, 1200)
        private val mRectDstN: Rect = Rect(60, 760, 300, 1000)
    
        private val mRegion: Region = Region(mRectSourceN).also {
            it.op(mRectDstN, Region.Op.REVERSE_DIFFERENCE)
        }
        private val mRegionIterator: RegionIterator = RegionIterator(mRegion)
    
        private val mRect: Rect = Rect()
        private val mPaint: Paint = Paint().also {
            it.color = Color.BLACK
        }
    
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            mPaint.color = Color.GREEN
            canvas.drawRect(mRectSource, mPaint)
            mPaint.color = Color.RED
            canvas.drawRect(mRectDst, mPaint)
            while (mRegionIterator.next(mRect)) {
                canvas.drawRect(mRect, mPaint)
            }
        }
    

    Region.Op.DIFFERENCE

    1618482017(1).png

    Region.Op.INTERSECT

    1618482132(1).jpg

    Region.Op.REVERSE_DIFFERENCE

    1618482195(1).jpg

    Region.Op.XOR

    1618482249(1).jpg

    Region.Op.REPLACE

    1618482302(1).jpg

    Region.Op.UNION

    1618482396(1).jpg

    相关文章

      网友评论

          本文标题:自定义View - Region

          本文链接:https://www.haomeiwen.com/subject/lwsdlltx.html