上一篇講到通過通過goolge官方的ViewDragHelper工具實(shí)現(xiàn)拖動(dòng)的方法(上一篇見http://www.reibang.com/p/94477b804136),那么有一個(gè)問題就是在DragframeLayout中的onTouchEvent一直接收不到觸摸消息,而且在onInterceptTouchEvent的時(shí)候宙拉,并沒有觸發(fā)ViewDragHelper.tryCaptureView方法,因此誕生了另一種比較原始的方法:通過自定義可拖動(dòng)view來實(shí)現(xiàn)
主要方法:
initEdge:設(shè)置可拖動(dòng)view能拖動(dòng)范圍的初始邊界炕吸,一般情況下為父布局的邊界旬昭。注意view.getLeft...等會獲取到會0眯漩,我是在網(wǎng)路數(shù)據(jù)返回的情況下設(shè)置邊界旧噪,并顯示的吨娜。也有方法開一個(gè)子線程獲取。
onTouchEvent:拖動(dòng)的計(jì)算以及重新layout
代碼:
public class DragImageView extends com.facebook.drawee.view.SimpleDraweeView {
String TAG = "DragImageView";
private Rect mFloatRectArea;//浮層廣告的最后拖動(dòng)位置
private int mSensitive = 5; //拖動(dòng)靈敏度淘钟,影響點(diǎn)擊事件宦赠,目前測試下來對2K的屏幕正常
public DragImageView(Context context) {
this(context, null);
}
public DragImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DragImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 設(shè)置在父布局中的邊界
* @param l
* @param t
* @param r
* @param b
*/
public void initEdge(int l,int t,int r,int b) {
edgeLeft = l;
edgeTop = t;
edgeRight = r;
edgeBottom = b;
mFloatRectArea = new Rect(-1,-1,-1,-1);
Log.e(TAG,"L="+l+";t="+t+";r="+r+";b="+b);
}
int edgeLeft, edgeTop, edgeRight, edgeBottom;
int lastX, lastY, movex, movey, dx, dy;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
movex = lastX;
movey = lastY;
break;
case MotionEvent.ACTION_MOVE:
dx = (int) event.getRawX() - lastX;
dy = (int) event.getRawY() - lastY;
int left = getLeft() + dx;
int top = getTop() + dy;
int right = getRight() + dx;
int bottom = getBottom() + dy;
if (left < edgeLeft) {
left = edgeLeft;
right = left + getWidth();
}
if (right > edgeRight) {
right = edgeRight;
left = right - getWidth();
}
if (top < edgeTop) {
top = edgeTop;
bottom = top + getHeight();
}
if (bottom > edgeBottom) {
bottom = edgeBottom;
top = bottom - getHeight();
}
layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//避免滑出觸發(fā)點(diǎn)擊事件
if ((int) (event.getRawX() - movex) >= mSensitive
|| (int) (event.getRawY() - movey) >= mSensitive) {
return true;
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/df_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.windfindtech.ishanghai.view.SwipeScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_white"
android:scrollbars="none">
<RelativeLayout
android:id="@+id/network_tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_white">
...........
</RelativeLayout>
</com.windfindtech.ishanghai.view.SwipeScrollView>
<com.windfindtech.ishanghai.view.DragImageView
android:id="@+id/iv_drag_adver"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right|top"
android:src="@drawable/ic_launcher" />
</FrameLayout>