android拖动实现

这个也是从网上得到的代码,例子比较简单,但是如果有需要此功能的,这个例子可以提供很多提示,首先,给个截图




这个是拖动以后的效果,一个imageview和一个button控件,提供两份代码下载吧,一份是只有一个Button的,另一份就是像上图,就是多了一个imagview!先看下代码吧,比较简单:

[java] view plain copy
  1. public class DraftTestextends Activityimplements OnTouchListener{ 
  2.     /** Called when the activity is first created. */ 
  3.     int screenWidth; 
  4.     int screenHeight; 
  5.     int lastX; 
  6.     int lastY; 
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main); 
  11.  
  12.         DisplayMetrics dm = getResources().getDisplayMetrics(); 
  13.         screenWidth = dm.widthPixels; 
  14.         screenHeight = dm.heightPixels - 50
  15.         Button button=(Button)findViewById(R.id.btn); 
  16.         ImageView imageView=(ImageView)findViewById(R.id.btn2); 
  17.         imageView.setOnTouchListener(this); 
  18.         button.setOnTouchListener(this); 
  19.     } 
  20.  
  21.     @Override 
  22.     public boolean onTouch(View v, MotionEvent event) { 
  23.         // TODO Auto-generated method stub 
  24.         int action=event.getAction(); 
  25.         Log.i("@@@@@@", "Touch:"+action); 
  26.         //Toast.makeText(DraftTest.this, "λ�ã�"+x+","+y, Toast.LENGTH_SHORT).show(); 
  27.         switch(action){ 
  28.         case MotionEvent.ACTION_DOWN: 
  29.             lastX = (int) event.getRawX(); 
  30.             lastY = (int) event.getRawY(); 
  31.             break
  32.             /**
  33.              * layout(l,t,r,b)
  34.              * l  Left position, relative to parent
  35.             t  Top position, relative to parent
  36.             r  Right position, relative to parent
  37.             b  Bottom position, relative to parent 
  38.              * */ 
  39.         case MotionEvent.ACTION_MOVE: 
  40.             int dx =(int)event.getRawX() - lastX; 
  41.             int dy =(int)event.getRawY() - lastY; 
  42.          
  43.             int left = v.getLeft() + dx; 
  44.             int top = v.getTop() + dy; 
  45.             int right = v.getRight() + dx; 
  46.             int bottom = v.getBottom() + dy;                     
  47.             if(left < 0){ 
  48.                 left = 0
  49.                 right = left + v.getWidth(); 
  50.             }                    
  51.             if(right > screenWidth){ 
  52.                 right = screenWidth; 
  53.                 left = right - v.getWidth(); 
  54.             }                    
  55.             if(top < 0){ 
  56.                 top = 0
  57.                 bottom = top + v.getHeight(); 
  58.             }                    
  59.             if(bottom > screenHeight){ 
  60.                 bottom = screenHeight; 
  61.                 top = bottom - v.getHeight(); 
  62.             }                    
  63.             v.layout(left, top, right, bottom); 
  64.             Log.i("@@@@@@", "position��" + left +", " + top +", " + right +", " + bottom);    
  65.             lastX = (int) event.getRawX(); 
  66.             lastY = (int) event.getRawY();                   
  67.             break
  68.         case MotionEvent.ACTION_UP: 
  69.             break;               
  70.         } 
  71.         return false;    
  72.     } 
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;	
	}
}



 



高度减去50是减去状态栏和标题栏的高度。

[java] view plain copy
  1. case MotionEvent.ACTION_DOWN: 
  2.             lastX = (int) event.getRawX(); 
  3.             lastY = (int) event.getRawY(); 
  4.             break
case MotionEvent.ACTION_DOWN:
			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();
			break;


然后获取控件一开始的位置,然后在ACTION_MOVIE中:

[java] view plain copy
  1. int dx =(int)event.getRawX() - lastX; 
  2.             int dy =(int)event.getRawY() - lastY; 
  3.          
  4.             int left = v.getLeft() + dx; 
  5.             int top = v.getTop() + dy; 
  6.             int right = v.getRight() + dx; 
  7.             int bottom = v.getBottom() + dy;                     
  8.             if(left < 0){ 
  9.                 left = 0
  10.                 right = left + v.getWidth(); 
  11.             }                    
  12.             if(right > screenWidth){ 
  13.                 right = screenWidth; 
  14.                 left = right - v.getWidth(); 
  15.             }                    
  16.             if(top < 0){ 
  17.                 top = 0
  18.                 bottom = top + v.getHeight(); 
  19.             }                    
  20.             if(bottom > screenHeight){ 
  21.                 bottom = screenHeight; 
  22.                 top = bottom - v.getHeight(); 
  23.             }                    
  24.             v.layout(left, top, right, bottom); 
  25.             Log.i("@@@@@@","position��" + left +", " + top +", " + right +", " + bottom);    
  26.             lastX = (int) event.getRawX(); 
  27.             lastY = (int) event.getRawY();   
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();	


getLeft()方法得到的是控件坐标距离父控件原点(左上角,坐标(0,0))的x轴距离,getReght()是控件右边距离父控件原点的x轴距离,同理,getTop和getButtom是距离的y轴距离。

[java] view plain copy
  1. if(left < 0){ 
  2.                 left = 0
  3.                 right = left + v.getWidth(); 
  4.             }                    
  5.             if(right > screenWidth){ 
  6.                 right = screenWidth; 
  7.                 left = right - v.getWidth(); 
  8.             }                    
  9.             if(top < 0){ 
  10.                 top = 0
  11.                 bottom = top + v.getHeight(); 
  12.             }                    
  13.             if(bottom > screenHeight){ 
  14.                 bottom = screenHeight; 
  15.                 top = bottom - v.getHeight(); 
  16.             }    
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();
			}	

这里的判断是为了是控件不超出屏幕以外,即:到达边界以后,不能再移动。

[java] view plain copy
  1. v.layout(left, top, right, bottom); 
v.layout(left, top, right, bottom);


 

设置View的位置。

有一点忘记说了,就是像ImageView和TextView这些控件,要想实现拖动,要在xml文件中设置它的clickable为true。

[java] view plain copy
  1. android:clickable="true" 
android:clickable="true"


 

就这样,这些就是这个demo的全部内容。

最后,是代码的下载地址:


http://download.csdn.net/detail/aomandeshangxiao/4187376

http://download.csdn.net/detail/aomandeshangxiao/4189910

问题:

这个拖动到新位置后把程序关闭再次打开它又回到了原先的位置,请教楼主怎么设置才能让它能把拖动后的位置保存呢,再次打开程序它都会显示上次最后拖动到的位置

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值