Browse Source

Merge branch 'master' of http://116.62.119.248:10082/cheng_zq/EffectDemo into lwz_

LAPTOP-K69FCNBP\crius 2 years ago
parent
commit
362c80a945

+ 1 - 0
.idea/misc.xml

@@ -25,6 +25,7 @@
         <entry key="..\:/Progect/gitee/EffectDemo/app/src/main/res/layout/activity_view.xml" value="0.25" />
         <entry key="..\:/Progect/gitee/EffectDemo/app/src/main/res/layout/activity_web.xml" value="0.1816123188405797" />
         <entry key="..\:/Progect/gitee/EffectDemo/app/src/main/res/layout/demo_activity.xml" value="0.18020833333333333" />
+        <entry key="..\:/Progect/gitee/EffectDemo/app/src/main/res/layout/item_word_sound.xml" value="0.10190217391304347" />
       </map>
     </option>
   </component>

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -144,6 +144,7 @@
         <activity
             android:name=".activity.DragActivity"
             android:screenOrientation="landscape" />
+        <activity android:name=".activity.NewSongLyricActivity"/>
     </application>
 
 </manifest>

+ 93 - 35
app/src/main/java/com/xunao/effectdemo/activity/NewSongLyricActivity.java

@@ -1,19 +1,24 @@
 package com.xunao.effectdemo.activity;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.graphics.Color;
 import android.media.MediaPlayer;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
 import com.xunao.effectdemo.R;
 import com.xunao.effectdemo.bean.MediaBean;
+import com.xunao.effectdemo.bean.WordTimeBean;
 import com.xunao.effectdemo.net.ApiHttpClient;
 import com.xunao.effectdemo.net.ApiUrl;
 import com.xunao.effectdemo.net.CSMHttpCallback;
@@ -24,6 +29,9 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+
 import com.xunao.effectdemo.bean.MediaBean.ResultBean.ListBean.*;
 import com.zhy.view.flowlayout.FlowLayout;
 import com.zhy.view.flowlayout.TagAdapter;
