来自老外:

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(Bundle 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.draw(canvas);        }    }

  


拖拽图片效果

方法一:

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;              }});      }  }  

  

布局文件

<?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="拖动看看~~" /></LinearLayout>

  


方法二:

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. */@Overridepublic 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 };public 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 - temp[0], y - temp[1], x + v.getWidth()- temp[0], y - temp[1] + v.getHeight());//v.postInvalidate();break;case MotionEvent.ACTION_UP:break;}return false;}});}}

  


另一种:

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 void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);DisplayMetrics 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()) {case 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;}});}}

  


再一个,浮动按钮的实现。
主要功能:
点击按钮可以进行拖动;
当点击按钮时按钮会出现于所有按钮的最上方;

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("测试按钮移动");        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};public 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() {public void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}}).setTitle("test button").setMessage("test test test!!!").show();    }        }

  



更多相关文章

  1. android 捕获返回(后退)按钮事件的两种方法
  2. ANDROID GRIDVIEW 点击某个位置获取某个单元格
  3. 最完美的android仿ios开关按钮源码
  4. 第一章:初入Android大门(弹出对话框)
  5. Android点击按钮播放音效
  6. android框架设计
  7. 第一章:初入Android大门(Gallery拖动相片特效)
  8. Android(安卓)2.1 GPS定位和拍照功能代码
  9. android 添加对back按钮的处理,点击提示退出

随机推荐

  1. 解决 Android Studio : minSdkVersion 8
  2. Android(安卓)6.0(API 23)及以上动态权限
  3. android增加5G热点
  4. Unity3D与安卓原生的交互(二)
  5. Android原生项目引入最新的React Native
  6. Android APK文件结构 完整打包编译的流程
  7. [Android]破解android安卓apk软件所需的
  8. android 按两次退出的实现
  9. android anim 动画效果 基础知识
  10. ubuntu系统下,搭建Android开发环境!!