draw and scale custom rectangle on a view in android -


i want custom view,it can include effect:first draw pic on view background draw rectangle on image,and draw other image named in rectangle, know rectangle have 4 fixed point,when drag 1 of these,the rectangle can scale,at same time scale, have read more link,but not find example,i have done something,but cannot finish scale rectangle,my code is:

public class drawview extends view implements ontouchlistener { private static final string tag = "drawview";  private static final int linelength = 30;  paint paint = new paint(); float locationx, locationy; private int mlasttouchx; private int mlasttouchy;  private int mposx; private int mposy;  private int mposx1; private int mposy1; bitmap bitmap, bmp, xiao;   int screenwidth, screenheight;  int xlength;  boolean isfirst = true; boolean isleft = false;  rect r, rbig,outrect;  public drawview(context context) {     super(context);     setfocusable(true);     setfocusableintouchmode(true);     this.setontouchlistener(this);      windowmanager wm = (windowmanager) context             .getsystemservice(context.window_service);     screenheight = wm.getdefaultdisplay().getheight();     screenwidth = wm.getdefaultdisplay().getwidth();     mposx = screenwidth / 2;     mposy = screenheight / 2;      paint.setcolor(color.red);     paint.setantialias(true);     bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.meinv);     bmp = bitmapfactory.decoderesource(getresources(), r.drawable.icon);     xiao = bitmapfactory.decoderesource(getresources(),             r.drawable.msn_protocol);      xlength = (int) math.hypot(xiao.getwidth(), xiao.getheight());      r = new rect();     r.set((int) mposx - linelength - xiao.getwidth(), (int) mposy             - linelength - xiao.getheight(), (int) mposx - linelength,             (int) mposy - linelength);  //  log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);     rbig = new rect();     rbig.set((int) mposx - linelength, (int) mposy - linelength,             (int) mposx + linelength, (int) mposy + linelength);     //log.i("r", rbig.left + " " + rbig.top + " " + rbig.right + " " + rbig.bottom);    }  @override public void ondraw(canvas canvas) {        canvas.drawbitmap(bitmap, 0, 0, null);      canvas.drawbitmap(xiao, mposx - linelength - xiao.getwidth(), mposy             - linelength - xiao.getheight(), null);       canvas.drawline(mposx - linelength,             mposy - linelength - xiao.getheight() / 2, mposx + linelength,             mposy - linelength - xiao.getheight() / 2, paint);       canvas.drawline(mposx - linelength - xiao.getwidth() / 2, mposy             - linelength, mposx - linelength - xiao.getwidth() / 2, mposy             + linelength, paint);       canvas.drawbitmap(xiao, mposx + linelength,             mposy - linelength - xiao.getheight(), null);       canvas.drawbitmap(xiao, mposx - linelength - xiao.getwidth(), mposy             + linelength, null);       canvas.drawbitmap(xiao, mposx + linelength, mposy + linelength, null);       canvas.drawline(mposx + linelength + xiao.getwidth() / 2, mposy             - linelength, mposx + linelength + xiao.getwidth() / 2, mposy             + linelength, paint);       canvas.drawline(mposx - linelength,             mposy + linelength + xiao.getheight() / 2, mposx + linelength,             mposy + linelength + xiao.getheight() / 2, paint);       if (isleft) {            matrix matrix = new matrix();         matrix.prescale(0.8f, 0.8f);         bitmap rotatedbitmap = bitmap.createbitmap(bmp, 0, 0,                 bmp.getwidth(), bmp.getheight(), matrix, true);         canvas.drawbitmap(rotatedbitmap, mposx - linelength, mposy                 - linelength, null);       }  }  public boolean ontouch(view view, motionevent event) {      isfirst = false;     switch (event.getaction()) {     case motionevent.action_down: {         mposx1 = (int) event.getx();         mposy1 = (int) event.gety();         mlasttouchx = mposx1;         mlasttouchy = mposx1;       //  log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);         log.i("action_down", "" + mposx1 + " " + mposy1);         if (r.contains(mposx1, mposy1)) {             isleft = true;              invalidate();         }           break;     }      case motionevent.action_move: {           int x = (int) event.getx();          int y = (int) event.gety();          log.i("aa",""+x+""+y);              int dx = x - mlasttouchx;          int dy = y - mlasttouchy;          mlasttouchx = x;         mlasttouchy = y;          mposx += dx;         mposy += dy;          r.set((int) mposx - linelength - xiao.getwidth(), (int) mposy                 - linelength - xiao.getheight(), (int) mposx - linelength,                 (int) mposy - linelength);          rbig.set((int) mposx - linelength, (int) mposy - linelength,                 (int) mposx + linelength, (int) mposy + linelength);      //  log.i("r", rbig.left + " " + rbig.top + " " + rbig.right + " " + rbig.bottom);              invalidate();            break;     }      case motionevent.action_up: {          break;     }      case motionevent.action_cancel: {          break;     }      }     return true; }          } 

the pic :http://i.stack.imgur.com/wxi35.png,the effect have finished system gallery,but debug source ,i failed, other question: imitate crop function of system gallery , https://stackoverflow.com/questions/6724218/i-cannot-find-the-initial-value-in-gallery-the-source

i dont know drawing rectangle but, how moved image

mainpinchview.java

public class mainpinchview extends activity { int menuid = 0; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     mainpinchimageview obj = new mainpinchimageview(this);     bitmap bmp = bitmapfactory.decoderesource(getresources(), r.drawable.my);     obj.setimage(bmp, 100, 100);     setcontentview(obj);   } 

}

