Android下图片可拖动到任意位置的效果
下面为Activity的代码:

public class DraftTest extends Activity { /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.sign);  DisplayMetrics dm = getResources().getDisplayMetrics();  final int screenWidth = dm.widthPixels;     final int screenHeight = dm.heightPixels - 50;     //拖动的按钮  final Button b=(Button)findViewById(R.id.startBtn);    //添加触摸事件  b.setOnTouchListener(new OnTouchListener(){   int lastX, lastY; //记录移动的最后的位置   public boolean onTouch(View v, MotionEvent event) {       //获取Action    int ea=event.getAction();      Log.i("TAG", "Touch:"+ea);    switch(ea){       case MotionEvent.ACTION_DOWN:   //按下     lastX = (int) event.getRawX();        lastY = (int) event.getRawY();        break;        /**        * layout(l,t,r,b)        * l  Left position, relative to parent                       t  Top position, relative to parent                       r  Right position, relative to parent                       b  Bottom position, relative to parent          * */      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);        Log.i("", "position:" + left +", " + top + ", " + right + ", " + bottom);     //将当前的位置再次设置     lastX = (int) event.getRawX();        lastY = (int) event.getRawY();        break;       case MotionEvent.ACTION_UP:   //脱离     break;                     }       return false;      }});    } }

XML配置文件Sign.xml的代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layoutRacingNeedForSpeed" android:padding="0px" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button   android:id="@+id/startBtn"  android:text="tuodongdeanniu"  android:layout_centerInParent="true"  android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>

这样就可以实现了~

更多相关文章

  1. Android(安卓)ListView 去除边缘阴影、选中色、拖动背景色等(and
  2. Android(安卓)SeekBar的使用
  3. Android(安卓)读取MCC, MNC
  4. Android(安卓)SeekBar的使用
  5. Android移动应用基础学习——第二章UI开发
  6. Android中gravity与layout_gravity的区别
  7. Android(安卓)基本控件Text属性
  8. Android(安卓)基本控件Text属性
  9. 美国Android占28%份额 摩托Droid最受欢迎

随机推荐

  1. 【Android】SD卡的安全存储问题
  2. Android撬动IT市场的新支点!
  3. Android教程之一:Window下搭建Android开发
  4. Android程序员必看之Android六大优势
  5. Android撬动IT市场的新支点
  6. Android与物联网设备通信-概念入门
  7. 简析Android对Linux内核的改动-(上)
  8. Android将允许纯C/C++开发应用
  9. 最封闭的开源系统:话说 Android(安卓)的八
  10. android本地系统服务详解