You first implement a custom drag shadow class that imageview the image in the control to 50% as a drag shadow, and the drag Shadow is implemented as follows
public class Mydragshadowbuilder extends Dragshadowbuilder {//Drag the area of the shadow private static drawable shadow;// Save drawn drag shadow image private static Bitmap newbitmap;public Mydragshadowbuilder (View arg0) {super (ARG0);//TODO auto-generated Constructor Stubshadow = new Colordrawable (color.ltgray);//light gray}//draw in this method drag shadow image instantiation Newbitmap variable @overridepublic void Onprovideshadowmetrics (Point shadowsize, point shadowtouchpoint) {//TODO auto-generated method Stubsuper.onprovideshadowmetrics (Shadowsize, shadowtouchpoint); int width, heigth;//set the width/height of the drag shadow to 1.5 times times width of the original width = ( int) (GetView (). GetWidth () * 1.5); heigth = (int) (GetView (). GetHeight () * 1.5);//sets the drawing area of the drag image shadow.setbounds (0, 0, Width , heigth);//sets the width and height of the drag shadow image Shadowsize.set (width, heigth);//sets the position of the finger where the drag image is set to the midpoint Shadowtouchpoint.set (WIDTH/2, HEIGTH/2 The IF (GetView () instanceof ImageView) {//GetView () method returns a value that is the constructor method passed in arg0 parameter ImageView ImageView = (ImageView) getView ();//Received Take drawable object drawable drawable = imageview.getdrawable ();//Get ImageView Bitmapbitmap Bitmap = ((bitmapdrawable) drawable). Getbitmap ();//Create a new Bitmapnewbitmap = Bitmap.createbitmap (width, heigth, config.argb_ 8888); Canvas canvas = new Canvas (NEWBITMAP),//draws the image on the canvas, but now does not formally draw the image on the shadow image, but currently just enlarges the bitmap and draws it on the Newbitmap Canvas.drawbitmap (Newbitmap, New rect (0, 0, bitmap.getwidth (), Bitmap.getheight ()), new rect (0, 0, Width, heigth), null);}} @Overridepublic void Ondrawshadow (canvas canvas) {//TODO auto-generated method Stubsuper.ondrawshadow (canvas);// The image is formally drawn on the shadow Image Canvas.drawbitmap (newbitmap, 0, 0, New Paint ());}}
The main window to implement
public class Mainactivity extends Activity implements Ondraglistener {private framelayout dragdropregin;//drag to image private I Mageview ImageView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);d ragdropregin = (framelayout) Findviewbyid (r.id.fl_dragdrop_region); ImageView = (ImageView) Findviewbyid (R.id.imageview);//Set drag listener Imageview.setonlongclicklistener for the target (new Onlongclicklistener () {@Overridepublic Boolean onlongclick (View v) {//TODO auto-generated method Stubdragshadowbuilder Mysbuilder = new Mydragshadowbuilder (imageView);//Start dragging, the first parameter in the method is an object of type Clipdata. Used to pass clipboard data, can be Nullv.startdrag (null, mysbuilder, NULL, 0); return true;});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean Ondrag (View V, dragevent event) {//TODO auto-generated method Stubint ACtion = event.getaction (); switch (action) {//Start dragging case DragEvent.ACTION_DRAG_STARTED:System.out.println ("DRAG STARTED" break;//into the target area case DragEvent.ACTION_DRAG_ENTERED:System.out.println ("DRAG entered"); break;//move case in target area DragEvent.ACTION_DRAG_LOCATION:System.out.println ("DRAG location x=" + event.getx () + "y =" + event.gety ()); break;// Leave the target area case DragEvent.ACTION_DRAG_EXITED:System.out.println ("Drage EXITED") break;//drop the ImageView control case in the target area DragEvent.ACTION_DROP:System.out.println ("drag DROP"); ImageView ImageView = (ImageView) getlayoutinflater (). Inflate (R.layout.image, NULL); Layoutparams layoutparams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);//due to Framelayout You cannot directly use coordinates to set the location of a child view here with the left margin and the top margin layoutparams.leftmargin = (int) event.getx (); layoutparams.topmargin = (int) event.gety (); /Add to view, complete replication Dragdropregin.addview (ImageView, layoutparams); Break;case dragevent.action_drag_ended: SYSTEM.OUT.PRINTLN ("drag ended");d Efault:return false;} return true;}}and layout files
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <framelayout android:id=" @+id/fl_dragdrop_region " android:layout_width=" Match_parent " android:layout_height=" match_parent "> <imageview android:id=" @+id/imageview " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:layout_ gravity= "center" android:src= "@drawable/ic_launcher"/> </framelayout></relativelayout >
<?xml version= "1.0" encoding= "Utf-8"? ><imageview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:layout_ gravity= "center" android:layout_margintop= "50DP" android:src= "@drawable/ic_launcher"/>
Android drag ImageView for copy effect