布局demo四:拖拽button

效果图:

布局demo四:拖拽button_第1张图片  布局demo四:拖拽button_第2张图片

 

代码如下:

package com.zch.test;


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.view.Window;
import android.widget.ImageButton;
import android.widget.Toast;

 

public class MainActivity extends Activity { 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_main);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        final int screenWidth = dm.widthPixels;
        final int screenHeight = dm.heightPixels - 50;
        final ImageButton btn = (ImageButton) findViewById(R.id.btn);
        btn.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);
                switch (ea) {
               //ACTION_DOWN和ACTION_UP就是单点触摸屏幕,按下去和放开的操作;
               //ACTION_POINTER_DOWN和ACTION_POINTER_UP就是多点触摸屏幕,当有一只手指按下去的时候,另一只手指按下和放开的动作捕捉;
               //ACTION_MOVE就是手指在屏幕上移动的操作;
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;
                    int l = v.getLeft() + dx;
                    int b = v.getBottom() + dy;
                    int r = v.getRight() + dx;
                    int t = v.getTop() + dy;
                    // 下面判断移动是否超出屏幕
                    if (l < 0) {
                        l = 0;
                        r = l + v.getWidth();
                    }
                    if (t < 0) {
                        t = 0;
                        b = t + v.getHeight();
                    }
                    if (r > screenWidth) {
                        r = screenWidth;
                        l = r - v.getWidth();
                    }
                    if (b > screenHeight) {
                        b = screenHeight;
                        t = b - v.getHeight();
                    }
                    v.layout(l, t, r, b);
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    Toast.makeText(MainActivity.this,
                            "当前位置:" + l + "," + t + "," + r + "," + b,
                            Toast.LENGTH_SHORT).show();
                    v.postInvalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                }
                return false;
            }
        });
       
    }

 } 

 

配置文件:

<?xml version="1.0" encoding="utf-8"?> 

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

       android:layout_width="fill_parent" 

       android:layout_height="fill_parent" 

       android:orientation="vertical" > 

       <ImageButton
           android:id="@+id/btn"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ico_hover"
           />

           </LinearLayout> 

 

 

 

你可能感兴趣的:(布局demo四:拖拽button)