java - How to draw a rectangle in android when a user moves finger? -
i want draw rectangle. first corner should point user first touches screen. when user moves finger, should draw rectangle. here's a link shows video want do. don't understand , maybe can me. want draw rectangle on white background not on image.
my code :
package com.example.androiddrawing; import java.util.arraylist; import java.util.list; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.point; import android.util.attributeset; import android.view.motionevent; import android.view.view; public class canvasview extends view { private canvas canvas; private paint paint = new paint(); private paint paint2 = new paint(); private paint paint3 = new paint(); private path path = new path(); private point point = new point(); private static list<path> lines = new arraylist<path>(); private static list<point> points = new arraylist<point>(); private float x, x2, xc, xd, x3, x4; private float y, y2, yc, yd, y3, y4; private boolean touchstarted = false; public enum drawmode { freedrawmode, rectdrawmode }; public static drawmode currentdrawmode; public void setdrawmode(drawmode newdrawmode) { this.currentdrawmode = newdrawmode; } public canvasview(context context, attributeset attrs) { super(context, attrs); paint.setantialias(true); paint.setstrokewidth(5); paint.setcolor(color.black); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); paint2.setantialias(true); paint2.setstrokewidth(5); paint2.setcolor(color.red); paint2.setstyle(paint.style.stroke); paint2.setstrokejoin(paint.join.round); paint3.setantialias(true); paint3.setcolor(color.black); paint3.setstrokewidth(10); paint3.setstyle(paint.style.stroke); } @override protected void ondraw(canvas canvas) { (path p : lines) canvas.drawpath(p, paint); canvas.drawpath(path, paint2); (point point : points) canvas.drawcircle(point.x, point.y, 0, paint); } @override public boolean ontouchevent(motionevent event) { x = event.getx(); y = event.gety(); system.out.println(currentdrawmode); if (currentdrawmode == drawmode.freedrawmode) { switch (event.getaction()) { case motionevent.action_down: // set new starting point paint2.setcolor(color.red); path = new path(); path.moveto(x, y); touchstarted = true; break; // return true; case motionevent.action_move: // connect points touchstarted = false; path.lineto(x, y); break; case motionevent.action_up: if (touchstarted) { point = new point(); point.x = (int) x; point.y = (int) y; paint2.setcolor(color.black); points.add(point); touchstarted = false; system.out.println("siin"); } else { system.out.println("seal"); paint2.setcolor(color.black); lines.add(path); } break; default: return false; } } else if (currentdrawmode == drawmode.rectdrawmode) { switch (event.getaction()) { case motionevent.action_down: // set new starting point paint3.setcolor(color.red); //code here break; // return true; case motionevent.action_move: //code here break; case motionevent.action_up: //code here break; default: return false; } } // makes our view repaint , call ondraw invalidate(); return true; } }
i should write code put comments //code here, don't understand, how have draw rectangle.
you can use below code. hope helps you.
public class drawsample extends view { int mstartx; int mstarty; int mendx; int mendy; paint mpaint = new paint(); int mselectedcolor = color.black; public drawsample(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); mpaint.setcolor(mselectedcolor); mpaint.setstrokewidth(5); mpaint.setstyle(paint.style.stroke); setfocusable(true); } public drawsample(context context, attributeset attrs) { this(context, attrs, 0); } @override public boolean ontouchevent(motionevent event) { switch (event.getactionmasked()) { case motionevent.action_down: mstartx = (int) event.getx(); mstarty = (int) event.gety(); break; case motionevent.action_move: mendx = (int) event.getx(); mendy = (int) event.gety(); invalidate(); break; case motionevent.action_up: mendx = (int) event.getx(); mendy = (int) event.gety(); invalidate(); break; default: super.ontouchevent(event); break; } return true; } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawrect(mstartx, mstarty, mendx, mendy, mpaint); } }
Comments
Post a Comment