Browse Source

单词泛红控件修改

czq 2 years ago
parent
commit
541b6c10ac

+ 13 - 10
app/src/main/java/com/xunao/effectdemo/activity/NewSongLyricActivity.java

@@ -47,7 +47,7 @@ import com.zhy.view.flowlayout.TagFlowLayout;
 public class NewSongLyricActivity extends Activity {
 
 	private SongLyricTextView textView;
-	private Button start,stop;
+	private Button start,stop,test;
 	private MediaPlayer mediaPlayer;
 	private String TAG = "NewSongLyricActivity";
 	private TagFlowLayout flowLayout;
@@ -99,6 +99,10 @@ public class NewSongLyricActivity extends Activity {
 			textView.stopPlay();
 			handler.removeCallbacks(runnable);
 		});
+		test = findViewById(R.id.test);
+		test.setOnClickListener(v->textView.startNormalLine(
+				0,
+				startIndex));
 		mediaPlayer = new MediaPlayer();
 		flowLayout = findViewById(R.id.tab_flowlayout);
 		getMedia();
@@ -176,24 +180,23 @@ public class NewSongLyricActivity extends Activity {
 						if (num==beanList.size()){
 							num = 0;
 						}
-						textView.startPlayLine(
-								startIndex,
-								startIndex + timeBean.getWord().length(), // 因为有个换行符,所以 + 1
-								1);
-						startIndex += timeBean.getWord().length();
-
 						if (timeBean.getTime()+timeBean.getStartTime()+canTime == timeBean.getEndTime()){
 							canTime =0;
 							normal = startIndex;
 
 						}else{
 							if (canTime==0){
-//								textView.startNormalLine(
-//										0,
-//										startIndex);
 							}
 							canTime +=timeBean.getTime();
 						}
+
+						textView.startPlayLine(
+								startIndex,
+								startIndex + timeBean.getWord().length(), // 因为有个换行符,所以 + 1
+								normal,1);
+						startIndex += timeBean.getWord().length();
+
+
 						timeBean = beanList.get(num);
 					}
 					break;

+ 2 - 1
app/src/main/java/com/xunao/effectdemo/activity/SongLyricActivity.kt

@@ -60,7 +60,8 @@ class SongLyricActivity : AppCompatActivity() {
                     {
                         text.startPlayLine(
                             startIndexTemp,
-                            min(lyric.length, startIndexTemp + it.length + 1), // 因为有个换行符,所以 + 1
+                            min(lyric.length, startIndexTemp + it.length + 1),
+                            startIndexTemp,// 因为有个换行符,所以 + 1
                             duration
                         )
                     },

+ 20 - 12
app/src/main/java/com/xunao/effectdemo/view/SongLyricTextView.kt

@@ -83,13 +83,13 @@ class SongLyricTextView @JvmOverloads constructor(
                 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
-                    )
+//                    path.addRect(
+//                        layout.getLineLeft(i),
+//                        layout.getLineTop(i).toFloat(),
+//                        layout.getLineRight(i),
+//                        layout.getLineBottom(i).toFloat(),
+//                        Path.Direction.CCW
+//                    )
                 } else {
                     // 如果该行正好是要变色的行,直接改变颜色
                     // 把需要的consumeWidth放入path中
@@ -120,7 +120,6 @@ class SongLyricTextView @JvmOverloads constructor(
 
                 if (lineWidth <= consumeWidth) {
                     // 如果是之前已经变色区域,直接添加到path中
-                    consumeWidth -= lineWidth
                     path.addRect(
                         layout.getLineLeft(i),
                         layout.getLineTop(i).toFloat(),
@@ -128,6 +127,10 @@ class SongLyricTextView @JvmOverloads constructor(
                         layout.getLineBottom(i).toFloat(),
                         Path.Direction.CCW
                     )
+
+                    oldStartWidth -= lineWidth
+                    consumeWidth -= lineWidth
+
                 } else {
                     // 如果该行正好是要变色的行,直接改变颜色
                     // 把需要的consumeWidth放入path中
@@ -149,6 +152,10 @@ class SongLyricTextView @JvmOverloads constructor(
         }
     }
 
+    private var startWidth : Float = 0f
+    private var endWidth : Float = 0f
+    private var oldStartWidth : Float = 0f
+
     /**
      * 开始播放
      *
@@ -156,12 +163,13 @@ class SongLyricTextView @JvmOverloads constructor(
      * @param endIndex 结束的文字的索引
      * @param duration 播放时长
      * */
-    fun startPlayLine(startIndex: Int, endIndex: Int, duration: Long) {
+    fun startPlayLine(startIndex: Int, endIndex: Int,oldStart: Int, duration: Long) {
         isPlaying = 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)
+        startWidth = mPaint.measureText(this.text.substring(0, startIndex))
+        oldStartWidth = mPaint.measureText(this.text.substring(0, oldStart))
+        endWidth = startWidth!! + mPaint.measureText(this.text.substring(startIndex, endIndex))
+        animator.setFloatValues(startWidth!!, endWidth!!)
         animator.duration = if (duration > 0) duration else 10
         animator.start()
     }

+ 6 - 0
app/src/main/res/layout/activity_songe.xml

@@ -43,4 +43,10 @@
         android:layout_height="wrap_content"
         android:text="结束播放" />
 
+    <Button
+        android:id="@+id/test"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="测试数据" />
+
 </LinearLayout>