本文主要是介绍自定义控件(30)---绘图区域(Range),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自启舰
public void setEmpty() //置空
public boolean set(Region region)
public boolean set(Rect r)
public boolean set(int left, int top, int right, int bottom)
public boolean setPath(Path path, Region clip)//使用SetPath()构造不规则区域
public class MyRegionView extends View { public MyRegionView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //初始化画笔 Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.FILL); paint.setStrokeWidth(2); Region rgn = new Region(10,10,100,100); // rgn.set(100, 100, 200, 200); drawRegion(canvas, rgn, paint); } //这个函数不懂没关系,下面会细讲 private void drawRegion(Canvas canvas,Region rgn,Paint paint) { RegionIterator iter = new RegionIterator(rgn); Rect r = new Rect(); while (iter.next(r)) { canvas.drawRect(r, paint); } } }
未开启Set函数时
使用Set函数后,替换为新区域
使用SetPath()构造不规则区域
public class MyRegionView extends View { public MyRegionView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //初始化Paint Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.FILL); paint.setStrokeWidth(2); //构造一个椭圆路径 Path ovalPath = new Path(); RectF rect = new RectF(50, 50, 200, 500); ovalPath.addOval(rect, Direction.CCW); //SetPath时,传入一个比椭圆区域小的矩形区域,让其取交集 Region rgn = new Region(); rgn.setPath(ovalPath,new Region(50, 50, 200, 200)); //画出路径 drawRegion(canvas, rgn, paint); } //这个函数不懂没关系,下面会细讲 private void drawRegion(Canvas canvas,Region rgn,Paint paint) { RegionIterator iter = new RegionIterator(rgn); Rect r = new Rect(); while (iter.next(r)) { canvas.drawRect(r, paint); } } }
这篇关于自定义控件(30)---绘图区域(Range)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!