- 在下滑时,随着手指的移动,图片区域跟随移动,并且activity的背景和页码逐渐变的透明
- 滑动距离不超过设定的临界值时,会有回弹效果。
- 滑动超过设置的临界值时,放开手指,页面滑动退出消失
- 图片可以正常放大缩小,页面不跟随手指上下滑动
- 使用了共享元素的页面切换效果
上代码
class SlideCloseLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) {
private var previousX: Float = 0f
private var previousY: Float = 0f
private var scrollListener: LayoutScrollListener? = null
init {
background?.alpha = 255
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
ev?.pointerCount?.let {
if (it > 1) return false
val y: Float = ev.rawY
val x: Float = ev.rawX
when (ev.action) {
MotionEvent.ACTION_DOWN -> {
previousX = x
previousY = y
}
MotionEvent.ACTION_MOVE -> {
val diffY = y - previousY
val diffX = x - previousX
if (diffY <= 0) return false
if (Math.abs(diffX) + 50 < Math.abs(diffY)) {
return true
}
}
}
}
return false
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
ev?.let {
val y = ev.rawY
val x = ev.rawX
when (ev.action) {
MotionEvent.ACTION_DOWN -> {
previousX = x
previousY = y
}
MotionEvent.ACTION_MOVE -> {
val d