android - Program control flow to onDraw() -
android - Program control flow to onDraw() -
i know when method
protected void ondraw(final canvas canvas) {}
will called. asking the command flow.constructor of class called other class.when command comes constructor phone call methods in class??
also want drawing when draw image touched , moved. used ontouchevent(motionevent event).but dont know how invoke ondraw after coding in ontouch.that alter coordinate values how phone call ondraw redraw image?
can help?
public class drawview extends view { paint paint = new paint(); public drawview(context context) { // todo auto-generated constructor stub super(context); } @override protected void ondraw(final canvas canvas) { // todo auto-generated method stub paint.setcolor(color.black); paint.setstrokewidth(3); canvas.drawrect(30, 350, 50, 400, paint); super.ondraw(canvas); // other drawings } @override public boolean ontouchevent(motionevent event) { // todo auto-generated method stub switch (event.getaction()) { case motionevent.action_down://some code break; case motionevent.action_move://some code break; case motionevent.action_up://some code break; default: break; } homecoming super.ontouchevent(event); } }
/** * @author rajeshcp */
public class simpledrag extends view { private paint mpaint; private rect mrect; /** * @param context * @return of type simpledrag * constructor function * @since feb 19, 2013 * @author rajeshcp */ public simpledrag(context context) { super(context); init(); } /** * @param context * @param attrs * @return of type simpledrag * constructor function * @since feb 19, 2013 * @author rajeshcp */ public simpledrag(context context, attributeset attrs) { super(context, attrs); init(); } /** * @param context * @param attrs * @param defstyle * @return of type simpledrag * constructor function * @since feb 19, 2013 * @author rajeshcp */ public simpledrag(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } /* (non-javadoc) * @see android.view.view#ondraw(android.graphics.canvas) * @since feb 19, 2013 * @author rajeshcp */ @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawcolor(color.blue, porterduff.mode.clear); if( mrect != null ) { mpaint.setcolor(color.red); canvas.drawrect(mrect, mpaint); } } private void init() { mrect = new rect(0, 0, 50, 50); mpaint = new paint(paint.filter_bitmap_flag | paint.dither_flag | paint.anti_alias_flag); } private point mtouchpoint; /* (non-javadoc) * @see android.view.view#ontouchevent(android.view.motionevent) * @since feb 19, 2013 * @author rajeshcp */ @override public boolean ontouchevent(motionevent event) { final int action = event.getaction(); if( action == motionevent.action_cancel || action == motionevent.action_down) { mtouchpoint = new point((int)event.getx(), (int)event.gety()); if( !mrect.contains(mtouchpoint.x, mtouchpoint.y) ) { homecoming false; } } if( action == motionevent.action_move ) { final point curretpoint = new point((int)event.getx(), (int)event.gety()); int xmoved = curretpoint.x - mtouchpoint.x; int ymoved = curretpoint.y - mtouchpoint.y; mrect.set(mrect.left + xmoved, mrect.top + ymoved, mrect.right + xmoved, mrect.bottom + ymoved); mtouchpoint = curretpoint; invalidate(); } homecoming true; } }
call invalidate when ever want ondraw method called.
android ontouchevent ondraw
Comments
Post a Comment