实现步骤
1.自定义view 实现拖动事件
package com.example.administrator.mydemo.dragview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.example.administrator.mydemo.R;
public class DragView extends FrameLayout {
private float mStartX;
private float mStartY;
private int rawX;
private int rawY;
private int lastX;
private int lastY;
private int pHeight;
private int pWidth;
private long mLastTime;
private ViewGroup parent;
private ImageView iv_pic, iv_close;
private OnDragViewClickListener dragViewClickListener;
private boolean flag = false;
public void SetClickListener(OnDragViewClickListener dragViewClickListener) {
this.dragViewClickListener = dragViewClickListener;
}
public DragView(Context context) {
this(context, null);
}
public DragView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.layout_dragview, this);
iv_close = view.findViewById(R.id.iv_close);
iv_pic = view.findViewById(R.id.iv_pic);
}
//判断是否触摸view
private boolean isTouchInView(ImageView view, float xAxis, float yAxis) {
if (view == null) {
return false;
}
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (yAxis >= top && yAxis <= bottom && xAxis >= left
&& xAxis <= right) {
return true;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取相对屏幕的坐标,即以屏幕左上角为原点
rawX = (int) event.getRawX();
rawY = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //捕获手指触摸按下动作
//获取相对View的坐标,即以此View左上角为原点
mStartX = event.getRawX();
mStartY = event.getRawY();
if (isTouchInView(iv_close, rawX, rawY)) {
flag = true;
}
mLastTime = System.currentTimeMillis();
lastX = rawX;
lastY = rawY;
if (getParent() != null) {
parent = (ViewGroup) getParent();
pHeight = parent.getHeight();
pWidth = parent.getWidth();
}
break;
case MotionEvent.ACTION_MOVE: //捕获手指触摸移动动作
int dx = rawX - lastX;
int dy = rawY - lastY;
float x = getX() + dx;
float y = getY() + dy;
//判断是否到边界
x = x < 0 ? 0 : x > pWidth - getWidth() ? pWidth - getWidth() : x;
y = getY() < 0 ? 0 : getY() + getHeight() > pHeight ? pHeight - getHeight() : y;
setX(x);
setY(y);
lastX = rawX;
lastY = rawY;
break;
case MotionEvent.ACTION_UP: //捕获手指触摸离开动作
if (System.currentTimeMillis() - mLastTime < 800) {
if (Math.abs(mStartX - event.getRawX()) < 10.0 && Math.abs(mStartY - event.getRawY()) < 10.0) {
//处理点击的事件
if (flag) {
dragViewClickListener.onDragViewListener("delete", "");
} else
dragViewClickListener.onDragViewListener("pic", "");
}
}
flag = false;
break;
}
return true;
}
}
2.自定义拖动的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:srcCompat="@color/colorAccent" />
<ImageView
android:id="@+id/iv_close"
android:layout_width="20dp"
android:layout_height="20dp"
app:srcCompat="@mipmap/close" />
</RelativeLayout>
3.拖动view中控件点击事件回调
package com.example.administrator.mydemo.dragview;
public interface OnDragViewClickListener {
void onDragViewListener(String name, String context);
}
4.Activity中调用
package com.example.administrator.mydemo.dragview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.administrator.mydemo.R;
public class DragViewActivity extends AppCompatActivity {
private DragView dragView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dragview);
dragView = findViewById(R.id.DragView);
dragView.SetClickListener(new OnDragViewClickListener() {
@Override
public void onDragViewListener(String name, String context) {
if (name.equals("pic")) {
Toast.makeText(DragViewActivity.this, "点击事件", Toast.LENGTH_SHORT).show();
} else if (name.equals("delete")) {
Toast.makeText(DragViewActivity.this, "关闭view", Toast.LENGTH_SHORT).show();
dragView.setVisibility(View.GONE);
}
}
});
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dragView.setVisibility(View.VISIBLE);
}
});
}
}
5.Activity中布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".dragview.DragViewActivity">
<com.example.administrator.mydemo.dragview.DragView
android:id="@+id/DragView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="124dp"
android:layout_marginStart="124dp"
android:layout_marginTop="100dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
完成