what i'm trying achieve:
this first time i've used surfaceview, mulitthreading , touch events. i'm trying mask rectangular bitmap image using circular bitmap mask. i've been attemping in thread inside surfaceview. surfaceview has ontouch() method when drag image move relative mask remain stationary.
the problem:
the mask works following code on first iteration of while loop. when hits unlockcanvasandpost(canvas) second time around overlays mask image on top of underlying bitmap , masking ceases taking effect. fixed, still having problems redraw in ontouch event - see below.
@override public void run() { while(isrunnable){ // draw canvas //if surface isn't available go top of loop if(!surfaceholder.getsurface().isvalid()){ continue; } result = bitmap.createbitmap(maskimage.getwidth(), maskimage.getheight(), bitmap.config.argb_8888); //lock canvas before trying draw canvas = surfaceholder.lockcanvas(); maskpaint = new paint(paint.anti_alias_flag); maskpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in)); canvas tempcanvas = new canvas(result); //draw bitmaps canvas canvas.drawargb(255, 255, 255, 255); tempcanvas.drawbitmap(usericonimage, imagex, imagey, null); tempcanvas.drawbitmap(maskimage, maskx, masky, maskpaint); maskpaint.setxfermode(null); canvas.drawbitmap(result,0,0,new paint()); surfaceholder.unlockcanvasandpost(canvas); result = null; } }// run()
what i've tried:
this works. when drag reposition imgage in ontouch() traces of drawn image. according android.developer.com can avoid using drawbitmap() canvas , drawables - surfaceview section. i'm trying do, apparently incorrectly. don't have continually create objects inside while loop , avoid if possible.
my ontouch() method so:
@override public boolean ontouch(view v, motionevent motionevent) { try{ thread.sleep(50); }catch (interruptedexception e){ e.printstacktrace(); } switch (motionevent.getaction()){ case motionevent.action_move: imagex = motionevent.getx(); imagey = motionevent.gety(); break; } return true; }
Comments
Post a Comment