Skip to content

Commit bb6146b

Browse files
committed
tweak zoom calculation
1 parent 071cf40 commit bb6146b

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

app/src/main/java/com/geode/launcher/GeometryDashActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
387387
}
388388

389389
if (mScreenZoomFit && mScreenZoom != 1.0f) {
390-
frameLayout.scaleX = 1/mScreenZoom
391-
frameLayout.scaleY = 1/mScreenZoom
390+
frameLayout.fitZoom = true
392391
}
393392

394393
if (mScreenZoom != 1.0f) {

app/src/main/java/com/geode/launcher/utils/ConstrainedFrameLayout.kt

+37-18
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,71 @@ package com.geode.launcher.utils
22

33
import android.content.Context
44
import android.widget.FrameLayout
5-
import kotlin.math.roundToInt
5+
import kotlin.math.ceil
6+
import kotlin.math.floor
7+
import kotlin.math.min
68

79
class ConstrainedFrameLayout(context: Context) : FrameLayout(context) {
810
// avoid running the aspect ratio fix when we don't need it
911
var aspectRatio: Float? = null
1012
var zoom: Float? = null
13+
var fitZoom: Boolean = false
1114

1215
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
1518

16-
val currentAspectRatio = aspectRatio
17-
if (currentAspectRatio != null) {
19+
val currentZoom = zoom
20+
if (currentZoom != null) {
1821
val width = MeasureSpec.getSize(widthMeasureSpec)
1922
val height = MeasureSpec.getSize(heightMeasureSpec)
2023
if (width == 0 || height == 0) {
2124
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
2225
return
2326
}
2427

25-
val calculatedWidth = height * currentAspectRatio
26-
val padding = ((width - calculatedWidth) / 2).toInt()
28+
val calculatedWidth = width * currentZoom
29+
val calculatedHeight = height * currentZoom
2730

28-
// ignore paddings where the screen ends up looking odd
29-
paddingX += padding
31+
paddingX = (width - calculatedWidth)/2
32+
paddingY = (height - calculatedHeight)/2
3033
}
3134

32-
val currentZoom = zoom
33-
if (currentZoom != null) {
35+
val currentAspectRatio = aspectRatio
36+
if (currentAspectRatio != null) {
3437
val width = MeasureSpec.getSize(widthMeasureSpec)
3538
val height = MeasureSpec.getSize(heightMeasureSpec)
3639
if (width == 0 || height == 0) {
3740
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
3841
return
3942
}
4043

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
4446

45-
paddingX = (widthFactor * zoomFactor).roundToInt()
46-
paddingY = (heightFactor * zoomFactor).roundToInt()
47+
paddingX = padding
4748
}
4849

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+
)
5170
}
5271

5372
super.onMeasure(

0 commit comments

Comments
 (0)