这个也是从网上得到的代码,例子比较简单,但是如果有需要此功能的,这个例子可以提供很多提示,首先,给个截图
这个是拖动以后的效果,一个imageview和一个button控件,提供两份代码下载吧,一份是只有一个Button的,另一份就是像上图,就是多了一个imagview!先看下代码吧,比较简单:
public class DraftTest extends Activity implements OnTouchListener{ /** Called when the activity is first created. */ int screenWidth; int screenHeight; int lastX; int lastY; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 50; Button button=(Button)findViewById(R.id.btn); ImageView imageView=(ImageView)findViewById(R.id.btn2); imageView.setOnTouchListener(this); button.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action=event.getAction(); Log.i("@@@@@@", "Touch:"+action); //Toast.makeText(DraftTest.this, "λ�ã�"+x+","+y, Toast.LENGTH_SHORT).show(); switch(action){ 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; } }