mainpinchimageview.java

   public class mainpinchimageview extends imageview {      private static final string tag = "touch";     // these matrices used move , zoom image     matrix matrix = new matrix();     matrix savedmatrix = new matrix();      int flag = 0;      // can in 1 of these 3 states     static final int none = 0;     static final int drag = 1;     static final int zoom = 2;     int mode = none;      // remember things zooming     pointf start = new pointf();     pointf mid = new pointf();     float olddist = 1f;      context context;      public mainpinchimageview(context context) {         super(context);         super.setclickable(true);         this.context = context;          matrix.settranslate(1f, 1f);         setimagematrix(matrix);      // sets default matrix          setscaletype(scaletype.matrix); //controls how image should resized or moved match size of imageview.          setontouchlistener(new ontouchlistener() {              @override             public boolean ontouch(view v, motionevent event) {                 // handle touch events here...                 switch (event.getaction() & motionevent.action_mask) {                 case motionevent.action_down:                     savedmatrix.set(matrix); //                      start.set(event.getx(), event.gety());                     log.d(tag, "mode=drag");                     mode = drag;                     break;                  case motionevent.action_move:                      if (mode == drag) {                         // ...                         matrix.set(savedmatrix);                         matrix.posttranslate(event.getx() - start.x, event.gety()- start.y);                     }                      break;                 }                 setimagematrix(matrix);                 return true; // indicate event handled             }         });     }      public void setimage(bitmap bm, int displaywidth, int displayheight) {         super.setimagebitmap(bm);          int displayheight = (getresources().getdisplaymetrics().heightpixels)/2;         int displaywidth = (getresources().getdisplaymetrics().widthpixels)/2;          int imgw = displaywidth/2;         int imgh = displayheight/2;             // fit screen.         float scale;         if ((displayheight / bm.getheight()) >= (displaywidth / bm.getwidth())) {             scale = (float) displaywidth / (float) bm.getwidth();         } else {             scale = (float) displayheight / (float) bm.getheight();         }          savedmatrix.set(matrix);         matrix.set(savedmatrix);         matrix.postscale(scale, scale, mid.x, mid.y);         setimagematrix(matrix);          savedmatrix.set(matrix);         matrix.set(savedmatrix);         matrix.posttranslate(displaywidth - imgw, displayheight - imgh);          setimagematrix(matrix);     } } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -