java - Why OnTouchListener is not set for me? -
i working on custom view called canvasview
. view allows me draw stuff on outside of ondraw
method. this:
public class canvasview extends view { private arraylist<shape> shapes; private paint paint; public canvasview (context c) { super(c); init (); } public canvasview(context context, attributeset attrs) { super (context, attrs); init (); } public canvasview(context context, attributeset attrs, int defstyleattr) { super (context, attrs, defstyleattr); init (); } private void init () { shapes = new arraylist<> (); paint = new paint (); paint.setstrokewidth (5); paint.setcolor (color.black); } //focus on method, think others irrelevant @override public void setontouchlistener (final ontouchlistener listener) { final ontouchlistener baselistener = new ontouchlistener () { @override public boolean ontouch(view v, motionevent event) { float x = event.getx (); float y = event.gety (); if (x < 18 || x > getwidth () - 18 || y < 18 || y > getheight () - 18) return false; return true; } }; super.setontouchlistener (new ontouchlistener () { @override public boolean ontouch(view v, motionevent event) { if (baselistener.ontouch (v, event)) { if (listener != null) { return listener.ontouch (v, event); } else { return true; } } return false; } }); } @override protected void ondraw (canvas c) { super.ondraw (c); (shape s : shapes) { s.draw (c); } //draw border. irrelevant c.drawline (3, 3, getwidth () - 3, 3, paint); c.drawline (3, getheight () - 3, getwidth () - 3, getheight () - 3, paint); c.drawline (3, 3, 3, getheight () - 3, paint); c.drawline (getwidth () - 3, 3, getwidth () - 3, getheight () - 3, paint); //draw inner border c.drawline (18, 18, getwidth () - 18, 18, paint); c.drawline (18, getheight () - 18, getwidth () - 18, getheight () - 18, paint); c.drawline (18, 18, 18, getheight () - 18, paint); c.drawline (getwidth () - 18, 18, getwidth () - 18, getheight () - 18, paint); } public void addshape (shape s) { shapes.add (s); } public void clear () { shapes.clear (); } }
explanation:
shape
interface method:
public void draw (canvas c);
focus on setontouchlistener
override. overrode method because want limit user not touch borders of canvasview
. can see, first invoke baselistener
check whether touch in bounds. , invoke listener passed in constructor. practice?
anyway, set ontouchlistener
of canvasview
in oncreate
method:
canvas.setontouchlistener (new view.ontouchlistener () { @override public boolean ontouch(view v, final motionevent event) { if (point1 != null && point2 != null) { throw new illegalstateexception ("both point1 , point2 not null"); } if (point1 == null) { point1 = new pointf (event.getx (), event.gety ()); } else { //point2 null point2 = new pointf (event.getx (), event.gety ()); canvas.addshape (new shape () { @override public void draw(canvas c) { c.drawline (point1.x, point1.y, point2.x, point2.y, paint); } }); canvas.setontouchlistener (null); } canvas.addshape (new shape () { @override public void draw(canvas c) { c.drawcircle (event.getx (), event.gety (), 13, paint); } }); return true; } });
explanation:
point1
, point2
fields declared in activity class. when user touches screen, 1 of them instantiated , little circle drawn @ point. when user touches second time, point2
instantiated , line drawn between 2 points.
when run app , touch screen, nothing drawn! think setonclicklistener
override not written correctly. can tell me why?
the ondraw
method of view called when view first created. cause called again, need mark view “dirty” (changes have been made) using invalidate()
function on view, every time contents of view change.
thus, convenient place call function after adding shape view in addshape
function.
public void addshape (shape s) { shapes.add (s); this.invalidate(); }
Comments
Post a Comment