【例子】随意拖动Button

一个可以随意拖动Button的例子,难点在于实时更新坐标

package org.test;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class TestActivity extends Activity
{
    private static final String tag="My Tag";
    private Context context;
    private View container;
    private Button btn;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    Log.v(tag, "onCreate");
	    context=this;
	    container=findViewById(R.id.container);
	    
	    btn=(Button)findViewById(R.id.btn);
	    btn.setOnTouchListener(touchLisener);
	    btn.setOnClickListener(new View.OnClickListener()
	    {
            @Override
            public void onClick(View v)
            {
                Log.i(tag,"btn clicked");
            }
        });
	}
	
	

    @Override
	protected void onStart()
	{
    	Log.v(tag, "onStart");
		super.onStart();
	}

	@Override
	protected void onResume()
	{
		Log.v(tag, "onResume");
		super.onResume();
	}

	OnTouchListener touchLisener=new OnTouchListener()
    {
        int lastX, lastY;
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            // TODO Auto-generated method stub
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    Log.v("My Tag", "v.getLeft()=" + String.valueOf(v.getLeft()) + "\n" + 
                    				"v.getTop()=" + String.valueOf(v.getTop())+"\n" + 
                    				"v.getRight()=" + String.valueOf(v.getRight())+"\n" + 
                    				"v.getBottom()" + String.valueOf(v.getBottom())+"\n" + 
                    				"v.getWidth()=" + String.valueOf(v.getWidth())+"\n" + 
                    				"v.getHeight()=" + String.valueOf(v.getHeight())+"\n" +
                    				"lastX:" + String.valueOf(lastX) + " " + 
                    				"lastY:" + String.valueOf(lastY));
                    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 > container.getMeasuredWidth())
		            {  
		                right = container.getMeasuredWidth();  
		                left = right - v.getWidth();  
		            }  
		
		            if (top < 0) 
		            {  
		                top = 0;  
		                bottom = top + v.getHeight();  
		            }  
		
		            if (bottom > container.getMeasuredHeight())
		            {  
		                bottom = container.getMeasuredHeight();  
		                top = bottom - v.getHeight();  
		            }
		//            v.measure(right - left, bottom - top);
		            v.layout(left, top, right, bottom);  
		
		            lastX = (int) event.getRawX();
		            lastY = (int) event.getRawY();
		            v.postInvalidate();
                        break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    break;
            }
            return false;
        }
    };
}

源代码

你可能感兴趣的:(【例子】随意拖动Button)