简书链接:视频拖动容器控件的实现
文章字数:175,阅读全文大约需要1分钟

需要包裹1个view视频view ,为第一个child.

videoView = getChildAt(0);

为了防止重新绘制过程中导致被还原,需要做的事情是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(videoView!=null){

if (!isNotFirst) {
initPointPosition.x = videoView.getLeft();
initPointPosition.y = videoView.getTop();
Prt.d(TAG, "first position:x:" + initPointPosition.x + ",y:" + initPointPosition.y);
isNotFirst = true;
}

videoView.layout(initPointPosition.x, initPointPosition.y,
initPointPosition.x + videoView.getMeasuredWidth(),
initPointPosition.y + videoView.getMeasuredHeight());
}

}

//如果是第一次就把初始化position赋值给initPointPosition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208



/**
* Created by luozheng on 2016/10/13. qssq666.cn Administrator
*/

public class VideoDragContainer extends FrameLayout {

private static final String TAG = "VideoDragContainer";
private ViewDragHelper mDragHelper;
private int mWidth;
private int mHeight;
private View videoView;
private Point initPointPosition = new Point();
private boolean isNotFirst;
private float mPercent = 1f;

public VideoDragContainer(Context context) {
super(context);
init(context);
}

public VideoDragContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public VideoDragContainer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public VideoDragContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}

private void init(Context context) {
mDragHelper = ViewDragHelper.create(this, 1f, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
// return child instanceof VideoContainer;
// return child instanceof true;
return true;
}

@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
Prt.i(TAG, "clampViewPositionHorizontal left:" + left + ",dx:" + dx);
if (left < 0) {
return 0;
} else if (left > (mWidth - child.getWidth())) {//往右边滑动不可超过直播的宽度
return mWidth - child.getWidth();
} else {
return left;
}

/* final int leftBound = getPaddingLeft();
final int rightBound =mWidth - videoView.getWidth();
final int newLeft = Math.min(Math.max(left, leftBound), rightBound);
return newLeft;*/
}

@Override
public int clampViewPositionVertical(View child, int top, int dy) {
//撤销
if (top < 0) {
return 0;
} else if (top > (mHeight - child.getHeight())) {//往右边滑动不可超过直播的宽度
return mHeight - child.getHeight();
} else {
return top;
}
}

@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
// changedView.offsetTopAndBottom(-dy);//自己撤销回去?拉多少撤销多少 如果高度 不变的话 clampViewPositionVertical return top就可以了


}

/**
* 手指释放的时候回调
*/
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
if (releasedChild == videoView) {
int mY = releasedChild.getTop();
if (releasedChild.getTop() < 0) {
mY = 0;
}
if (releasedChild.getBottom() > getHeight()) {
mY = getHeight() - releasedChild.getMeasuredHeight();
}
if (releasedChild.getRight() - releasedChild.getMeasuredWidth() / 2 > getWidth() / 2) {
initPointPosition.x = (int) (getWidth() - videoView.getWidth() * mPercent);
initPointPosition.y = mY;
mDragHelper.settleCapturedViewAt(initPointPosition.x, initPointPosition.y);

} else {
initPointPosition.x = (int) (0 - videoView.getWidth() * (1 - mPercent));
initPointPosition.y = mY;
mDragHelper.settleCapturedViewAt(initPointPosition.x, initPointPosition.y);

}

invalidate();
}


}


/**
* 不进行复写的话 没法拖动 直接被onInte return false;
* @param child
* @return
*/
@Override
public int getViewHorizontalDragRange(View child) {
return getMeasuredWidth() - child.getMeasuredWidth();
}


@Override
public int getViewVerticalDragRange(View child) {
return getMeasuredHeight() - child.getMeasuredHeight();
}
/* @Override
public int getViewVerticalDragRange(View child) {
return getMeasuredHeight() - child.getMeasuredHeight();
}*/
}

);

mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.DIRECTION_ALL);

}

@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
invalidate();
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(videoView!=null){

if (!isNotFirst) {
initPointPosition.x = videoView.getLeft();
initPointPosition.y = videoView.getTop();
Prt.d(TAG, "first position:x:" + initPointPosition.x + ",y:" + initPointPosition.y);
isNotFirst = true;
}

videoView.layout(initPointPosition.x, initPointPosition.y,
initPointPosition.x + videoView.getMeasuredWidth(),
initPointPosition.y + videoView.getMeasuredHeight());
}

}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
final int action = MotionEventCompat.getActionMasked(event);
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
return false;
}
return mDragHelper.shouldInterceptTouchEvent(event);
// return mDragHelper.shouldInterceptTouchEvent(event);
/* if (mDragHelper.shouldInterceptTouchEvent(event)) {
return true;
} else {
return super.onInterceptTouchEvent(event);
}*/
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
// mDragHelper.cancel();
return false;
}
mDragHelper.processTouchEvent(event);
return true;
}


@Override
protected void onFinishInflate() {
super.onFinishInflate();
videoView = getChildAt(0);
post(new Runnable() {
@Override
public void run() {
mWidth = getWidth();
mHeight = getHeight();
}
});
}
}