From d669413a268a86c8bb74356509de383e0c20ac62 Mon Sep 17 00:00:00 2001 From: Michel Couillard Date: Thu, 15 Apr 2021 18:26:32 -0400 Subject: [PATCH 1/2] Fix: center inner loop vertically --- android/src/main/java/com/wheelpicker/LoopView.java | 8 +++++++- example/yarn.lock | 12 +++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/wheelpicker/LoopView.java b/android/src/main/java/com/wheelpicker/LoopView.java index 6d650e90..126a7c52 100644 --- a/android/src/main/java/com/wheelpicker/LoopView.java +++ b/android/src/main/java/com/wheelpicker/LoopView.java @@ -190,6 +190,12 @@ protected void onDraw(Canvas canvas) { super.onDraw(canvas); return; } + + // to center loop vertically + int offsetY = (this.getHeight() - measuredHeight) / 2; + firstLineY = (int) ((measuredHeight - lineSpacingMultiplier * maxTextHeight) / 2.0F) + offsetY ; + secondLineY = (int) ((measuredHeight + lineSpacingMultiplier * maxTextHeight) / 2.0F) + offsetY; + as = new String[itemCount]; change = (int) (totalScrollY / (lineSpacingMultiplier * maxTextHeight)); preCurrentIndex = initPosition + change % arrayList.size(); @@ -245,7 +251,7 @@ protected void onDraw(Canvas canvas) { if (angle >= 90F || angle <= -90F) { canvas.restore(); } else { - int translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * maxTextHeight) / 2D); + int translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * maxTextHeight) / 2D) + offsetY; canvas.translate(0.0F, translateY); canvas.scale(1.0F, (float) Math.sin(radian)); if (translateY <= firstLineY && maxTextHeight + translateY >= firstLineY) { diff --git a/example/yarn.lock b/example/yarn.lock index 5f7f2285..f961261d 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -961,6 +961,11 @@ eslint-plugin-react-native "3.6.0" prettier "1.16.4" +"@react-native-community/picker@^1.8.0": + version "1.8.1" + resolved "https://registry.yarnpkg.com/@react-native-community/picker/-/picker-1.8.1.tgz#94f14f0aad98fa7592967b941be97deec95c3805" + integrity sha512-Sj9DzX1CSnmYiuEQ5fQhExoo4XjSKoZkqLPAAybycq6RHtCuWppf+eJXRMCOJki25BlKSSt+qVqg0fIe//ujNQ== + "@types/babel__core@^7.1.0": version "7.1.6" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.6.tgz#16ff42a5ae203c9af1c6e190ed1f30f83207b610" @@ -4421,7 +4426,7 @@ mkdirp@^0.5.1: dependencies: minimist "0.0.8" -moment@^2.22.0, moment@^2.24.0: +moment@^2.24.0: version "2.24.0" resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg== @@ -5073,9 +5078,10 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.9.0: integrity sha512-GFMtL0vHkiBv9HluwNZTggSn/sCyEt9n02aM0dSAjGGyqyNlAyftYm4phPxdvCigG15JreC5biwxCgTAJZ7yAA== react-native-wheel-picker-android@../: - version "2.0.7" + version "3.0.0" dependencies: - moment "^2.22.0" + "@react-native-community/picker" "^1.8.0" + moment "^2.24.0" react-native@0.61.5: version "0.61.5" From d79231ebbad9af822461ee65468d274811756068 Mon Sep 17 00:00:00 2001 From: Michel Couillard Date: Mon, 19 Apr 2021 16:00:19 -0400 Subject: [PATCH 2/2] 2px center adjustment --- android/src/main/java/com/wheelpicker/LoopView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/wheelpicker/LoopView.java b/android/src/main/java/com/wheelpicker/LoopView.java index 126a7c52..f954f9b5 100644 --- a/android/src/main/java/com/wheelpicker/LoopView.java +++ b/android/src/main/java/com/wheelpicker/LoopView.java @@ -192,7 +192,7 @@ protected void onDraw(Canvas canvas) { } // to center loop vertically - int offsetY = (this.getHeight() - measuredHeight) / 2; + int offsetY = (this.getHeight() - measuredHeight) / 2 - 2; firstLineY = (int) ((measuredHeight - lineSpacingMultiplier * maxTextHeight) / 2.0F) + offsetY ; secondLineY = (int) ((measuredHeight + lineSpacingMultiplier * maxTextHeight) / 2.0F) + offsetY;