relativelayout - Android View/Layout Masking -


i have application has requirement entire relativelayout (which custom view extending it) needs masked using custom image. there many views in layout need accessed touches , dynamic.

i have masked region drawing shapes using answer question:

@override protected void dispatchdraw(canvas canvas) {     path path = new path();     int count = canvas.save();      path.addcircle(400, 200, 300, path.direction.cw);      canvas.clippath(path);      super.dispatchdraw(canvas);     canvas.restoretocount(count);  } 

however, need use image source mask source instead of shape can author. have used porterduff toolkit alter bitmap:

    bitmap resizedbitmap = bitmap.createscaledbitmap(mmask, this.getwidth(), this.getheight(), false);     bitmap result = bitmap.createbitmap(this.getwidth(), this.getheight(), bitmap.config.argb_8888); //create bitmap same height      canvas mcanvas = new canvas(result);     paint paint = new paint(paint.anti_alias_flag);     paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in));     mcanvas.drawbitmap(resizedbitmap, 0, 0, paint);     paint.setxfermode(null);     ondrawcanvas.drawbitmap(result, 0,0, paint); 

that works masking image, need mask clip (i think). there way derive path image using different porterduff mode? or there simpler method, in ios can set layermask directly image? in advance.

there isn't direct api in framework allows trace solid image mask , generate path can use clipping. if can access mask contents svg, might able use path tracing techniques romain guy describes here: http://www.curious-creature.org/2013/12/21/android-recipe-4-path-tracing/

while isn't favorite technique, best option i've seen combination of items you've described. need create off-screen buffer (via canvas+bitmap) view contents can rendered, , use transfer mode technique apply result onto canvas attached window. have not tried viewgroup in dispatch, should work. following (untested) code starting point:

@override protected void onsizechanged(int w, int h, int oldw, int oldh) {     if (w != oldw || h != oldh) {         //create area use content rendering         mcanvasbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888);         mcanvas = new canvas(mcanvasbitmap);         mxfermode = new porterduffxfermode(porterduff.mode.dst_in);     } }  @override protected void dispatchdraw(canvas canvas) {     //render contents our buffer     super.dispatchdraw(mcanvas);      //render mask, clipping porterduffxfermode     paint.setxfermode(mxfermode);     mcanvas.drawbitmap(mmask, 0, 0, paint);      //clear state , transfer result     paint.setxfermode(null);     canvas.drawbitmap(mcanvasbitmap, 0, 0, paint); } 

one drawback approach memory; sufficiently large view must allocate block equal size of view container. can minimize impact doing once , reusing buffer.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -