view - onTouchevent() problem android -
when run code below works fine,it's simple app 3 balls can move around...
public class dragndrop extends activity { /** called when activity first created. */ private colorball[] colorballs = new colorball[3]; // array holds balls private static final string tag="mytag"; drawview myview; private int balid = 0; // variable know ball being dragged int x; int y; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); point point1 = new point(); point1.x = 50; point1.y = 20; point point2 = new point(); point2.x = 100; point2.y = 20; point point3 = new point(); point3.x = 150; point3.y = 20; // declare each ball colorball class colorballs[0] = new colorball(this,r.drawable.bol_groen, point1); colorballs[1] = new colorball(this,r.drawable.bol_rood, point2); colorballs[2] = new colorball(this,r.drawable.bol_blauw, point3); myview = new drawview(this); setcontentview(myview); } public class drawview extends view { public drawview(context context) { super(context); setfocusable(true); //necessary getting touch events // setting start point balls } // method draws balls @override protected void ondraw(canvas canvas) { //draw balls on canvas (colorball ball : colorballs) { canvas.drawbitmap(ball.getbitmap(), ball.getx(), ball.gety(), null); } } // events when touching screen public boolean ontouchevent(motionevent event) { int eventaction = event.getaction(); x = (int)event.getx(); y = (int)event.gety(); switch (eventaction ) { case motionevent.action_down: // touch down check if finger on ball balid = 0; (colorball ball : colorballs) { log.d(tag,"inside action down inside coords:"+x+" coords: "+y); log.d(tag,"ball coords:"+ball.getx()+" coords: "+ball.gety()); int x =x; int y =y; log.d(tag,"lalalalalala"+x+" coords: "+y); if (x > ball.getx() && x < ball.getx()+50 && y > ball.gety() && y < ball.gety()+50){//if (x > ball.getx() && x < ball.getx()+50 && y > ball.gety() && y < ball.gety()+50){ log.d(tag,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getx()+" coords: "+ball.gety()); balid = ball.getid(); break; } } break; case motionevent.action_move: // touch drag ball // move balls same finger if (balid > 0) { colorballs[balid-1].setx(x-25); colorballs[balid-1].sety(y-25); } break; case motionevent.action_up: // touch drop - things here after dropping break; } // redraw canvas myview.invalidate(); return true; } } }
but when try handle ontouchevent main activity doesn't work , strange can't read simple variable(x,y)!!! can't understand why happened,it seems can red them if it's in view!!!
public class dragndrop extends activity { /** called when activity first created. */ private colorball[] colorballs = new colorball[3]; // array holds balls private static final string tag="mytag"; drawview myview; private int balid = 0; // variable know ball being dragged int x; int y; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); point point1 = new point(); point1.x = 50; point1.y = 20; point point2 = new point(); point2.x = 100; point2.y = 20; point point3 = new point(); point3.x = 150; point3.y = 20; // declare each ball colorball class colorballs[0] = new colorball(this,r.drawable.bol_groen, point1); colorballs[1] = new colorball(this,r.drawable.bol_rood, point2); colorballs[2] = new colorball(this,r.drawable.bol_blauw, point3); myview = new drawview(this); setcontentview(myview); } // events when touching screen public boolean ontouchevent(motionevent event) { int eventaction = event.getaction(); x = (int)event.getx(); y = (int)event.gety(); switch (eventaction ) { case motionevent.action_down: // touch down check if finger on ball balid = 0; (colorball ball : colorballs) { log.d(tag,"inside action down inside coords:"+x+" coords: "+y); log.d(tag,"ball coords:"+ball.getx()+" coords: "+ball.gety()); int x =x; int y =y; log.d(tag,"lalalalalala"+x+" coords: "+y); if (x > ball.getx() && x < ball.getx()+50 && y > ball.gety() && y < ball.gety()+50){//if (x > ball.getx() && x < ball.getx()+50 && y > ball.gety() && y < ball.gety()+50){ log.d(tag,"inside ball coords!!:"+ball.getx()+" coords: "+ball.gety()); balid = ball.getid(); break; } } break; case motionevent.action_move: // touch drag ball // move balls same finger if (balid > 0) { colorballs[balid-1].setx(x-25); colorballs[balid-1].sety(y-25); } break; case motionevent.action_up: // touch drop - things here after dropping break; } // redraw canvas myview.invalidate(); return true; } public class drawview extends view { public drawview(context context) { super(context); setfocusable(true); //necessary getting touch events // setting start point balls } // method draws balls @override protected void ondraw(canvas canvas) { //canvas.drawcolor(0xffcccccc); //if want background color //draw balls on canvas (colorball ball : colorballs) { canvas.drawbitmap(ball.getbitmap(), ball.getx(), ball.gety(), null); } } } }
anyone knows why?
yes @bigstones ontouchevent working,it captures actions,the problem if have ontouchevent code inside activity variables x,y seems not work althought have value , can print it(or log)what saying tested,i've tried , change values if() statement(getx,gety)to integers , didn't work x,y.....check code again please! thanks!
found it!
i found problem layout should registering sort of listner did this:
i added code main clas:
private onclicklistener previewlistener = new onclicklistener() { @override public void onclick(view v) { system.err.println("i've been clicked"); } };
and line oncreate method:
previewlayout.setonclicklistener(previewlistener);
and worked!
Comments
Post a Comment