From c49cdd6d9e3da42b378755f10c728351a7c86825 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 16 May 2022 11:54:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=8C=E8=AF=8D=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E5=9D=90=E6=A0=87=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/toside/music/mobile/lyric/Lyric.java | 5 -- .../music/mobile/lyric/LyricModule.java | 3 - .../toside/music/mobile/lyric/LyricView.java | 85 ++++++++++--------- src/utils/lyricDesktop.js | 1 - 4 files changed, 43 insertions(+), 51 deletions(-) diff --git a/android/app/src/main/java/cn/toside/music/mobile/lyric/Lyric.java b/android/app/src/main/java/cn/toside/music/mobile/lyric/Lyric.java index 75a821d..66a19aa 100644 --- a/android/app/src/main/java/cn/toside/music/mobile/lyric/Lyric.java +++ b/android/app/src/main/java/cn/toside/music/mobile/lyric/Lyric.java @@ -158,11 +158,6 @@ public class Lyric extends LyricPlayer { lyricView.unlockView(); } - public void setPosition(int x, int y) { - if (lyricView == null) return; - lyricView.setPosition(x, y); - } - public void setMaxLineNum(int maxLineNum) { if (lyricView == null) return; lyricView.setMaxLineNum(maxLineNum); diff --git a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricModule.java b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricModule.java index 9f0263a..ad4c01d 100644 --- a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricModule.java +++ b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricModule.java @@ -120,9 +120,6 @@ public class LyricModule extends ReactContextBaseJavaModule { @ReactMethod public void setTextSize(float size) { lyric.setTextSize(size); } - @ReactMethod - public void setPosition(int x, int y) { lyric.setPosition(x, y); } - @ReactMethod public void setMaxLineNum(int maxLineNum) { lyric.setMaxLineNum(maxLineNum); } diff --git a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricView.java b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricView.java index 3320a85..77e0ce6 100644 --- a/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricView.java +++ b/android/app/src/main/java/cn/toside/music/mobile/lyric/LyricView.java @@ -67,7 +67,7 @@ public class LyricView extends Activity implements View.OnTouchListener { private OrientationEventListener orientationEventListener = null; final Handler fixViewPositionHandler; - final Runnable fixViewPositionRunnable = this::fixViewPosition; + final Runnable fixViewPositionRunnable = this::updateViewPosition; LyricView(ReactApplicationContext reactContext, LyricEvent lyricEvent) { this.reactContext = reactContext; @@ -86,14 +86,14 @@ public class LyricView extends Activity implements View.OnTouchListener { //rotation changed // if (rotation == Surface.ROTATION_90){} // check rotations here // if (rotation == Surface.ROTATION_270){} // - Log.d("Lyric", "rotation: " + rotation); + // Log.d("Lyric", "rotation: " + rotation); fixViewPositionHandler.postDelayed(fixViewPositionRunnable, 300); } mLastRotation = rotation; } }; } - Log.d("Lyric", "orientationEventListener: " + orientationEventListener.canDetectOrientation()); + // Log.d("Lyric", "orientationEventListener: " + orientationEventListener.canDetectOrientation()); if (orientationEventListener.canDetectOrientation()) { orientationEventListener.enable(); } @@ -132,36 +132,37 @@ public class LyricView extends Activity implements View.OnTouchListener { } private void fixViewPosition() { + int maxX = maxWidth - layoutParams.width; + int x = (int)(maxWidth * prevViewPercentageX); + if (x < 0) x = 0; + else if (x > maxX) x = maxX; + if (layoutParams.x != x) layoutParams.x = x; + + int maxY = maxHeight - layoutParams.height; + int y = (int)(maxHeight * prevViewPercentageY); + if (y < 0) y = 0; + else if (y > maxY) y = maxY; + if (layoutParams.y != y) layoutParams.y = y; + } + + private void updateViewPosition() { if (!updateWH()) return; int width = (int)(maxWidth * widthPercentage); if (layoutParams.width != width) layoutParams.width = width; - layoutParams.x = (int)(maxWidth * prevViewPercentageX); - layoutParams.y = (int)(maxHeight * prevViewPercentageY); - - // int maxX = this.maxWidth - layoutParams.width; - // int maxY = this.maxHeight - layoutParams.height; - // int x = layoutParams.x; - // int y = layoutParams.y; - // if (x < 0) x = 0; - // else if (x > maxX) x = maxX; - // if (y < 0) y = 0; - // else if (y > maxY) y = maxY; - - // layoutParams.x = x; - // layoutParams.y = y; - Log.d("Lyric", "widthPercentage: " + widthPercentage + " prevViewPercentageX: " + prevViewPercentageX); - Log.d("Lyric", "prevViewPercentageY: " + prevViewPercentageY + " layoutParams.x: " + layoutParams.x); - Log.d("Lyric", "layoutParams.y: " + layoutParams.y + " layoutParams.width: " + layoutParams.width); + fixViewPosition(); + // Log.d("Lyric", "widthPercentage: " + widthPercentage + " prevViewPercentageX: " + prevViewPercentageX); + // Log.d("Lyric", "prevViewPercentageY: " + prevViewPercentageY + " layoutParams.x: " + layoutParams.x); + // Log.d("Lyric", "layoutParams.y: " + layoutParams.y + " layoutParams.width: " + layoutParams.width); windowManager.updateViewLayout(textView, layoutParams); } - public void sendPositionEvent(int x, int y) { + public void sendPositionEvent(float x, float y) { WritableMap params = Arguments.createMap(); - params.putInt("x", x); - params.putInt("y", y); + params.putDouble("x", x); + params.putDouble("y", y); lyricEvent.sendEvent(lyricEvent.SET_VIEW_POSITION, params); } @@ -188,16 +189,15 @@ public class LyricView extends Activity implements View.OnTouchListener { public void showLyricView(Bundle options) { isLock = options.getBoolean("isLock", isLock); themeColor = options.getString("themeColor", themeColor); - prevViewPercentageX = (int) options.getDouble("lyricViewX", 0) / 100f; - prevViewPercentageY = (int) options.getDouble("lyricViewY", 0) / 100f; + prevViewPercentageX = (float) options.getDouble("lyricViewX", 0f) / 100f; + prevViewPercentageY = (float) options.getDouble("lyricViewY", 0f) / 100f; textX = options.getString("textX", textX); textY = options.getString("textY", textY); alpha = (float) options.getDouble("alpha", alpha); textSize = (float) options.getDouble("textSize", textSize); - widthPercentage = (int) options.getDouble("width", 100) / 100f; + widthPercentage = (float) options.getDouble("width", 100) / 100f; maxLineNum = (int) options.getDouble("maxLineNum", maxLineNum); handleShowLyric(); - fixViewPosition(); listenOrientationEvent(); } public void showLyricView() { @@ -207,7 +207,6 @@ public class LyricView extends Activity implements View.OnTouchListener { Log.e("Lyric", e.getMessage()); return; } - fixViewPosition(); listenOrientationEvent(); } @@ -304,10 +303,6 @@ public class LyricView extends Activity implements View.OnTouchListener { updateWH(); - //显示位置与指定位置的相对位置差 - layoutParams.x = (int)(maxWidth * prevViewPercentageX); - layoutParams.y = (int)(maxHeight * prevViewPercentageY); - //悬浮窗的宽高 // layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; // layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; @@ -317,6 +312,12 @@ public class LyricView extends Activity implements View.OnTouchListener { // layoutParams.height = 100; layoutParams.height = textView.getPaint().getFontMetricsInt(null) * maxLineNum + 8; + //显示位置与指定位置的相对位置差 + layoutParams.x = (int)(maxWidth * prevViewPercentageX); + layoutParams.y = (int)(maxHeight * prevViewPercentageY); + + fixViewPosition(); + //设置透明 layoutParams.format = PixelFormat.TRANSPARENT; @@ -338,18 +339,18 @@ public class LyricView extends Activity implements View.OnTouchListener { textView.setText(text); } - public void setPosition(int x, int y) { - if (textView == null) return; - layoutParams.x = x; - layoutParams.y = y; - windowManager.updateViewLayout(textView, layoutParams); - } - public void setMaxLineNum(int maxLineNum) { if (textView == null) return; this.maxLineNum = maxLineNum; textView.setMaxLines(maxLineNum); layoutParams.height = textView.getPaint().getFontMetricsInt(null) * maxLineNum + 8; + + int maxY = maxHeight - layoutParams.height; + int y = layoutParams.y; + if (y < 0) y = 0; + else if (y > maxY) y = maxY; + if (layoutParams.y != y) layoutParams.y = y; + windowManager.updateViewLayout(textView, layoutParams); } @@ -392,9 +393,9 @@ public class LyricView extends Activity implements View.OnTouchListener { tranY = nowY - lastY; int x = layoutParams.x + (int)tranX; - int y = layoutParams.y + (int)tranY; if (x < 0) x = 0; else if (x > maxX) x = maxX; + int y = layoutParams.y + (int)tranY; if (y < 0) y = 0; else if (y > maxY) y = maxY; @@ -426,8 +427,8 @@ public class LyricView extends Activity implements View.OnTouchListener { //根据移动的位置来判断 // dy = 0; tranY = 0; - int percentageX = (int)((float)layoutParams.x / (float) maxWidth * 100f); - int percentageY = (int)((float)layoutParams.y / (float) maxHeight * 100f); + float percentageX = (float)layoutParams.x / (float) maxWidth * 100f; + float percentageY = (float)layoutParams.y / (float) maxHeight * 100f; if (percentageX != prevViewPercentageX || percentageY != prevViewPercentageY) { prevViewPercentageX = percentageX / 100f; prevViewPercentageY = percentageY / 100f; diff --git a/src/utils/lyricDesktop.js b/src/utils/lyricDesktop.js index b54af87..fa7924b 100644 --- a/src/utils/lyricDesktop.js +++ b/src/utils/lyricDesktop.js @@ -166,7 +166,6 @@ export const setPosition = (x, y) => { } export const setMaxLineNum = maxLineNum => { - console.log(maxLineNum, isShowLyric) if (!isShowLyric) return Promise.resolve() return LyricModule.setMaxLineNum(maxLineNum) }