Android Drag pictures and multi-layer image overlay

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.