android - Draw paint over image not blank space using canvas -


i trying draw line on image, works fine faced problem. problem want draw line on image only, not blank space. code here sizechanged method declare this.

class drawingpaint extends view implements view.ontouchlistener {  private canvas mcanvas; private path mpath; private paint mpaint, mbitmappaint; public arraylist<pathpoints> paths = new arraylist<pathpoints>(); private arraylist<pathpoints> undonepaths = new arraylist<pathpoints>(); private bitmap mbitmap; private int color; private int x, y; private string texttodraw = null; private boolean istextmodeon = false; int lastcolor = 0xffff0000; static final float stroke_width = 15f; matrix m; int imageheight,imagewidth;  public drawingpaint(context context/*, int color*/) {     super(context);     //this.color = color;     setfocusable(true);     setfocusableintouchmode(true);      this.setontouchlistener(this);      mbitmappaint = new paint(paint.dither_flag);     /*mpaint = new paint();     mpaint.setantialias(true);     mpaint.setdither(true);     mpaint.setstyle(paint.style.stroke);     mpaint.setstrokejoin(paint.join.round);     mpaint.setstrokecap(paint.cap.round);     mpaint.setstrokewidth(stroke_width);     mpaint.settextsize(30);      mpath = new path();     paths.add(new pathpoints(mpath, color, false));     mcanvas = new canvas();*/ }  public void colorchanged(int color) {     this.color = color;     mpaint.setcolor(color); }  public void setcolor(int color) {      mpaint = new paint();     this.color = color;     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(stroke_width);     mpaint.settextsize(30);     mpath = new path();    paths.add(new pathpoints(mpath, color, false));    mcanvas = new canvas();  }  @override protected void onsizechanged(int w, int h, int oldw, int oldh) {     super.onsizechanged(w, h, oldw, oldh);     // mbitmap = addreportitemactivity.mphoto;     mbitmap = customgalleryhandler.getminstance().getbitmapsend();     float xscale = (float) w / (float) mbitmap.getwidth();     float yscale = (float) h / (float) mbitmap.getheight();     if (xscale > yscale) // make sure both dimensions fit (use         // smaller scale)         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);      bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     bitmapfactory.decodefile(new file(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()).getabsolutepath(), options);     imageheight = options.outheight;     imagewidth = options.outwidth;       m = new matrix();     rectf drawablerect = new rectf(0, 0, imagewidth, imageheight);     //onmeasure(imagewidth,imageheight);     rectf viewrect = new rectf(0, 0, w, h);     m.setrecttorect(drawablerect, viewrect, matrix.scaletofit.center);      mbitmap = bitmap.createbitmap(mbitmap,0,0,imagewidth,             imageheight, m,true);  }  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     int width = measurespec.getsize(widthmeasurespec);     int height = measurespec.getsize(heightmeasurespec);      setmeasureddimension(width, height);     //setmeasureddimension(this.getwidth(), this.getheight());     //setmeasureddimension(560, 100);even though give ensured size, can't //anyway. }  @override protected void ondraw(canvas canvas) {     float[] pts = {0, 0};     m.mappoints(pts);     canvas.drawbitmap(mbitmap, pts[0], pts[1], mbitmappaint);    // canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint);     (pathpoints p : paths) {         mpaint.setcolor(p.getcolor());         log.v("", "color code : " + p.getcolor());         if (p.istexttodraw()) {             canvas.drawtext(p.texttodraw, p.x, p.y, mpaint);         } else {             canvas.drawpath(p.getpath(), 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, color, false));  }  private void drawtext(int x, int 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() {     try {         if (paths.size() > 0) {             undonepaths.add(paths.remove(paths.size() - 1));             invalidate();         } else {          }     } catch (exception e) {         e.tostring();     }  }  public void onclickredo() {     try {         if (undonepaths.size() > 0) {             paths.add(undonepaths.remove(undonepaths.size() - 1));             invalidate();         } else {          }     } catch (exception e) {         e.tostring();     } }   /*class pathpoints {     private path path;     // private paint mpaint;     private int color;     private string texttodraw;     private boolean istexttodraw;     private int x, y;      public pathpoints(path path, int color, boolean istexttodraw) {         this.path = path;         this.color = color;         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;     }      public int getcolor() {         return color;     }      public void setcolor(int color) {         this.color = 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;     }  }*/ 

}

perhaps you'd want extend customized view imageview self.

public class line extends imageview {  private static final string tag = "line"; private paint linedraw = null;  private int mstartx = 0; private int mstarty = 0; private int mendx = 0; private int mendy = 0; private float mstrokewidth = 0;  public line(context context) {     super(context);      init_paint(); }  public line(context context, attributeset attrs) {     super(context, attrs);      init_attributes(context, attrs);      init_paint(); }  public line(context context, attributeset attrs, int defstyleattr) {     super(context, attrs, defstyleattr);      init_attributes(context, attrs);      init_paint(); }  private void init_paint() {     linedraw = new paint(paint.anti_alias_flag);      linedraw.setstyle(paint.style.fill);     linedraw.setcolor(color.black);     linedraw.setstrokewidth(mstrokewidth); }  private void init_attributes(context context, attributeset attrs) {     typedarray = context.obtainstyledattributes(attrs, r.styleable.line);     mstartx = a.getinteger(r.styleable.line_startx, 0);     mstarty = a.getinteger(r.styleable.line_starty, 0);     mendx = a.getinteger(r.styleable.line_endx, 0);     mendy = a.getinteger(r.styleable.line_endy, 0);      mstrokewidth = a.getdimension(r.styleable.line_strokewidth, 0);      a.recycle(); }  public void setstartx(int startx) {     this.mstartx = startx; }  public void setstarty(int starty) {     this.mstarty = starty; }  public void setendx(int endx) {     this.mendx = endx; }  public void setendy(int endy) {     this.mendy = endy; }  public void setstrokewidth(float strokewidth) {     this.mstrokewidth = strokewidth; }  @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) {     int width = measurespec.getsize(widthmeasurespec);     int height = measurespec.getsize(heightmeasurespec);      setmeasureddimension(width, height); }  @override public void ondraw(canvas canvas) {     //call imageview's default ondraw() method.     super.ondraw(canvas);      // can instead use canvas.drawpath();     canvas.drawline(mstartx, mstarty, mendx, mendy, linedraw); } } 

in attr.xml add following.

<resources>     <declare-styleable name="line">         <attr name="startx" format="integer" />         <attr name="endx" format="integer" />         <attr name="starty" format="integer" />         <attr name="endy" format="integer" />         <attr name="strokewidth" format="dimension" />     </declare-styleable> </resources> 

and can use so.

<views.customview.line     android:layout_width="wrap_content"     android:layout_height="wrap_content"     app:startx="5"     app:starty="5"     app:endx="500"     app:endy="500"     app:strokewidth="15dp"     android:src="@drawable/add_bookmark"     /> 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -