ontouchevent - Android: Touch event for select item on a canvas -
i have many circles drawn on canvas , want know of them touched. 1 solution create bitmap size of screen , draw copy of circles on screen different color when call getpixel () bitmap poosso recognize circle clicked. problem not know how draw circle on bitmap ... is, how draw bitmap drawing on canvas.
paint paint; bitmap screen; int w,h; int px=-1,py=-1; //coordinate @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { //registriamo le dimensioni della view w=measurespec.getsize(widthmeasurespec); h=measurespec.getsize(heightmeasurespec); setmeasureddimension(w,h); } public customview(context context, attributeset attrs) { super(context, attrs); bitmap.config conf = bitmap.config.argb_8888; //each pixel stored on 4 bytes screen=bitmap.createbitmap(w,h, conf); paint=new paint(); // pennello paint.setcolor(color.red); paint.setantialias(true); } @override protected void ondraw(canvas canvas) { // todo auto-generated method stub super.ondraw(canvas); if(px==-1&&py==-1){ // se non abbiamo ancora settato le coordinate, posizioniamo la bmp al centro px=w/2-bw/2; //metà della larghezza view, meno metà della figura py=h/2-bh/2; //metà dell'altezza view, meno metà della figura } canvas.drawcircle(px, py, 70, paint); } public void updateposition(int x, int y) { invalidate(); } }
you want create canvas associated bitmap this
gcanvas = new canvas(); gcanvas.setbitmap(yourbitmap);
and use canvas , draw want
edit: in case wasn't clear, draw on canvas draw on bitmap. according x, y color , compare
hope helps , enjoy work
edit: working example
public class mainactivity extends activity { private bitmap image; private int width; private int height; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.clickete); display display = getwindowmanager().getdefaultdisplay(); width = display.getwidth(); height = display.getheight(); createclickarea(); findviewbyid(r.id.clicklayout).setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { int result = whichcircle((int)event.getrawx(), (int)event.getrawy()); log.i("color clicked: ", string.valueof(result)); return false; } }); drawable dr = new bitmapdrawable(getresources(), image); findviewbyid(r.id.clicklayout).setbackgrounddrawable(dr); } private void createclickarea(){ bitmap.config conf = bitmap.config.argb_8888; image = bitmap.createbitmap(width, height, conf); canvas gcanvas = new canvas(); gcanvas.setbitmap(image); paint paint = new paint(); paint.setstyle(style.fill); gcanvas.drawrgb(0, 0, 0); for(int = 0; < 4; i++){ switch(i){ case 0: paint.setcolor(color.white); break; case 1: paint.setcolor(color.yellow); break; case 2: paint.setcolor(color.blue); break; case 3: paint.setcolor(color.green); break; } gcanvas.drawcircle((i + 1) * 80, (i + 1) * 80, 40, paint); } } private int whichcircle(int x, int y){ int result = 0; int color = image.getpixel(x, y); switch(color){ case color.white: result = 1; break; case color.yellow: result = 2; break; case color.blue: result = 3; break; case color.green: result = 4; break; } return result; } }
just resolve deprecated methods , hide bitmap, sorry don't have time replace now, lunch getting cold :)'. , remember work in full screen or adjust rawx, rawy according bitmap position.
Comments
Post a Comment