1
1
package com.xyoye.player_component.widgets.popup
2
2
3
- import android.animation.ObjectAnimator
4
3
import android.animation.ValueAnimator
5
4
import android.annotation.SuppressLint
6
5
import android.content.Context
@@ -10,6 +9,7 @@ import android.view.View
10
9
import android.view.animation.DecelerateInterpolator
11
10
import androidx.core.animation.addListener
12
11
import com.xyoye.common_component.utils.dp2px
12
+ import com.xyoye.common_component.utils.getScreenHeight
13
13
import com.xyoye.common_component.utils.getScreenWidth
14
14
15
15
@@ -23,7 +23,8 @@ class PopupGestureHandler(
23
23
) : View.OnTouchListener {
24
24
25
25
companion object {
26
- val POPUP_MARGIN = dp2px(10 )
26
+ val POPUP_MARGIN_X = dp2px(10 )
27
+ val POPUP_MARGIN_Y = dp2px(50 )
27
28
}
28
29
29
30
private var lastX = 0f
@@ -60,7 +61,7 @@ class PopupGestureHandler(
60
61
lastY = event.rawY
61
62
}
62
63
MotionEvent .ACTION_UP -> {
63
- correctPosition(v.context, v.width)
64
+ correctPosition(v.context, v.width, v.height )
64
65
}
65
66
66
67
}
@@ -86,21 +87,34 @@ class PopupGestureHandler(
86
87
/* *
87
88
* 位置修正
88
89
*/
89
- fun correctPosition (context : Context , viewWidth : Int ) {
90
+ fun correctPosition (context : Context , viewWidth : Int , viewHeight : Int ) {
90
91
val screenWidth = context.applicationContext.getScreenWidth(false )
92
+ val screenHeight = context.applicationContext.getScreenHeight(false )
91
93
92
94
val startX = viewPosition.getPosition().x
93
- var endX = POPUP_MARGIN
94
- if (startX * 2 + viewWidth > screenWidth) {
95
- endX = screenWidth - viewWidth - POPUP_MARGIN
95
+ val endX = if (startX * 2 + viewWidth > screenWidth) {
96
+ // 超出屏幕一半时靠右
97
+ screenWidth - viewWidth - POPUP_MARGIN_X
98
+ } else {
99
+ // 靠左
100
+ POPUP_MARGIN_X
96
101
}
97
102
98
- mAnimator = ObjectAnimator .ofInt(startX, endX).apply {
103
+ val startY = viewPosition.getPosition().y
104
+ var endY = startY
105
+ if (endY > screenHeight - viewHeight - POPUP_MARGIN_Y ) {
106
+ // 与底部需要有一定的间距
107
+ endY = screenHeight - viewHeight - POPUP_MARGIN_Y
108
+ } else if (endY < POPUP_MARGIN_Y ) {
109
+ // 与顶部需要有一定的间距
110
+ endY = POPUP_MARGIN_Y
111
+ }
112
+ val startPoint = Point (startX, startY)
113
+ val endPoint = Point (endX, endY)
114
+
115
+ mAnimator = ValueAnimator .ofObject(PointEvaluator (), startPoint, endPoint).apply {
99
116
addUpdateListener {
100
- val x = it.animatedValue as Int
101
- val position = viewPosition.getPosition()
102
- val newPosition = Point (x, position.y)
103
- viewPosition.setPosition(newPosition)
117
+ viewPosition.setPosition(it.animatedValue as Point )
104
118
}
105
119
}
106
120
startAnimator()
0 commit comments