添加翻译

This commit is contained in:
lyswhut 2021-08-12 13:11:14 +08:00
parent cdabeae985
commit 0f19c38840
4 changed files with 48 additions and 21 deletions

View File

@ -9,6 +9,7 @@ import android.util.Log;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -18,10 +19,16 @@ public class Lyric extends LyricPlayer {
ReactApplicationContext reactAppContext;
boolean isShowLyric = false;
String lastText = "LX Music ^-^";
// String lastText = "LX Music ^-^";
int lastLine = 0;
List lines = new ArrayList();
boolean isShowTranslation = false;
String lyricText = "";
String lyricTransText = "";
Lyric(ReactApplicationContext reactContext) {
Lyric(ReactApplicationContext reactContext, boolean isShowTranslation) {
this.reactAppContext = reactContext;
this.isShowTranslation = isShowTranslation;
registerScreenBroadcastReceiver();
}
@ -73,12 +80,23 @@ public class Lyric extends LyricPlayer {
@Override
public void run() {
lyricView.showLyricView();
lyricView.setLyric(lastText);
setViewLyric(lastLine);
setTempPause(false);
}
});
}
private void setViewLyric(int lineNum) {
lastLine = lineNum;
if (lyricView == null) return;
HashMap line = (HashMap) lines.get(lineNum);
if (line == null) {
lyricView.setLyric("", "");
} else {
lyricView.setLyric((String) line.get("text"), (String) line.get("translation"));
}
}
public void showLyric(boolean isLock, String themeColor, int lyricViewX, int lyricViewY, String textX, String textY, Promise promise) {
if (lyricEvent == null) lyricEvent = new LyricEvent(reactAppContext);
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
@ -104,23 +122,23 @@ public class Lyric extends LyricPlayer {
@Override
public void setLyric(String lyric, String translationLyric) {
if (lyricView != null) super.setLyric(lyric, translationLyric);
lyricText = lyric;
lyricTransText = translationLyric;
if (lyricView != null) super.setLyric(lyric, isShowTranslation ? translationLyric : "");
}
@Override
public void onSetLyric(List lines) {
for (int i = 0; i < lines.size(); i++) {
HashMap line = (HashMap) lines.get(i);
this.lines = lines;
// for (int i = 0; i < lines.size(); i++) {
// HashMap line = (HashMap) lines.get(i);
// Log.d("Lyric", (String) line.get("text") + " " + (String) line.get("translation"));
}
// }
}
@Override
public void onPlay(int lineNum, String text) {
// HashMap line = (HashMap) lines.get(lineNum);
lastText = text;
if (lyricView == null) return;
lyricView.setLyric(text);
setViewLyric(lineNum);
// Log.d("Lyric", lineNum + " " + text + " " + (String) line.get("translation"));
}
@ -135,7 +153,8 @@ public class Lyric extends LyricPlayer {
}
public void toggleTranslation(boolean isShowTranslation) {
this.isShowTranslation = isShowTranslation;
if (lyricView != null) super.setLyric(lyricText, isShowTranslation ? lyricTransText : "");
}
public void setColor(String color) {

View File

@ -16,6 +16,8 @@ public class LyricModule extends ReactContextBaseJavaModule {
Lyric lyric;
// final Map<String, Object> constants = new HashMap<>();
boolean isShowTranslation = false;
LyricModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
@ -42,7 +44,7 @@ public class LyricModule extends ReactContextBaseJavaModule {
@ReactMethod
public void showLyric(boolean isLook, String themeColor, int lyricViewX, int lyricViewY, String textX, String textY , Promise promise) {
if (lyric == null) lyric = new Lyric(reactContext);
if (lyric == null) lyric = new Lyric(reactContext, isShowTranslation);
lyric.showLyric(isLook, themeColor, lyricViewX, lyricViewY, textX, textY, promise);
}
@ -63,7 +65,9 @@ public class LyricModule extends ReactContextBaseJavaModule {
@ReactMethod
public void toggleTranslation(boolean isShowTranslation, Promise promise) {
this.isShowTranslation = isShowTranslation;
if (lyric == null) return;
lyric.toggleTranslation(isShowTranslation);
promise.resolve(null);
}

View File

@ -1,5 +1,7 @@
package com.lxmusicmobile.lyric;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
@ -21,8 +23,6 @@ import com.facebook.react.bridge.WritableMap;
import cn.toside.music.mobile.R;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
public class LyricView extends Activity implements View.OnTouchListener {
TextView textView = null;
WindowManager windowManager = null;
@ -151,7 +151,7 @@ public class LyricView extends Activity implements View.OnTouchListener {
textView.setGravity(textPositionX | textPositionY);
textView.setTextColor(Color.parseColor(themeColor));
textView.setShadowLayer(1, 0, 0, Color.BLACK);
textView.setMaxLines(2);
textView.setMaxLines(4);
textView.setEllipsize(TextUtils.TruncateAt.END);
//监听 OnTouch 事件 为了实现"移动歌词"功能
@ -190,7 +190,7 @@ public class LyricView extends Activity implements View.OnTouchListener {
// layoutParams.height= DisplayUtil.dp2px(mContext,55);
layoutParams.width = MATCH_PARENT;
// layoutParams.height = 100;
layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, reactContext.getResources().getDisplayMetrics());
layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, reactContext.getResources().getDisplayMetrics());
//设置透明
layoutParams.format = PixelFormat.TRANSPARENT;
@ -199,9 +199,13 @@ public class LyricView extends Activity implements View.OnTouchListener {
windowManager.addView(textView, layoutParams);
}
public void setLyric(String text) {
public void setLyric(String text, String transText) {
if (textView == null) return;
textView.setText(text);
if (transText.equals("")) {
textView.setText(text);
} else {
textView.setText(text + "\n" + transText);
}
}
@Override

View File

@ -99,7 +99,7 @@ export const setLyric = (lyric, translation) => {
* @returns {Promise} Promise
*/
export const toggleTranslation = isShowTranslation => {
if (!isShowLyric) return Promise.resolve()
// if (!isShowLyric) return Promise.resolve()
return LyricModule.toggleTranslation(isShowTranslation)
}