Drawing- Android Programming -
i want draw straight line next finger on touch on canvas, such action_down point starting point , action_up end point. path followed finger can figure should line starting point end point. please help me ondraw() function.
this code draw line when touch finger 1 point drawlineview.java
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.util.attributeset; import android.view.motionevent; import android.view.view; public class drawlineview extends view { private paint paint = new paint(); private path path= new path(); float eventx; float eventy; public drawlineview(context context, attributeset attrs) { super(context, attrs); paint.setantialias(true); paint.setstrokewidth(5f); paint.setcolor(color.blue); paint.setstyle(paint.style.stroke); } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, paint); } @override public boolean ontouchevent(motionevent event) { eventx=event.getx(); eventy=event.gety(); switch(event.getaction()){ case motionevent.action_down : path.moveto(eventx, eventy); homecoming true; case motionevent.action_up : path.lineto(eventx, eventy); break; default : homecoming false; } invalidate(); homecoming true; } }
activity start it:
import android.app.activity; import android.os.bundle; public class drawactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new drawlineview(this, null)); } }
android
No comments:
Post a Comment