实现可自由拖动的view

一.原理:

1.原理就是重写onTouchEvent方法,通过ACTION_DOWN,ACTION_MOVE,ACTION_UP,处理按下移动拿开改变view的位置。

2.需要注意的是,在大部分情况下都会出现父容器重绘,当view的位置发生改变时,也要相应改变其layoutparams,否则父布局重绘时,由于view的layoutparams没发生改变导致重绘的时候view会回到原点或在xml布局设置的原始位置,所 以在改变view的位置同时,还要设置其layoutparams。

二.代码:

public class MyImage extends ImageView {

private static final String TAG = "MyImage";

private Context mContext;

private int screenWidth;//屏幕宽高

private int screenHeight;

private int mLastX;

private int mLastY;

private int mLeft;//相对于父控件的位置

private int mTop;

private int mRight;

private int mBottom;

private int startX;//起始触摸位置,为了判断处理事件响应

private int startY;

public MyImage(Context context) {

super(context);

mContext = context;

}

public MyImage(Context context, AttributeSet attrs) {

super(context, attrs);

mContext = context;

}

public MyImage(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

mContext = context;

}

/**

* 设置 父容器 宽高

* @param w

* @param h

*/

public void initWH(int w,int h){

this.screenWidth = w;

this.screenHeight = h;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

int measuredHeight = this.getMeasuredHeight();//view自身高度

int measuredWidth = this.getMeasuredWidth();

int action = event.getAction();

switch (action) {

case MotionEvent.ACTION_DOWN:

startX = mLastX = (int) event.getRawX();

startY = mLastY = (int) event.getRawY();

break;

case MotionEvent.ACTION_MOVE:

int dx = (int) event.getRawX() - mLastX;

int dy = (int) event.getRawY() - mLastY;

//得到控件坐标距离父控件原点(左上角,坐标(0,0))的x轴距离

mLeft = this.getLeft() + dx;

//以此类推

mTop = this.getTop() + dy;

mRight = this.getRight() + dx;

mBottom = this.getBottom() + dy;

if (mLeft screenWidth) {

mRight = screenWidth;

mLeft = mRight - measuredWidth;

}

if (mTop screenHeight){

mBottom = screenHeight;

mTop = mBottom - measuredHeight;

}

int marginRight = screenWidth - mRight;

int marginBottom = screenHeight - mBottom;

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) this.getLayoutParams();

params.rightMargin = marginRight;

params.bottomMargin = marginBottom;

this.layout(mLeft, mTop, mRight, mBottom);

mLastX = (int) event.getRawX();

mLastY = (int) event.getRawY();

break;

case MotionEvent.ACTION_UP:

int endX = (int) event.getRawX();

int endY = (int) event.getRawY();

//判断移动的距离 极短 即为点击事件

if (Math.abs(endX - startX) < 3 || Math.abs(endY - startY) < 3) {

if(listener != null){

listener.click(this);

}

}

break;

}

return true;

}

//自定义一个点击事件

public interface MyOnClickListener{

void click(View view);

}

private MyOnClickListener listener;

public void setMyOnClickListener(MyOnClickListener listener){

this.listener = listener;

}

4.为了模拟父容器重绘的情况,布局文件中添加一个listview 及SwipeRefreshLayout,父布局就是一个相对布局,我们要拖动的imageView初始设置在右下角,布局文件就不贴出了。

5.在MainActivity中,获取RelativeLayout 对象,当界面获取焦点时,获取容器的宽高。再调用MyImage中的初始化父容器高度。

private int containerHeight;//父容器高度

private int containerWidth;//父容器宽度

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

// 这里来获取容器的宽和高

if (hasFocus) {

containerHeight = relativeLayout.getHeight();

containerWidth = relativeLayout.getWidth();

imageView.initWH(containerWidth,containerHeight);

}

}

6.为view设置点击事件。由于在重写onTouchEvent时,事件被拦截,使用我们自定义的触发事件

imageView.setMyOnClickListener(new MyImage.MyOnClickListener() {

@Override

public void click(View view) {

Log.e(TAG,"点击了");

}

});

三.总结:

在ACTION_MOVE处理view移动时,首先要判断是否滑出窗体,最重要的分清view的layout与LayoutParams,layout是view相对容器的左上角(0,0)位置,layoutParams是view相对于容器的左上右下的位置

你可能感兴趣的:(实现可自由拖动的view)