From Foreigner:
Import Android.app.activity;import android.content.context;import Android.graphics.canvas;import android.os.Bundle ; Import android.view.MotionEvent; import Android.widget.absolutelayout;import Android.widget.button;public class Drag_and_drop extends activity {/** Called when the activity is first created. */@Override public void OnCreate (Bund Le Icicle) {super.oncreate (icicle); MyView tx = new MyView (this); Tx.settext ("Drag Me"); Absolutelayout L = new Absolutelayout (this); Absolutelayout.layoutparams p = new Absolutelayout.layoutparams (AbsoluteLayout.LayoutParams.WRAP_CONTENT, absolutelayout.layoutparams.wrap_content,10,10); L.addview (TX,P); Setcontentview (l); }}class MyView extends button{public MyView (Context c) {super (c); } @Override public boolean onmotionevent (Motionevent event) {int action = event.getaction (); int mcurx = (int) event.getx (); int mcury = (int) event.getY (); if (action = = motionevent.action_move) {//this.settext ("x:" + Mcurx + ", y:" + Mcury); Absolutelayout.layoutparams p = newabsolutelayout.layoutparams (AbsoluteLayout.LayoutParams.WRAP_CONTENT, Absolutelayout.layoutparams.wrap_content,this.mleft + mcurx,this.mtop +mcury); This.setlayoutparams (P); } if (action = = motionevent.action_up) {//this.settext ("not moving"); } return true; } @Override public void draw (canvas canvas) {//TODO auto-generated method stub Super.dra W (canvas); } }
Drag and drop picture effects
Method One:
Import android.app.Activity; Import Android.os.Bundle; Import android.view.MotionEvent; Import Android.view.View; Import Android.view.View.OnTouchListener; Import Android.widget.ImageView; public class DragSample01 extends Activity {ImageView img; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.DRAG_SAMPLE01); img = (ImageView) Findviewbyid (R.id.img_view); Img.setontouchlistener (New Ontouchlistener () {private int mx, my; public boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) { Case MOTIONEVENT.ACTION_MOVE:MX = (int) (EVENT.GETRAWX ()); my = (int) (Event.getrawy ()-50); V.layout (Mx-img.getwidth ()/2, My-img.getheight ()/2, mx + img.getwidth ()/2, my + img.getheight ()/2); Break } return true; }}); } }
Layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><button android:id= "@+id/btn" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Drag to see ~ ~"/></linearlayout>
Method Two:
Import Android.app.activity;import Android.os.bundle;import Android.util.displaymetrics;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.button;public class Drafttest extends activity {/** Called when the activity is first created. */@Overridep ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Final Button btn = (Button) Findviewbyid (R.ID.BTN) Btn.setontouchlistener (new Ontouchlistener () {int[] temp = new int[] { 0, 0};p ublic boolean onTouch (View V, motionevent event) {int eventaction = event.getaction (); int x = (int) event.getrawx ( ); int y = (int) event.getrawy (); switch (eventaction) {case Motionevent.action_down://touch-down so check if thetemp[0] = (int) event.getx (); temp[1] = Y-v.gettop (); Break;case motionevent.action_move://Touch drag with the Ballv.layout (x-t Emp[0], y-temp[1], X + v.getwidth ()-temp[0], y-temp[1] + v.getheight ());//v.posTinvalidate (); Break;case MotionEvent.ACTION_UP:break;} return false;}});}}
Another type of:
Import Android.app.activity;import Android.os.bundle;import Android.util.displaymetrics;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.button;public class Drafttest extends activity {/** Called when the activity is first created. */public voi D onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);D Isplaymetrics DM = getresources (). Getdisplaymetrics (); final int screenwidth = dm.widthpixels;final int screenheight = DM. Heightpixels-50;final button B = (button) Findviewbyid (R.ID.BTN); B.setontouchlistener (new Ontouchlistener () {int LastX , Lasty;public boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubswitch (Event.getaction ()) {C ASE MotionEvent.ACTION_DOWN:lastX = (int) event.getrawx (); lasty = (int) event.getrawy (); Break;case Motionevent.action_ Move:int dx = (int) event.getrawx ()-Lastx;int dy = (int) Event.getrawy ()-Lasty;int left = V.getLeft () + dx;int top = v.gettop () + Dy;int right = V.getright () + Dx;int bottom = V.getbottom () + dy;if (left < 0) {left = 0;right = left + V.getwidth ();} if (right > ScreenWidth) {right = Screenwidth;left = Right-v.getwidth ();} if (Top < 0) {top = 0;bottom = top + v.getheight ();} if (Bottom > screenheight) {bottom = Screenheight;top = Bottom-v.getheight ();} V.layout (left, top, right, bottom), lastx = (int) event.getrawx (); lasty = (int) event.getrawy (); break;case MotionEvent.ACTION_UP:break;} return false;}});}}
Another, the implementation of the floating button.
Main functions:
Click the button to drag;
The button appears at the top of all buttons when the button is clicked;
Import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.text.style.absolutesizespan;import Android.util.log;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.widget.absolutelayout;import Android.widget.Button;public Class HelloWorld2 extends activity {/** Called when the activity is first created. */absolutelayout mlayoutgroup = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Mlayoutgroup = new Absolutelayout (this); Absolutelayout.layoutparams layoutparams = new Absolutelayout.layoutparams (320, 480, 0, 0); Setcontentview (Mlayoutgroup, layoutparams); Button button= New button (this); Button.settext ("Testbutton"); Layoutparams = new Absolutelayout.layoutparams (120, 60, 20, 20); Mlayoutgroup.addview (button, layoutparams); Button.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {//TODO auto-generated method stub// alert ();}}); Button.setontouchlistener (Touchlistener); Final button Btbutton = New button (this); Btbutton.settext ("Test button move"); Layoutparams = new Absolutelayout.layoutparams (120, 60, 20, 160); Mlayoutgroup.addview (Btbutton, layoutparams); Btbutton.setontouchlistener (Touchlistener); } ontouchlistener Touchlistener = new Ontouchlistener () {int temp[] = new int[]{0, 0};p ublic boolean OnTouch (View arg0, motionevent arg1) {//TODO auto-generated method Stubint eventaction = Arg1.getaction (); LOG.E ("Testbuttonmove", "Ontouchaction:" +eventaction); int x = (int) arg1.getrawx (); int y = (int) arg1.getrawy (); switch ( eventaction) {case motionevent.action_down:temp[0] = (int) arg1.getx (); temp[1] = (int) (Y-arg0.getTop ()); Mlayoutgroup.bringchildtofront (arg0); Arg0.postinvalidate (); Break;case MotionEvent.ACTION_MOVE:int left = x- Temp[0];int top = Y-temp[1];int right = left + arg0.getwidth (); int bottom = top + arg0.getheight (); Arg0.layout (left, top , right, bottom); arg0.postinvalidate (); break;default:break;} return false;} }; void alert () {new Alertdialog.builder (this). Setneutralbutton ("OK", new Dialoginterface.onclicklistener () {Publi c void OnClick (Dialoginterface arg0, int arg1) {//TODO auto-generated Method stub}}). settitle ("Test button"). Setmessage ( ' Test test test!!! '). Show (); } }
Android drag and Drop pictures & Drag the floating button to run around