android - How can I erase my objects in the drawing application without changing the Background Image? -


i want erase bitmap don't want erase background image. when try erase white , draw hard in frames.

this mainactivity code

    private final string tag = "mainactivity";      private imageview eraser;     private button btnchooseimage;     private imagebutton btnclear, btnsave, btnshare, btncamera;      private drawingview drawingview;      private static final int select_photo = 100;     private static final int camera_request = 1888;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          if (savedinstancestate == null) {              setcontentview(r.layout.activity_main);              drawingview = (drawingview) findviewbyid(r.id.drawing);              btnchooseimage = (button) findviewbyid(r.id.btnchooseimage);             btnchooseimage.setonclicklistener(this);              btnclear = (imagebutton) findviewbyid(r.id.btnclear);             btnclear.setonclicklistener(this);              btnsave = (imagebutton) findviewbyid(r.id.btnsave);             btnsave.setonclicklistener(this);              btnshare = (imagebutton) findviewbyid(r.id.btnshare);             btnshare.setonclicklistener(this);              btncamera = (imagebutton) findviewbyid(r.id.btncamera);             btncamera.setonclicklistener(this);              eraser = (imageview) findviewbyid(r.id.eraser);             eraser.setonclicklistener(this);         }     }          @suppresslint("newapi") @override     public void onclick(view v) {          if (v == eraser) {              if (drawingview.iseraseractive()) {                  drawingview.deactivateeraser();                  eraser.setimageresource(r.drawable.eraser);              } else {                  drawingview.activateeraser();                  eraser.setimageresource(r.drawable.pencil);             }          } else if (v == btnclear) {              drawingview.reset();             drawingview.setbackground(null);          } else if (v == btnsave) {              saveimage();          } else if (v == btncamera) {              intent cameraintent = new intent(android.provider.mediastore.action_image_capture);             startactivityforresult(cameraintent, camera_request);          } else if (v == btnshare) {              intent share = new intent(intent.action_send);             share.settype("image/png");              share.putextra(intent.extra_stream, uri.parse(saveimage().getabsolutepath())); //"file:///sdcard/temporary_file.jpg"             startactivity(intent.createchooser(share, "share image"));          } else if (v == btnchooseimage) {              intent photopickerintent = new intent(intent.action_pick);             photopickerintent.settype("image/*");             startactivityforresult(photopickerintent, select_photo);          }      }       public file saveimage() {         drawingview.setdrawingcacheenabled(true);         bitmap bm = drawingview.getdrawingcache();          file fpath = environment.getexternalstoragedirectory();          file f = null;          f = new file(fpath, uuid.randomuuid().tostring() + ".png");          try {             fileoutputstream strm = new fileoutputstream(f);             bm.compress(bitmap.compressformat.png, 80, strm);             strm.close();              toast.maketext(getapplicationcontext(), "image saved successfully.", toast.length_short).show();         } catch (ioexception e) {             e.printstacktrace();         }          return f;     }      @suppresslint("newapi") @suppresswarnings("deprecation")     @override     protected void onactivityresult(int requestcode, int resultcode, intent imagereturnedintent) {         super.onactivityresult(requestcode, resultcode, imagereturnedintent);          switch (requestcode) {              case select_photo:                 if (resultcode == result_ok) {                     uri selectedimage = imagereturnedintent.getdata();                     inputstream imagestream = null;                     try {                         imagestream = getcontentresolver().openinputstream(selectedimage);                         bitmap bitmap = bitmapfactory.decodestream(imagestream);                          bitmapdrawable ob = new bitmapdrawable(getresources(), bitmap);                          if(build.version.sdk_int >= 16)                         {                             drawingview.setbackground(ob);                         }else {                             drawingview.setbackgrounddrawable(ob);                         }                      } catch (filenotfoundexception e) {                         e.printstacktrace();                     }                 }                 break;             case camera_request:                 if (resultcode == result_ok) {                      bitmap photo = (bitmap) imagereturnedintent.getextras().get("data");                      bitmapdrawable ob = new bitmapdrawable(getresources(), photo);                      if(build.version.sdk_int >= 16)                     {                         drawingview.setbackground(ob);                     }else {                         drawingview.setbackgrounddrawable(ob);                     }                 }         }     }   } 

and here drawingview code

private canvas m_canvas;      private path m_path;      private paint m_paint;      private arraylist<pair<path, paint>> paths = new arraylist<pair<path, paint>>();      private arraylist<pair<path, paint>> undonepaths = new arraylist<pair<path, paint>>();      private float mx, my;      private static final float touch_tolerance = 0.001f;      private boolean iseraseractive = false;      public drawingview(context context, attributeset attr) {         super(context, attr);          setfocusable(true);          setfocusableintouchmode(true);          setbackgroundcolor(color.white);          this.setontouchlistener(this);          oncanvasinitialization();     }      public void oncanvasinitialization() {          m_paint = new paint();         m_paint.setantialias(true);         m_paint.setdither(true);         m_paint.setcolor(color.parsecolor("#000000"));          m_paint.setstyle(paint.style.stroke);         m_paint.setstrokejoin(paint.join.round);         m_paint.setstrokecap(paint.cap.round);         m_paint.setstrokewidth(2);          m_canvas = new canvas();           m_path = new path();         paint newpaint = new paint(m_paint);         paths.add(new pair<path, paint>(m_path, newpaint));      }      @override     protected void onsizechanged(int w, int h, int oldw, int oldh) {         super.onsizechanged(w, h, oldw, oldh);     }      public boolean ontouch(view arg0, 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();             invalidate();             break;         }         return true;     }      @override     protected void ondraw(canvas canvas) {         (pair<path, paint> p : paths) {             canvas.drawpath(p.first, p.second);         }     }      private void touch_start(float x, float y) {          if (iseraseractive) {             m_paint.setcolor(color.white);             m_paint.setstrokewidth(10);             paint newpaint = new paint(m_paint); // clones mpaint object             paths.add(new pair<path, paint>(m_path, newpaint));          } else {              m_paint.setcolor(color.black);             m_paint.setstrokewidth(2);             paint newpaint = new paint(m_paint); // clones mpaint object             paths.add(new pair<path, paint>(m_path, newpaint));          }           m_path.reset();         m_path.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) {             m_path.quadto(mx, my, (x + mx) / 2, (y + my) / 2);             mx = x;             = y;         }     }      private void touch_up() {         m_path.lineto(mx, my);          // commit path our offscreen         m_canvas.drawpath(m_path, m_paint);          // kill don't double draw         m_path = new path();         paint newpaint = new paint(m_paint); // clones mpaint object         paths.add(new pair<path, paint>(m_path, newpaint));     }      public void activateeraser()     {         iseraseractive = true;     }       public void deactivateeraser()     {         iseraseractive = false;     }      public boolean iseraseractive()     {         return iseraseractive;     }       public void reset()     {         paths.clear();          invalidate(); } } 

i wanted erase drawing view without changing background image imported gallery

make 1 function in view class , clear list views have saved before , redraw view like:

public void cleardrawing() {   paths.clear();   invalidate(); } 

invalidate() whill refresh view. call method view object on click event in activity. hope help.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -