Visitar URL original
优化可拖拽view自动吸边代码 · jacker-java/AndroidCustomView@4bb47b7 · GitHub
Skip to content

Commit 4bb47b7

Browse files
committed
优化可拖拽view自动吸边代码
1 parent 46d1a0f commit 4bb47b7

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

‎app/src/main/java/com/allen/androidcustomview/listener/OnDragTouchListener.kt‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.allen.androidcustomview.listener
33
import android.animation.Animator
44
import android.animation.AnimatorSet
55
import android.animation.ValueAnimator
6-
import android.annotation.SuppressLint
76
import android.view.MotionEvent
87
import android.view.View
98
import android.view.ViewGroup
@@ -46,7 +45,11 @@ class OnDragTouchListener : View.OnTouchListener {
4645
//吸附在边缘时候距离边界的距离
4746
var mBorderMargin = 0f
4847

49-
@SuppressLint("ClickableViewAccessibility")
48+
var mBorderMarginLeft = -1f
49+
var mBorderMarginRight = -1f
50+
var mBorderMarginTop = -1f
51+
var mBorderMarginBottom = -1f
52+
5053
override fun onTouch(view: View, event: MotionEvent): Boolean {
5154
when (event.action) {
5255
MotionEvent.ACTION_DOWN -> {
@@ -96,6 +99,8 @@ class OnDragTouchListener : View.OnTouchListener {
9699
setAutoToBorder(view)
97100
}
98101
view.parent.requestDisallowInterceptTouchEvent(false)
102+
//调取performClick()方法消除警告OnDragTouchListener#onTouch should call View#performClick when a click is detected more...
103+
view.performClick()
99104
}
100105
}
101106
return true
@@ -129,12 +134,12 @@ class OnDragTouchListener : View.OnTouchListener {
129134
*/
130135
private fun getLeftOrRightAnimation(v: View): Animator {
131136
//当用户拖拽完后,让控件回到最近的边缘
132-
var leftAndRightEnd = mBorderMargin
137+
var leftOrRightEnd = getBorderMargin(mBorderMarginLeft)
133138
//吸附在右边边界处
134139
if (left + v.width / 2 >= mMaxWidth / 2) {
135-
leftAndRightEnd = (mMaxWidth - v.width - mBorderMargin)
140+
leftOrRightEnd = (mMaxWidth - v.width - getBorderMargin(mBorderMarginRight))
136141
}
137-
val animator = ValueAnimator.ofFloat(left.toFloat(), leftAndRightEnd)
142+
val animator = ValueAnimator.ofFloat(left.toFloat(), leftOrRightEnd)
138143
animator.interpolator = DecelerateInterpolator()
139144
animator.addUpdateListener { animation ->
140145
val leftMargin = (animation.animatedValue as Float).toInt()
@@ -154,18 +159,19 @@ class OnDragTouchListener : View.OnTouchListener {
154159
return when {
155160
//吸附到距离底部mBorderMargin的距离的位置
156161
top + v.height >= mMaxHeight - mBorderMargin -> {
157-
topOrBottomEnd = mMaxHeight - v.height - mBorderMargin
162+
topOrBottomEnd = mMaxHeight - v.height - getBorderMargin(mBorderMarginBottom)
158163
createTopOrBottomAnimation(v, topOrBottomEnd)
159164
}
160165
//吸附到距离顶部mBorderMargin的距离的位置
161166
top <= mBorderMargin -> {
162-
topOrBottomEnd = mBorderMargin
167+
topOrBottomEnd = getBorderMargin(mBorderMarginTop)
163168
createTopOrBottomAnimation(v, topOrBottomEnd)
164169
}
165170
else -> null
166171
}
167172
}
168173

174+
169175
/**
170176
* 创建底吸附到底部或顶部的动画
171177
*/
@@ -180,5 +186,11 @@ class OnDragTouchListener : View.OnTouchListener {
180186
return animator
181187
}
182188

189+
/**
190+
* 获取吸附的边界值
191+
*/
192+
private fun getBorderMargin(margin: Float): Float {
193+
return if (margin != -1f) margin else mBorderMargin
194+
}
183195

184196
}

0 commit comments

Comments
 (0)