以下是一些看起来与我需要的代码相近的代码:
@OverrIDeprotected voID dispatchDraw(Canvas into) { int w = into.getWIDth(); int h = into.getHeight(); int w3 = w / 3; int h3 = h / 3; into.drawline(0,w,h,mPaint); Region protect = new Region(w / 3,h / 3,2 * w / 3,2 * h / 3); into.clipRegion(protect,Op.INTERSECT); into.drawcolor(color.magenta); // or super.dispatchDraw() here.}
这给了我这个:
这与我想要的完全相反.请注意上面代码中名为“protect”的区域.我希望洋红色填充发生在除了该区域之外的任何地方.具体来说,我想看到的是:
在窗口的类比中,我应该能够移除限制并以正常方式绘制一个人或某物,这与窗户和墙壁重叠.
编辑:这是Rajesh CP答案的简化工作版本.我还在末尾的所有内容上添加了一个红色的“前景”条纹,以表明我可以删除限制并添加它们.谢谢Rajesh!
public class MaskTest extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(new ClippVIEw(getApplicationContext())); } private class ClippVIEw extends VIEw { private Paint line_paint = new Paint(); private Paint strip_paint = new Paint(); public ClippVIEw(Context context) { super(context); line_paint.setcolor(color.GRAY); line_paint.setstrokeWIDth(20); strip_paint.setcolor(color.RED); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); int w = canvas.getWIDth(); int h = canvas.getHeight(); int w3 = w / 3; int h3 = h / 3; // This line represents some unkNown background that we are drawing over. canvas.drawline(0,line_paint); // 'protect' represents some area to not paint over until desired. Region protect = new Region(w3,h3,2 * h / 3); canvas.clipRegion(protect,Op.DIFFERENCE); // Paint magenta over the entire canvas,avoIDing the protected region. canvas.drawcolor(color.magenta); // Remove the protected region. Region all = new Region(0,h); canvas.clipRegion(all,Op.UNION); // Draw a wIDe foreground strip over the entire canvas. canvas.drawRect(w / 2 - w / 20,w / 2 + w / 20,strip_paint); } }}解决方法
public class ClippVIEw extends VIEw{ private Paint paint= new Paint(); public ClippVIEw(Context context) { super(context); } private Region protect; /* * (non-Javadoc) * @see androID.vIEw.VIEw#onDraw(androID.graphics.Canvas) * @since Apr 12,2013 * @author rajeshcp */ @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); // vIEw.buildDrawingCache(); int w = canvas.getWIDth(); int h = canvas.getHeight(); int w3 = w / 3; int h3 = h / 3; canvas.drawline(0,paint); protect = (protect == null) ? new Region(w3,2 * h / 3) : protect; canvas.clipRegion(protect,Op.DIFFERENCE); canvas.drawcolor(color.magenta); }}
这样做我认为这就是你想要的.
总结以上是内存溢出为你收集整理的如何在Android中绘画时屏蔽一个简单的区域?全部内容,希望文章能够帮你解决如何在Android中绘画时屏蔽一个简单的区域?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)