Android 控件随意拖动

12 篇文章 0 订阅
7 篇文章 0 订阅
    package com.geyi.examples;  
      
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.util.DisplayMetrics;  
    import android.util.Log;  
    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. */  
        @Override  
        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;  
      
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    // TODO Auto-generated method stub  
                    int ea=event.getAction();  
                    Log.i("TAG", "Touch:"+ea);  
      
                    //Toast.makeText(DraftTest.this, "位置:"+x+","+y, Toast.LENGTH_SHORT).show();  
      
                    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;  
                }});  
        }  
    }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值