Why won't my undo button work in android java? -


i have image user can paint on in android application. decided add undo button user can cancel mistakes. made when user draws on screen, saves bitmap array. when undo button pressed, changes image last bitmap in array. however, keeps setting image whatever current image (i.e. doesn't change @ all).

  public class edit extends appcompatactivity implements view.onclicklistener { float movex,movey,downy,downx,upy=0,upx ; list<bitmap> undos = new arraylist<bitmap>(); @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_edit);    final  imagebutton btn2 = (imagebutton) findviewbyid(r.id.imagebutton2);     btn2.setonclicklistener(this);     final button buttonchoose = (button) findviewbyid(r.id.buttonchoose);     buttonchoose.setonclicklistener(this);     final relativelayout viewx = (relativelayout) findviewbyid(r.id.rlxv);     viewtreeobserver vto = viewx.getviewtreeobserver();     vto.addongloballayoutlistener(new ongloballayoutlistener() {         @override         public void ongloballayout() {             viewx.getviewtreeobserver().removeongloballayoutlistener(this);              intent intent7 = getintent();             string bitmapxy = (string) intent7.getextras().get("bitmapimage");             graphrequest request = graphrequest.newgraphpathrequest(                     accesstoken.getcurrentaccesstoken(),                     "/" + bitmapxy,                     new graphrequest.callback() {                         @override                         public void oncompleted(graphresponse response) {                             httpurlconnection connection = null;                             try {                                 int viewwidth = viewx.getmeasuredwidth();                                 jsonobject photos = response.getjsonobject();                                 jsonarray linky = photos.optjsonarray("images");                                 final jsonobject linko = linky.optjsonobject(0);                                 final string link2 = linko.optstring("source");                                 url linkf = new url(link2);                                 connection = (httpurlconnection) linkf.openconnection();                                 connection.setdoinput(true);                                 connection.connect();                                 inputstream input = connection.getinputstream();                                 bitmap bitmap = bitmapfactory.decodestream(input);                                 float hh = (float) bitmap.getheight();                                 float ww = (float) bitmap.getwidth();                                 float viewwidthx = (float) viewwidth;                                 float height = (hh / ww) * viewwidthx;                                  bitmap bitmapf = bitmap.createscaledbitmap(bitmap, viewwidth, math.round(height), false);                                final  imageview image1 = (imageview) findviewbyid(r.id.image1);                                 image1.setimagebitmap(bitmapf);     undos.add(bitmapf);                                 image1.setontouchlistener(new view.ontouchlistener() {                                     @override                                     public boolean ontouch(view v, motionevent event) {  display d = getwindowmanager().getdefaultdisplay();                                          float dw = d.getwidth();                                         float dh = d.getheight();                                         float x = event.getx();                                          float y = event.gety();       float r = 2;                                          bitmap btx = ((bitmapdrawable)image1.getdrawable()).getbitmap();                                         canvas canvas = new canvas(btx);                                         canvas.drawbitmap(btx,0,0,null);                                         paint paint1 = new paint();                                       int bg1 =  buttonchoose.getdrawingcachebackgroundcolor();                                         colordrawable buttoncolor = (colordrawable) buttonchoose.getbackground();                                         int bg2 =buttoncolor.getcolor();                                         paint1.setcolor(bg2);                                         paint1.setshadowlayer(5, 2, 2, bg2);                                         paint1.setstrokewidth(20);           switch(event.getaction()){  case motionevent.action_down:  downy = event.gety();   downx = event.getx();     break; case motionevent.action_move:     movey = event.gety(); movex = event.getx();     canvas.drawline(downx, downy, movex, movey, paint1);     image1.invalidate();     downx = movex;     downy=movey;      break; case motionevent.action_up:     upy = event.gety();     upx = event.getx();     canvas.drawline(downx, downy, upx, upy, paint1);     image1.invalidate();     bitmap btxyz = ((bitmapdrawable)image1.getdrawable()).getbitmap();      break;   }                                          return true;                                     }                                 });                             } catch (ioexception e) {                                 e.printstacktrace();                             }                         }                     });              bundle parameters = new bundle();             parameters.putstring("fields", "images");             request.setparameters(parameters);             request.executeasync();               }     });  }       @override public void onclick(view v) {   switch(v.getid()) {          case r.id.buttonchoose:         final colorpicker cp = new colorpicker(edit.this, 0, 0, 0);      /* show color picker dialog */         cp.show();  /* on click listener dialog, when user select color */         button okcolor = (button) cp.findviewbyid(r.id.okcolorbutton);         okcolor.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {              /* can single channel (value 0-255) */                 int selectedcolorr = cp.getred();                 int selectedcolorg = cp.getgreen();                 int selectedcolorb = cp.getblue();              /* or android rgb color (see android color class reference) */                 int selectedcolorrgb = cp.getcolor();                 button buttonchoose = (button) findviewbyid(r.id.buttonchoose);                 buttonchoose.settext("");                 buttonchoose.setbackgroundcolor(selectedcolorrgb);                 cp.dismiss();             }         });         break;       case r.id.imagebutton2:           int newimage = undos.size();           log.e("lenght of array",""+newimage);           if(newimage >=1) {               bitmap newimage2 = undos.get(newimage-2);               imageview imgxv = (imageview) findviewbyid(r.id.image1);               imgxv.setimagebitmap(newimage2);                undos.remove(newimage-1);           }           break;     } }   } 

try exact code -

case r.id.imagebutton2:        if(undos.size() > 0) {           bitmap newimage2 = undos.remove(undos.size() - 1);           imageview imgxv = (imageview) findviewbyid(r.id.image1);           imgxv.setimagebitmap(newimage2);       }       break; 

edit added line in ongloballayout(), try -

