其实这属于View事件传递处理的范畴
移动的view就是箭头所指的本地视频窗口
下面给出移动的代码
mBinding.flVideoCallLocalVideo.setOnTouchListener(object :View.OnTouchListener{
override fun onTouch(p0: View?, event: MotionEvent): Boolean {
//得到事件的坐标
val eventX = event.getRawX().toInt()
val eventY = event.getRawY().toInt()
when (event.getAction()) {
MotionEvent.ACTION_DOWN -> {
//第一次记录lastX/lastY
//得到父视图的right/bottom
if(maxRight==0) {//保证只赋一次值
maxRight = mBinding.clAudioCall.getRight();
maxBottom = mBinding.clAudioCall.getBottom();
}
isclick = false
lastX = eventX
lastY = eventY
}
MotionEvent.ACTION_MOVE -> {
isclick = true
//计算事件的偏移
val dx = eventX - lastX
val dy = eventY - lastY
LogUtils.i("ACTION_MOVE",dx.toString()+"\t"+dy)
//根据事件的偏移来移动imageView
var left: Int = mBinding.flVideoCallLocalVideo.getLeft() + dx
var top: Int = mBinding.flVideoCallLocalVideo.getTop() + dy
var right: Int = mBinding.flVideoCallLocalVideo.getRight() + dx
var bottom: Int = mBinding.flVideoCallLocalVideo.getBottom() + dy
//限制left >=0
if (left < 0) {
right += -left
left = 0
}
//限制top
if (top < 0) {
bottom += -top
top = 0
}
//限制right <=maxRight
if (right > maxRight) {
left -= right - maxRight
right = maxRight
}
//限制bottom <=maxBottom
if (bottom > maxBottom) {
top -= bottom - maxBottom
bottom = maxBottom
}
val layoutParams = mBinding.flVideoCallLocalVideo.layoutParams as ConstraintLayout.LayoutParams
layoutParams.setMargins(left, top, maxRight -right, maxBottom-bottom); //控件相对父控件左上右下的距离
layoutParams.width = mBinding.flVideoCallLocalVideo.width;
layoutParams.height = mBinding.flVideoCallLocalVideo.height;
mBinding.flVideoCallLocalVideo.layoutParams = layoutParams;
//不能用layout,它移动后会跑回原位
//mBinding.flVideoCallLocalVideo.layout(left, top, right, bottom)
//再次记录lastX/lastY
lastX = eventX
lastY = eventY
}
MotionEvent.ACTION_CANCEL,MotionEvent.ACTION_UP -> {
isclick = false
}
else -> {}
}
return isclick
}
})
有需要移动和点击同时存在的源码,完整的声网音视频项目代码,可私信我