@@ -2,52 +2,71 @@ package com.geode.launcher.utils
2
2
3
3
import android.content.Context
4
4
import android.widget.FrameLayout
5
- import kotlin.math.roundToInt
5
+ import kotlin.math.ceil
6
+ import kotlin.math.floor
7
+ import kotlin.math.min
6
8
7
9
class ConstrainedFrameLayout (context : Context ) : FrameLayout(context) {
8
10
// avoid running the aspect ratio fix when we don't need it
9
11
var aspectRatio: Float? = null
10
12
var zoom: Float? = null
13
+ var fitZoom: Boolean = false
11
14
12
15
override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
13
- var paddingX = 0
14
- var paddingY = 0
16
+ var paddingX = 0.0f
17
+ var paddingY = 0.0f
15
18
16
- val currentAspectRatio = aspectRatio
17
- if (currentAspectRatio != null ) {
19
+ val currentZoom = zoom
20
+ if (currentZoom != null ) {
18
21
val width = MeasureSpec .getSize(widthMeasureSpec)
19
22
val height = MeasureSpec .getSize(heightMeasureSpec)
20
23
if (width == 0 || height == 0 ) {
21
24
super .onMeasure(widthMeasureSpec, heightMeasureSpec)
22
25
return
23
26
}
24
27
25
- val calculatedWidth = height * currentAspectRatio
26
- val padding = ((width - calculatedWidth) / 2 ).toInt()
28
+ val calculatedWidth = width * currentZoom
29
+ val calculatedHeight = height * currentZoom
27
30
28
- // ignore paddings where the screen ends up looking odd
29
- paddingX + = padding
31
+ paddingX = (width - calculatedWidth) / 2
32
+ paddingY = (height - calculatedHeight) / 2
30
33
}
31
34
32
- val currentZoom = zoom
33
- if (currentZoom != null ) {
35
+ val currentAspectRatio = aspectRatio
36
+ if (currentAspectRatio != null ) {
34
37
val width = MeasureSpec .getSize(widthMeasureSpec)
35
38
val height = MeasureSpec .getSize(heightMeasureSpec)
36
39
if (width == 0 || height == 0 ) {
37
40
super .onMeasure(widthMeasureSpec, heightMeasureSpec)
38
41
return
39
42
}
40
43
41
- val zoomFactor = 1 - currentZoom
42
- val widthFactor = width/ 2.0f + paddingX/ 4.0f
43
- val heightFactor = height/ 2.0f
44
+ val calculatedWidth = (height - paddingY * 2 ) * currentAspectRatio
45
+ val padding = (width - calculatedWidth) / 2
44
46
45
- paddingX = (widthFactor * zoomFactor).roundToInt()
46
- paddingY = (heightFactor * zoomFactor).roundToInt()
47
+ paddingX = padding
47
48
}
48
49
49
- if (paddingX > 0 || paddingY > 0 ) {
50
- setPadding(paddingX, paddingY, paddingX, paddingY)
50
+ if (paddingX > 0.0 || paddingY > 0.0 ) {
51
+ if (fitZoom) {
52
+ val width = MeasureSpec .getSize(widthMeasureSpec)
53
+ val height = MeasureSpec .getSize(heightMeasureSpec)
54
+
55
+ val calculatedHeight = (height - paddingY * 2 )
56
+ val calculatedWidth = (width - paddingX * 2 )
57
+
58
+ val zoomFactor = min(height / calculatedHeight, width / calculatedWidth)
59
+
60
+ scaleX = zoomFactor
61
+ scaleY = zoomFactor
62
+ }
63
+
64
+ setPadding(
65
+ ceil(paddingX).toInt(),
66
+ ceil(paddingY).toInt(),
67
+ floor(paddingX).toInt(),
68
+ floor(paddingY).toInt()
69
+ )
51
70
}
52
71
53
72
super .onMeasure(
0 commit comments