Android Canvas update line coords -
i have drawn line starting @ point (always zero) , ending @ point b using canvas.drawline(xstart, ystart, xend, yend);
but point b draggable, want update canvas line xend , yend b position when dragged. problems when update line coords thorows error 1 line explanation:
"fatal signal 11 (sigsegv), code 1, fault addr 0x10c in tid 5638 (xample.launcher)" says xample.launcher packagename com.example.launcher, don't know if problem there
here goes code.
drawview (point b)
import android.app.activity; import android.view.view; public class drawview { string label; int color, direction, x, y; view view; drawpath path; public drawview(){ } public drawview(string label, int color, int direction, int x, int y, view view){ this.label = label; this.color = color; this.direction = direction; this.x = x; this.y = y; this.view = view; } public string getlabel() { return label; } public void setlabel(string label) { this.label = label; } public int getcolor() { return color; } public void setcolor(int color) { this.color = color; } public int getdirection() { return direction; } public void setdirection(int direction) { this.direction = direction; } 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; } public view getview() { return view; } public void setview(view view) { this.view = view; } public drawpath getpath() { return path; } public void setpath(drawpath path) { this.path = path; } }
drawpath (line between point point b)
package com.example.launcher; import android.content.context; import android.graphics.canvas; import android.graphics.paint; import android.util.log; import android.view.view; public class drawpath extends view{ int color, x, y; canvas canvas; paint paint; public drawpath(context context) { super(context); // todo auto-generated constructor stub } @override protected void ondraw(canvas canvas) { // todo auto-generated method stub super.ondraw(canvas); this.canvas = canvas; paint = new paint(); paint.setcolor(color); paint.setstrokewidth(10); log.e("this x, y", this.x + " " + this.y); canvas.drawline(0, 0, this.x, this.y, paint); invalidate(); } public void setcolor(int color){ this.color = color; } public int getcolor(){ return this.color; } public void setxvalue(int x){ this.x = x; if(canvas != null){ canvas.drawline(0, 0, this.x, this.y, paint); invalidate(); } } public float getxvalue(){ return this.x; } public void setyvalue(int y){ this.y = y; if(canvas != null){ canvas.drawline(0, 0, this.x, this.y, paint); invalidate(); } } public float getyvalue(){ return this.y; } }
and ontouch event
@override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub relativelayout.layoutparams params = (relativelayout.layoutparams)myview.getlayoutparams(); final int x = (int)event.getrawx(); final int y = (int)event.getrawy(); switch(event.getaction()){ case motionevent.action_down: xdelta = x - (draw.getx()); ydelta = y - (draw.gety()); break; case motionevent.action_move: params.setmargins((x - xdelta), (y - ydelta), 0, 0); draw.getpath().setxvalue((x - xdelta)); draw.getpath().setyvalue((y - ydelta)); myview.setlayoutparams(params); break; } return true; }
Comments
Post a Comment