@@ -63,6 +71,9 @@ public class NewSongLyricActivity extends Activity {
 	private String[] lyricList;
 	private List<StepsLyricjsonBean> wordList = new ArrayList<>();
 	private List<String> list = new ArrayList<>();
+	private List<String> timeList = new ArrayList<>();
+	private List<WordTimeBean> beanList = new ArrayList<>();
+	private WordTimeBean timeBean,currBean;
 
 	@Override
 	protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -76,16 +87,17 @@ public class NewSongLyricActivity extends Activity {
 		start = findViewById(R.id.start);
 		stop = findViewById(R.id.end);
 		start.setOnClickListener(v->{
-//			if(mediaPlayer.isPlaying()){
-//				mediaPlayer.seekTo(0);
-//			}else{
-//				mediaPlayer.start();
-//			}
+			if(mediaPlayer.isPlaying()){
+				mediaPlayer.seekTo(0);
+			}else{
+				mediaPlayer.start();
+			}
 			startPlay();
 		});
 		stop.setOnClickListener(v->{
 			mediaPlayer.pause();
-			textView.stopPlay();
+//			textView.stopPlay();
+			handler.removeCallbacks(runnable);
 		});
 		mediaPlayer = new MediaPlayer();
 		flowLayout = findViewById(R.id.tab_flowlayout);
@@ -127,47 +139,93 @@ public class NewSongLyricActivity extends Activity {
 		String msg = "";
 		list.clear();
 		for (StepsLyricjsonBean bean:wordList) {
-			for (String w:
-				 bean.getWord()) {
 
-				w = w.replaceAll("<i>","\n    ");
+			for (int i = 0; i < bean.getWord().size(); i++) {
+				String w = bean.getWord().get(i).replaceAll("<i>","\n    ");
+				if (w.contains("\n"))Log.e("MyTag","长度"+w.length());
 				list.add(w);
+				timeList.add(bean.getWordtims().get(i));
+
+				timeBean = new WordTimeBean();
+				timeBean.setStartTime(Integer.parseInt(bean.getStarttime()));
+				timeBean.setEndTime(Integer.parseInt(bean.getEndtime()));
+				timeBean.setWord(w);
+				timeBean.setTime(Integer.parseInt(bean.getWordtims().get(i)));
+				beanList.add(timeBean);
+
 				msg+=w;
 			}
 		}
 		textView.setText(msg);
 		setFlowData();
 	}
-
-	void startPlay(){
-		String[] lyricList = lyric.split("\n");
-		int startIndex = 0;
-		long delayTime = 0L;
-//            text.startPlayLine(
-//                0,
-//                lyric.length,
-//                5000
-//            )
-		for (String s : lyricList) {
-			int startIndexTemp = startIndex;
-			long duration = 1000;
-			textView.postDelayed(
-					new Runnable() {
-						@Override
-						public void run() {
-							textView.startPlayLine(
-									startIndexTemp,
-									startIndexTemp + s.length() + 1, // 因为有个换行符,所以 + 1
-									0);
+	private int num = 0;
+	private int canTime = 0;
+	private int normal = 0;
+	private Handler handler = new Handler(){
+		@SuppressLint("HandlerLeak")
+		@Override
+		public void handleMessage(@NonNull Message msg) {
+			super.handleMessage(msg);
+			switch (msg.what){
+				case 1001:
+					if (currBean==null){timeBean = beanList.get(num);}
+//					Log.e("MyTag","当前:"+timeBean.toString()+"   "+time);
+					if (timeBean.getStartTime()+canTime > time&& timeBean.getStartTime() + canTime-time   <100){
+						num++;
+						if (num==beanList.size()){
+							num = 0;
 						}
-					},
-					0
-			);
-			delayTime += duration + 50;
-			startIndex += s.length() + 1;
+						if (timeBean.getTime()+timeBean.getStartTime()+canTime == timeBean.getEndTime()){
+							canTime =0;
+//							normal = startIndex;
+//							textView.startNormalLine(
+//									0,
+//									startIndex);
+						}else{
+							canTime +=timeBean.getTime();
+						}
+
+						textView.startPlayLine(
+								startIndex,
+								startIndex + timeBean.getWord().length(), // 因为有个换行符,所以 + 1
+								1);
+						startIndex += timeBean.getWord().length();
+						timeBean = beanList.get(num);
+					}
+					break;
+			}
 		}
+	};
+	int startIndex = 0;
+	long delayTime = 0L;
+
+	void startPlay(){
+//		String[] lyricList = lyric.split("\n");
+		handler.post(runnable);
 	}
 
+
+	Timer timer;
+	int time = 0;
+	private Runnable runnable = new Runnable() {
+		@Override
+		public void run() {
+			timer=new Timer();
+			timer.schedule(new TimerTask() {
+				@Override
+				public void run() {
+					//定义一个消息传过去
+					Message msg=new Message();
+					msg.what=1001;
+					handler.sendMessage(msg);
+					time+=50;
+				}
+			},0,50); //延时0毫秒开始计时,每隔1秒计时一
+			//次
+		}
+	};
+
 	@Override
 	protected void onDestroy() {
 		super.onDestroy();

+ 58 - 0
app/src/main/java/com/xunao/effectdemo/bean/WordTimeBean.java

@@ -0,0 +1,58 @@
+package com.xunao.effectdemo.bean;
+
+/**
+ * author : 程中强
+ * e-mail : 740479946@qq.com
+ * date : 2022/9/2314:28
+ * desc :
+ * version: 1.0
+ */
+public class WordTimeBean {
+
+	private String word;
+	private int time;
+	private int startTime;
+	private int endTime;
+
+	public String getWord() {
+		return word;
+	}
+
+	public void setWord(String word) {
+		this.word = word;
+	}
+
+	public int getTime() {
+		return time;
+	}
+
+	public void setTime(int time) {
+		this.time = time;
+	}
+
+	public int getStartTime() {
+		return startTime;
+	}
+
+	public void setStartTime(int startTime) {
+		this.startTime = startTime;
+	}
+
+	public int getEndTime() {
+		return endTime;
+	}
+
+	public void setEndTime(int endTime) {
+		this.endTime = endTime;
+	}
+
+	@Override
+	public String toString() {
+		return "WordTimeBean{" +
+				"word='" + word + '\'' +
+				", time='" + time + '\'' +
+				", startTime='" + startTime + '\'' +
+				", endTime='" + endTime + '\'' +
+				'}';
+	}
+}

+ 61 - 2
app/src/main/java/com/xunao/effectdemo/view/SongLyricTextView.kt

@@ -27,7 +27,7 @@ class SongLyricTextView @JvmOverloads constructor(
     /**
      * 文字播放的颜色
      * */
-    private val playColor = Color.parseColor("#fec403")
+    private val playColor = Color.parseColor("#ff0000")
 
     /**
      * 要变色的宽度
@@ -38,8 +38,10 @@ class SongLyricTextView @JvmOverloads constructor(
      * 是否正在播放中
      * */
     private var isPlaying = false
+    private var isNormal = false
 
     private val mPaint = paint
+    private val mPaint2 = paint//原来的颜色
 
     /**
      * 要变色的区域
@@ -106,6 +108,45 @@ class SongLyricTextView @JvmOverloads constructor(
             mPaint.color = playColor
             layout.draw(canvas)
         }
+        if (isNormal) {
+            path.reset()
+            val lineCount = layout.lineCount
+            val content = text.toString()
+            for (i in 0 until lineCount) {
+                // 计算一行文字的宽度
+                val lineWidth = mPaint.measureText(
+                    content.substring(layout.getLineStart(i), layout.getLineEnd(i))
+                )
+
+                if (lineWidth <= consumeWidth) {
+                    // 如果是之前已经变色区域,直接添加到path中
+//                    consumeWidth -= lineWidth
+//                    path.addRect(
+//                        layout.getLineLeft(i),
+//                        layout.getLineTop(i).toFloat(),
+//                        layout.getLineRight(i),
+//                        layout.getLineBottom(i).toFloat(),
+//                        Path.Direction.CCW
+//                    )
+                } else {
+                    // 如果该行正好是要变色的行,直接改变颜色
+                    // 把需要的consumeWidth放入path中
+                    path.addRect(
+                        layout.getLineLeft(i),
+                        layout.getLineTop(i).toFloat(),
+                        layout.getLineLeft(i) + consumeWidth,
+                        layout.getLineBottom(i).toFloat(),
+                        Path.Direction.CCW
+                    )
+                    break
+                }
+            }
+            // 设置需要绘制的区域,并着色
+            canvas.clipPath(path)
+            mPaint.color = normalColor
+            layout.draw(canvas)
+            isNormal = false
+        }
     }
 
     /**
@@ -121,7 +162,24 @@ class SongLyricTextView @JvmOverloads constructor(
         val startWidth = mPaint.measureText(this.text.substring(0, startIndex))
         val endWidth = startWidth + mPaint.measureText(this.text.substring(startIndex, endIndex))
         animator.setFloatValues(startWidth, endWidth)
-        animator.duration = if (duration > 0) duration else 1000
+        animator.duration = if (duration > 0) duration else 10
+        animator.start()
+    }
+
+    /**
+     * 开始播放
+     *
+     * @param startIndex 开始的文字的索引
+     * @param endIndex 结束的文字的索引
+     * @param duration 播放时长
+     * */
+    fun startNormalLine(startIndex: Int, endIndex: Int) {
+        isNormal = true
+        if (startIndex == -1) return
+        val startWidth = mPaint.measureText(this.text.substring(0, startIndex))
+        val endWidth = startWidth + mPaint.measureText(this.text.substring(startIndex, endIndex))
+        animator.setFloatValues(startWidth, endWidth)
+        animator.duration = 0
         animator.start()
     }
 
@@ -130,6 +188,7 @@ class SongLyricTextView @JvmOverloads constructor(
      * */
     fun stopPlay() {
         isPlaying = false
+        isNormal = false
         animator.cancel()
         invalidate()
     }

+ 3 - 2
app/src/main/res/layout/activity_songe.xml

@@ -7,9 +7,9 @@
     android:layout_height="match_parent">
     <com.xunao.effectdemo.view.SongLyricTextView
         android:id="@+id/text"
-        android:visibility="gone"
+
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
+        android:layout_height="400dp"
 
         android:lineSpacingExtra="6dp"
         android:text="Hello World!"
@@ -19,6 +19,7 @@
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
     <ScrollView
+        android:visibility="gone"
         android:layout_width="match_parent"
         android:layout_height="400dp">