本文主要是介绍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的canvas的clipRegion废弃替代代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!