image - How to rotate a bitmap in android when click a button? -


i having problem when rotate bitmap. requirement follows,

  1. capture image through camera , display capture image in imageview.
  2. when click/touch image, show 2 buttons on imageview. 1 button rotate image respect clockwise (45 degree) direction , 1 button rotate image respect anti-clockwise direction.

    i used below code process, unfortunately works once time, when click button @ first time ti rotate image corresponding 45 degree after there no change in button click. // onclick

    public void onclick(view v) {

        switch (v.getid()) {      case r.id.right_image_rotate:          try {             system.gc();             runtime.getruntime().gc();             unbinddrawables(findviewbyid(r.id.image_viewer));             imagerotate();         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }         break;      case r.id.left_image_rotate:          try {             system.gc();             runtime.getruntime().gc();             unbinddrawables(findviewbyid(r.id.image_viewer));             imagerotate();         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }         break;     case r.id.image_viewer:         rotatelayout.setvisibility(view.visible);         imageleft_rotate.setvisibility(view.visible);         imageright_rotate.setvisibility(view.visible);         break;     default:         break;     } }        bitmap bitmaporg = bitmapfactory.decodefile(saved_image_file             .getabsolutepath());      int width = bitmaporg.getwidth();      int height = bitmaporg.getheight();      int newwidth = 200;      int newheight = 200;      // calculate scale - in case = 0.4f      float scalewidth = ((float) newwidth) / width;      float scaleheight = ((float) newheight) / height;      matrix matrix = new matrix();      matrix.postscale(scalewidth, scaleheight);     matrix.postrotate(45);      bitmap resizedbitmap = bitmap.createbitmap(bitmaporg, 0, 0, width,             height, matrix, true);      srcbitmap.setscaletype(scaletype.center);     srcbitmap.setimagebitmap(resizedbitmap); 

it's because each time click button, you're still referencing original image, not 1 changed. if want continue rotating or modifying changed image, you'll have reference it; not original.

just make global variable , store changed bitmap in there. when button pressed, check if it's null or not. if null, use original; else use changed one


Comments