java - Android how to draw line between 2 points -
i newbie in android studio , took code stackoverflow. have problem draw line between 2 points. can draw line between 2 points like: b c. want draw lines between them want. c or c a. wanted draw lines on image background. have background there, when draw line background app lagging. here's code:
package com.example.grzegorz.kropy; import android.content.context; import android.content.res.resources; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.point; import android.graphics.drawable.*; import android.util.attributeset; import android.util.typedvalue; import android.view.motionevent; import android.view.view; import android.graphics.bitmapfactory; import java.util.arraylist; import java.util.list; public class paintview extends view { private bitmap mbitmap; private canvas mcanvas; // holds "draw" calls private path mpath; //create empty path private paint mpaint; // style , color information how draw geometries, text , bitmaps. private static final int touch_tolerance_dp = 24; private static final int background =0xffcc00 ; private list<point> mpoints = new arraylist<point>(); private int mlastpointindex = 0; private int mtouchtolerance; private boolean ispathstarted = false; public paintview(context context) { super(context); mcanvas = new canvas(); mpath = new path(); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); mtouchtolerance = dp2px(touch_tolerance_dp); // todo test points point p1 = new point(20, 20); point p2 = new point(100, 100); point p3 = new point(200, 250); point p4 = new point(280, 400); point p5 = new point(350, 600); point p6 = new point(400, 500); point p7 = new point(450, 500); mpoints.add(p1); mpoints.add(p2); mpoints.add(p3); mpoints.add(p4); mpoints.add(p5); mpoints.add(p6); mpoints.add(p7); } public paintview(context context, attributeset attrs) { super(context, attrs); mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.polacz); mcanvas = new canvas(); mpath = new path(); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); mtouchtolerance = dp2px(touch_tolerance_dp); // todo test points point p1 = new point(20, 20); point p2 = new point(100, 100); point p3 = new point(200, 250); point p4 = new point(280, 400); point p5 = new point(350, 600); point p6 = new point(400, 500); point p7 = new point(450, 500); mpoints.add(p1); mpoints.add(p2); mpoints.add(p3); mpoints.add(p4); mpoints.add(p5); mpoints.add(p6); mpoints.add(p7); } public paintview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub mcanvas = new canvas(); mpath = new path(); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); mtouchtolerance = dp2px(touch_tolerance_dp); } @override protected void onsizechanged(int width, int height, int oldwidth, int oldheight) { super.onsizechanged(width, height, oldwidth, oldheight); clear(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); //canvas.drawcolor(background); drawable d = getresources().getdrawable(r.drawable.polacz); d.setbounds(canvas.getclipbounds()); d.draw(canvas); // mcustomimage.setbounds(canvas.getclipbounds()); // mcustomimage.draw(canvas); canvas.drawbitmap(mbitmap, 0, 0, null); // canvas.drawbitmap(bitmapfactory.decoderesource(getresources(), r.drawable.polacz1), 0, 0, null); canvas.drawpath(mpath, mpaint); // todo remove if dont want points drawn (point point : mpoints) { canvas.drawpoint(point.x, point.y, mpaint); } } @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: touch_start(x, y); invalidate(); break; case motionevent.action_move: touch_move(x, y); invalidate(); break; case motionevent.action_up: touch_up(x, y); invalidate(); break; } return true; } private void touch_start(float x, float y) { if (checkpoint(x, y, mlastpointindex)) { mpath.reset(); // user starts given point path can beis started ispathstarted = true; } else { // user starts move point doen's belongs mpinst list ispathstarted = false; } } private void touch_move(float x, float y) { // draw line finger move if (ispathstarted) { mpath.reset(); point p = mpoints.get(mlastpointindex); mpath.moveto(p.x, p.y); if (checkpoint(x, y, mlastpointindex + 1)) { p = mpoints.get(mlastpointindex + 1); mpath.lineto(p.x, p.y); mcanvas.drawpath(mpath, mpaint); mpath.reset(); ++mlastpointindex; } else { mpath.lineto(x, y); } } } /** * draws line. */ private void touch_up(float x, float y) { mpath.reset(); if (checkpoint(x, y, mlastpointindex + 1) && ispathstarted) { // move finished @ valid point draw whole line // start point point p = mpoints.get(mlastpointindex); mpath.moveto(p.x, p.y); // end point p = mpoints.get(mlastpointindex + 1); mpath.lineto(p.x, p.y); mcanvas.drawpath(mpath, mpaint); mpath.reset(); // increment point index ++mlastpointindex; ispathstarted = false; } } /** * sets paint * * @param paint */ public void setpaint(paint paint) { this.mpaint = paint; } /** * returns image bitmap * * @return */ public bitmap getbitmap() { return mbitmap; } /** * clears canvas */ public void clear() { mbitmap = bitmap.createbitmap(getwidth(), getheight(), bitmap.config.argb_8888); mbitmap.erasecolor(background); mcanvas.setbitmap(mbitmap); invalidate(); } /** * checks if user touch point tolerance */ private boolean checkpoint(float x, float y, int pointindex) { if (pointindex == mpoints.size()) { // out of bounds return false; } point point = mpoints.get(pointindex); //edit changed point.y poin.x in first if statement if (x > (point.x - mtouchtolerance) && x < (point.x + mtouchtolerance)) { if (y > (point.y - mtouchtolerance) && y < (point.y + mtouchtolerance)) { return true; } } return false; } public list<point> getpoints() { return mpoints; } public void setpoints(list<point> points) { this.mpoints = points; } private int dp2px(int dp) { resources r = getcontext().getresources(); float px = typedvalue.applydimension(typedvalue.complex_unit_dip, dp, r.getdisplaymetrics()); return (int) px; } }
xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" android:focusableintouchmode="false"> <com.example.grzegorz.kropy.paintview android:layout_width="670dp" android:layout_height="820dp" /> <!--<button--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"--> <!--android:text="graj"--> <!--android:id="@+id/button"--> <!--android:layout_alignparenttop="true"--> <!--android:layout_alignparentleft="true"--> <!--android:onclick="gra"--> <!--android:layout_alignparentstart="true" />--> </relativelayout>
public class drawingpanel extends view implements ontouchlistener { private canvas mcanvas; private path mpath; private paint mpaint, mbitmappaint; private arraylist<pathpoints> paths = new arraylist<pathpoints>(); private arraylist<pathpoints> undonepaths = new arraylist<pathpoints>(); private bitmap mbitmap; private int color; public int ssl = color.white; private int x, y; private int strockwidth = 3; private string texttodraw = null; public static boolean istextmodeon = false; //public static boolean flag = true; //public static int angle = 0; public drawingpanel(context context, int color, bitmap bitmap) { super(context); this.color = color; system.out.println("---> drawing pannel construction <--- "); setfocusable(true); setfocusableintouchmode(true); this.setontouchlistener(this); mbitmap = bitmap; mbitmappaint = new paint(paint.dither_flag); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(strockwidth); mpaint.settextsize(30); mpath = new path(); paths.add(new pathpoints(mpath, color,strockwidth, false)); mcanvas = new canvas(); } public void colorchanged(int color) { this.color = color; mpaint.setcolor(color); //invalidate(); } public void changewidthofstrock(int strockwidth) { this.strockwidth = strockwidth; mpaint.setstrokewidth(strockwidth); //invalidate(); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); // mbitmap = addreportitemactivity.mphoto; //mbitmap = getintent().getextras().getparcelable("bitmap"); system.out.println("mbitmap"+mbitmap); if(mbitmap != null) { float xscale = (float) w / (float) mbitmap.getwidth(); float yscale = (float) h / (float) mbitmap.getheight(); if (xscale > yscale) // make sure both dimensions fit (use xscale = yscale; float newx = (float) w * xscale; float newy = (float) h * xscale; // use same scale both // dimensions } // if want centered on display (black borders) mbitmap = bitmap.createscaledbitmap(mbitmap, this.getwidth(),this.getheight(), true); // mcanvas = new canvas(mbitmap); } @override protected void ondraw(canvas canvas) { canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); (pathpoints p : paths) { mpaint.setcolor(p.getcolor()); mpaint.setstrokewidth(p.getstrockwidth()); if (p.istexttodraw()) { //canvas.drawtext(p.texttodraw, p.x, p.y, mpaint); } else { canvas.drawpath(p.getpath(), mpaint); } } mpaint.setcolor(ssl); mpaint.setstrokewidth(strockwidth); canvas.drawpath(mpath, mpaint); } private float mx, my; private static final float touch_tolerance = 0; private void touch_start(float x, float y) { mpath.reset(); mpath.moveto(x, y); mx = x; = y; } private void touch_move(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance) { mpath.quadto(mx, my, (x + mx) / 2, (y + my) / 2); mx = x; = y; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw //mpath = new path(); paths.add(new pathpoints(mpath, ssl,strockwidth ,false)); mpath = new path(); } private void drawtext(int x, int y) { //log.v(tag, "here"); //log.v(tag, "x " + x + " y " + y); this.x = x; this.y = y; paths.add(new pathpoints(color, texttodraw, true, x, y)); // mcanvas.drawtext(texttodraw, x, y, mpaint); } @override public boolean ontouch(view arg0, motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: if (!istextmodeon) { touch_start(x, y); invalidate(); } break; case motionevent.action_move: if (!istextmodeon) { touch_move(x, y); invalidate(); } break; case motionevent.action_up: if (istextmodeon) { drawtext((int) x, (int) y); invalidate(); } else { touch_up(); invalidate(); } break; } return true; } public void onclickundo() { if (paths.size() > 0) { undonepaths.add(paths.remove(paths.size() - 1)); invalidate(); } else { } // toast user } public void onclickredo() { if (undonepaths.size() > 0) { paths.add(undonepaths.remove(undonepaths.size() - 1)); invalidate(); } else { } // toast user } class pathpoints { private path path; // private paint mpaint; private int color; private int strockw; private string texttodraw; private boolean istexttodraw; private int x, y; public pathpoints(path path, int color,int strockwidth ,boolean istexttodraw) { this.path = path; this.color = color; this.strockw = strockwidth; this.istexttodraw = istexttodraw; } public pathpoints(int color, string texttodraw, boolean istexttodraw,int x, int y) { this.color = color; this.texttodraw = texttodraw; this.istexttodraw = istexttodraw; this.x = x; this.y = y; } public path getpath() { return path; } public void setpath(path path) { this.path = path; } /* * private paint getpaint() { mpaint = new paint(); * mpaint.setantialias(true); mpaint.setcolor(color); * mpaint.setstyle(paint.style.stroke); * mpaint.setstrokejoin(paint.join.round); * mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(6); * return mpaint; } */ public int getcolor() { return color; } public void setcolor(int color) { this.color = color; } public int getstrockwidth() { return strockw; } public void setstrokwidth(int color) { this.strockw = color; } public string gettexttodraw() { return texttodraw; } public void settexttodraw(string texttodraw) { this.texttodraw = texttodraw; } public boolean istexttodraw() { return istexttodraw; } public void settexttodraw(boolean istexttodraw) { this.istexttodraw = istexttodraw; } public int getx() { return x; } public void setx(int x) { this.x = x; } public int gety() { return y; } public void sety(int y) { this.y = y; } }
}
add view layout
drawingpanel drawingpanel = new drawingpanel(getapplicationcontext(), color, bitmap); rl_layout.addview(drawingpanel);
Comments
Post a Comment