android - Using clipRect - explanation -
public class pocii extends activity { myview mv = new myview(this); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(mv); } } class myview extends view { public myview(context context) { super(context); } @override public void ondraw(canvas canvas) { paint paint = new paint(); canvas.drawrect(0,0,100,100, paint); canvas.cliprect(0,0,50,50); } }
my question is, shouldn't above code draw rectangle , crop top left portion? rectangle not getting cropped.
please explain cliprect does. clipping? clip in form of rectangle, given co-ordinates? if so, why above code not working?
canvas.cliprect(left, top, right, bottom) reduces region of screen future draw operations can write to. sets clipbounds spacial intersection of current clipping rectangle , rectangle specified. there lot of variants of cliprect method accept different forms regions , allow different operations on clipping rectangle. if want explicitly set clipping region, try:
canvas.cliprect(left, top, right, bottom, region.op.replace);
the 5th argument means replace clipping rectangle rather creating intersection previous version.
try moving cliprect statement before drawrect statement. or, try adding:
paint.setcolor(color.yellow); drawrect(0,0,75,75);
after existing cliprect statement. should draw 50x50 yellow square on had before.
another note: (after long frustration apparently, largely undocumented view/viewgroup/drawing code) found canvas.translate(x,y) adjusts cliprect. interaction of cliprect , drawing matrix confusing. helpful print out:
canvas.getmatrix()
and
canvas.getclipbounds()
before , after modifications canvas , before drawing things.
Comments
Post a Comment