public class DragLayout extends RelativeLayout {
private ViewDragHelper mDragger;
private ViewDragHelper.Callback callback;
private ImageView imageView;
private String mTitle;
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
public DragLayout(Context context) {
this(context, null);
}
public void setTitle(String title) {
this.mTitle = title;
}
public DragLayout(final Context context, AttributeSet attrs) {
super(context, attrs);
callback = new DraggerCallBack();
//第二个参数就是滑动灵敏度的意思 可以随意设置
mDragger = ViewDragHelper.create(this, 1.0f, callback);
imageView = new ImageView(getContext());
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// imageView.setImageResource(R.drawable.core_service_news);
imageView.setImageResource(R.mipmap.core_float_service);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.rightMargin = DensityUtils.dp2px(getContext(), 5);
layoutParams.bottomMargin = DensityUtils.dp2px(getContext(), 210); //
layoutParams.width = 125; // 写死尺寸,保证不同机型大小一致,2019.12.16
layoutParams.height = 150;
addView(imageView, layoutParams);
}
class DraggerCallBack extends ViewDragHelper.Callback {
//这个地方实际上函数返回值为true就代表可以滑动 为false 则不能滑动
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
//取得左边界的坐标
final int leftBound = getPaddingLeft();
//取得右边界的坐标
final int rightBound = getWidth() - child.getWidth() - leftBound;
//这个地方的含义就是 如果left的值 在leftbound和rightBound之间 那么就返回left
//如果left的值 比 leftbound还要小 那么就说明 超过了左边界 那我们只能返回给他左边界的值
//如果left的值 比rightbound还要大 那么就说明 超过了右边界,那我们只能返回给他右边界的值
return Math.min(Math.max(left, leftBound), rightBound);
}
//纵向的注释就不写了 自己体会
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
final int topBound = getPaddingTop();
final int bottomBound = getHeight() - child.getHeight() - topBound;
return Math.min(Math.max(top, topBound), bottomBound);
}
@Override
public int getViewHorizontalDragRange(View child) {
return imageView == child ? child.getWidth() : 0;
}
@Override
public int getViewVerticalDragRange(View child) {
return imageView == child ? child.getHeight() : 0;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//决定是否拦截当前事件
return mDragger.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//处理事件
mDragger.processTouchEvent(event);
return isTouchPointInView(imageView, event.getRawX(), event.getRawY());
}
//(x,y)是否在view的区域内
private boolean isTouchPointInView(View view, float x, float y) {
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();
//view.isClickable() &&
if (y >= top && y <= bottom && x >= left
&& x <= right) {
return true;
}
return false;
}
}