Skip to content

Commit 9343476

Browse files
committed
[功能优化]: 优化悬浮窗位置
1 parent 0bd5eda commit 9343476

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

player_component/src/main/java/com/xyoye/player_component/widgets/popup/PlayerPopupManager.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class PlayerPopupManager(
9696
val popupSize = computerPopupSize(player.getVideoSize())
9797
//悬浮窗位置
9898
mPosition = Point(
99-
appContext.getScreenWidth(false) - popupSize.x - PopupGestureHandler.POPUP_MARGIN,
99+
appContext.getScreenWidth(false) - popupSize.x - PopupGestureHandler.POPUP_MARGIN_X,
100100
((appContext.getScreenHeight() - popupSize.y) * 0.5).toInt()
101101
)
102102

@@ -163,7 +163,7 @@ class PlayerPopupManager(
163163
val currentOrientation = appContext.resources.configuration.orientation
164164
if (currentOrientation != mOrientation) {
165165
mOrientation = currentOrientation
166-
mGestureHandler.correctPosition(appContext, width)
166+
mGestureHandler.correctPosition(appContext, width, height)
167167
}
168168
}
169169
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.xyoye.player_component.widgets.popup
2+
3+
import android.animation.TypeEvaluator
4+
import android.graphics.Point
5+
6+
7+
/**
8+
* Created by xyoye on 2023/3/25.
9+
*/
10+
11+
class PointEvaluator : TypeEvaluator<Point> {
12+
override fun evaluate(fraction: Float, startValue: Point, endValue: Point): Point {
13+
val x = startValue.x + fraction * (endValue.x - startValue.x)
14+
val y = startValue.y + fraction * (endValue.y - startValue.y)
15+
return Point(x.toInt(), y.toInt())
16+
}
17+
}

player_component/src/main/java/com/xyoye/player_component/widgets/popup/PopupGestureHandler.kt

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.xyoye.player_component.widgets.popup
22

3-
import android.animation.ObjectAnimator
43
import android.animation.ValueAnimator
54
import android.annotation.SuppressLint
65
import android.content.Context
@@ -10,6 +9,7 @@ import android.view.View
109
import android.view.animation.DecelerateInterpolator
1110
import androidx.core.animation.addListener
1211
import com.xyoye.common_component.utils.dp2px
12+
import com.xyoye.common_component.utils.getScreenHeight
1313
import com.xyoye.common_component.utils.getScreenWidth
1414

1515

@@ -23,7 +23,8 @@ class PopupGestureHandler(
2323
) : View.OnTouchListener {
2424

2525
companion object {
26-
val POPUP_MARGIN = dp2px(10)
26+
val POPUP_MARGIN_X = dp2px(10)
27+
val POPUP_MARGIN_Y = dp2px(50)
2728
}
2829

2930
private var lastX = 0f
@@ -60,7 +61,7 @@ class PopupGestureHandler(
6061
lastY = event.rawY
6162
}
6263
MotionEvent.ACTION_UP -> {
63-
correctPosition(v.context, v.width)
64+
correctPosition(v.context, v.width, v.height)
6465
}
6566

6667
}
@@ -86,21 +87,34 @@ class PopupGestureHandler(
8687
/**
8788
* 位置修正
8889
*/
89-
fun correctPosition(context: Context, viewWidth: Int) {
90+
fun correctPosition(context: Context, viewWidth: Int, viewHeight: Int) {
9091
val screenWidth = context.applicationContext.getScreenWidth(false)
92+
val screenHeight = context.applicationContext.getScreenHeight(false)
9193

9294
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
96101
}
97102

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 {
99116
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)
104118
}
105119
}
106120
startAnimator()

0 commit comments

Comments
 (0)