android的canvas的clipRegion废弃替代代码

由于clipRegion的一些问题,导致他被废弃了,但又有时候会用到,所以写了一个工具类来替代它
代码如下

package com.example;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;

public class Util {

    public static void clipRegion(Region region, Canvas canvas){
        Region combinedRegion = new Region();
        Path compa=new Path();
        RegionIterator regionIterator=new RegionIterator(region);
        Rect sum=new Rect();
        Rect rect=new Rect();
        while(regionIterator.next(rect)){
//            combinedRegion.op(rect, Region.Op.UNION);
            compa.addRect(rect.left,rect.top,rect.right,rect.bottom, Path.Direction.CCW);
//            canvas.clipRect(rect);

        }
        canvas.clipPath(compa);
    }

    public static void drawRegion(Region region, Canvas canvas){
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        RegionIterator regionIterator=new RegionIterator(region);
        Rect rect=new Rect();
        while(regionIterator.next(rect)){

            canvas.drawRect(rect,paint);

        }

    }
}

代码解释:其实就是通过把region转化为path然后调用clippath来完成的。
代码有两个函数clipRegion和drawRegion就和名字一样一个是切割,一个是绘画,把相应的参数传入进去就可以了

你可能感兴趣的:(Android开发问题,android)