bitmap bitmapf = bitmap.createscaledbitmap(bitmap, viewwidth, math.round(height), false); final  imageview image1 = (imageview) findviewbyid(r.id.image1); image1.setimagebitmap(bitmapf); //the line before adding bitmap arraylist. bitmap bitmapfcopy = bitmapfcopy.copy(bitmapfcopy.getconfig(), true); undos.add(bitmapfcopy); 

edit 2

public class edit extends appcompatactivity implements view.onclicklistener {  //declare imageview here , access everywhere. imageview image1; float movex,movey,downy,downx,upy=0,upx ; list<bitmap> undos = new arraylist<bitmap>(); @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_edit);     //get reference here    image1 = (imageview) findviewbyid(r.id.image1);    final  imagebutton btn2 = (imagebutton) findviewbyid(r.id.imagebutton2);     btn2.setonclicklistener(this);     final button buttonchoose = (button) findviewbyid(r.id.buttonchoose);     buttonchoose.setonclicklistener(this);     final relativelayout viewx = (relativelayout) findviewbyid(r.id.rlxv);     viewtreeobserver vto = viewx.getviewtreeobserver();     vto.addongloballayoutlistener(new ongloballayoutlistener() {         @override         public void ongloballayout() {             viewx.getviewtreeobserver().removeongloballayoutlistener(this);              intent intent7 = getintent();             string bitmapxy = (string) intent7.getextras().get("bitmapimage");             graphrequest request = graphrequest.newgraphpathrequest(                     accesstoken.getcurrentaccesstoken(),                     "/" + bitmapxy,                     new graphrequest.callback() {                         @override                         public void oncompleted(graphresponse response) {                             httpurlconnection connection = null;                             try {                                 int viewwidth = viewx.getmeasuredwidth();                                 jsonobject photos = response.getjsonobject();                                 jsonarray linky = photos.optjsonarray("images");                                 final jsonobject linko = linky.optjsonobject(0);                                 final string link2 = linko.optstring("source");                                 url linkf = new url(link2);                                 connection = (httpurlconnection) linkf.openconnection();                                 connection.setdoinput(true);                                 connection.connect();                                 inputstream input = connection.getinputstream();                                 bitmap bitmap = bitmapfactory.decodestream(input);                                 float hh = (float) bitmap.getheight();                                 float ww = (float) bitmap.getwidth();                                 float viewwidthx = (float) viewwidth;                                 float height = (hh / ww) * viewwidthx;                                  bitmap bitmapf = bitmap.createscaledbitmap(bitmap, viewwidth, math.round(height), false);                                  //remove here , declare in oncreate() , don't make final                                                                    //final imageview image1 = (imageview) findviewbyid(r.id.image1);                                 image1.setimagebitmap(bitmapf);     undos.add(bitmapf);                                 image1.setontouchlistener(new view.ontouchlistener() {                                     @override                                     public boolean ontouch(view v, motionevent event) {  display d = getwindowmanager().getdefaultdisplay();                                          float dw = d.getwidth();                                         float dh = d.getheight();                                         float x = event.getx();                                          float y = event.gety();       float r = 2;                                          bitmap btx = ((bitmapdrawable)image1.getdrawable()).getbitmap();                                         canvas canvas = new canvas(btx);                                         canvas.drawbitmap(btx,0,0,null);                                         paint paint1 = new paint();                                       int bg1 =  buttonchoose.getdrawingcachebackgroundcolor();                                         colordrawable buttoncolor = (colordrawable) buttonchoose.getbackground();                                         int bg2 =buttoncolor.getcolor();                                         paint1.setcolor(bg2);                                         paint1.setshadowlayer(5, 2, 2, bg2);                                         paint1.setstrokewidth(20);           switch(event.getaction()){  case motionevent.action_down:  downy = event.gety();   downx = event.getx();     break; case motionevent.action_move:     movey = event.gety(); movex = event.getx();     canvas.drawline(downx, downy, movex, movey, paint1);     image1.invalidate();     downx = movex;     downy=movey;      break; case motionevent.action_up:     upy = event.gety();     upx = event.getx();     canvas.drawline(downx, downy, upx, upy, paint1);     image1.invalidate();     bitmap btxyz = ((bitmapdrawable)image1.getdrawable()).getbitmap();      break;   }                                          return true;                                     }                                 });                             } catch (ioexception e) {                                 e.printstacktrace();                             }                         }                     });              bundle parameters = new bundle();             parameters.putstring("fields", "images");             request.setparameters(parameters);             request.executeasync();               }     });  }       @override public void onclick(view v) {   switch(v.getid()) {          case r.id.buttonchoose:         final colorpicker cp = new colorpicker(edit.this, 0, 0, 0);      /* show color picker dialog */         cp.show();  /* on click listener dialog, when user select color */         button okcolor = (button) cp.findviewbyid(r.id.okcolorbutton);         okcolor.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {              /* can single channel (value 0-255) */                 int selectedcolorr = cp.getred();                 int selectedcolorg = cp.getgreen();                 int selectedcolorb = cp.getblue();              /* or android rgb color (see android color class reference) */                 int selectedcolorrgb = cp.getcolor();                 button buttonchoose = (button) findviewbyid(r.id.buttonchoose);                 buttonchoose.settext("");                 buttonchoose.setbackgroundcolor(selectedcolorrgb);                 cp.dismiss();             }         });         break;       case r.id.imagebutton2:           int newimage = undos.size();           log.e("lenght of array",""+newimage);           if(newimage >=1) {               bitmap newimage2 = undos.get(newimage-2);               //don't take reference again here               //imageview imgxv = (imageview) findviewbyid(r.id.image1);               image1.setimagebitmap(newimage2);                undos.remove(newimage-1);           }           break;     } }   } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -