Before moving:
After moving:
Code:
1 // two images to overlay 2 ImageView mIv; 3 ImageView mIv2; 4 // show the image after drawing 5 ImageView Mimageview; 6 canvas canvas; 7 8 int lastx, lasty;
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.main);5 6Mimageview =(ImageView) Findviewbyid (R.id.conta);7 Mimageview.setimageresource (R.DRAWABLE.A);8Mimageview.setdrawingcacheenabled (true);//Enable caching9 TenMIv2 =(ImageView) Findviewbyid (R.ID.IMG2); OneMiv2.setdrawingcacheenabled (true); AMiv2.setontouchlistener ( This); - -MIv =(ImageView) Findviewbyid (r.id.img); theMiv.setdrawingcacheenabled (true); -Miv.setontouchlistener ( This); -}
Here is the key code for the move
1 @Override2 Public BooleanOnTouch (View V, motionevent event) {3 4 Switch(Event.getaction ()) {5 6 CaseMotionevent.action_down:7LASTX = (int) event.getrawx ();8Lasty = (int) Event.getrawy ();9 Ten Break; One CaseMotionevent.action_move: A - intDX = (int) event.getrawx ()-Lastx; - intDY = (int) Event.getrawy ()-lasty; the - intleft = V.getleft () +DX; - inttop = v.gettop () +dy; - intright = V.getright () +DX; + intBottom = V.getbottom () +dy; - + A log.i (' life ', ' left = ' + left + ' v.getleft= ' + v.getleft () +" ; EVENT.GETRAWX = "
+ event.getrawx () + "; LASTX = "+ Lastx +"dx =" + dx); - - V.layout (left, top, right, bottom); - inLASTX = (int) event.getrawx (); -Lasty = (int) Event.getrawy (); to + Break; - Casemotionevent.action_up: the * Break; $ }Panax Notoginseng - return true; the}
Event handling for the Save button
1@SuppressLint ("Newapi")2 Public voidSave (View view) {3 //Save the superimposed picture4Bitmap Bitmap =Mimageview.getdrawingcache ();5 if(Canvas = =NULL) {6Canvas =NewCanvas (bitmap);7 }8 //draw according to the position of two robots9Canvas.drawbitmap (Miv.getdrawingcache (), Miv.getleft (), Miv.gettop (),NULL);TenCanvas.drawbitmap (Miv2.getdrawingcache (), Miv2.getleft (), Miv2.gettop (),NULL); One A //display on the interface - Mimageview.setimagebitmap (bitmap); - the //Save to local -File f =NewFile (Getcachedir (). GetAbsolutePath () + "/" -+ systemclock.currentthreadtimemillis () + "Img.jpg"); - Try { + Savemybitmap (f, bitmap); -}Catch(IOException e) { + e.printstacktrace (); A } at - //two robot settings are not visible - miv.setvisibility (view.gone); - miv2.setvisibility (view.gone); -}
SOURCE download
Android Drag pictures and multi-layer image overlay