需求:
在TextView 控件中 填入一段文本,根据时间来进行文本的高亮显示,像歌词的效果一样
步骤:
1.在 MainActivity 中 创建一个 Handler 用来进行文本的颜色赋值操作
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case SHOWHIGH:
Bundle b = (Bundle) msg.obj;
int start = b.getInt("start");
int end = b.getInt("end");
SpannableStringBuilder ssb = new SpannableStringBuilder(content);
ssb.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tvShowLrcHighLight.setText(ssb);
break;
}
};
};
2.在 Main Activity中 进行TextView的文本截取
String [] split = content.split("[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?|-]");
这是根据标点符号进行字符串的截取
3.最后开一个子线程 进行 高亮显示的文本的长度设置 并 发送Handler消息进行 UI 更新
new Thread(new Runnable(){
public void run() {
while(flag){
Bundle b = new Bundle();
try {
for (int i = 0; i < split.length; i++) {
start=0;
end+=split[i].length()+start+1;
if(end < content.length()){
b.putInt("start", start);
b.putInt("end", end-1);
Message msg = handler.obtainMessage(SHOWHIGH, b);
handler.sendMessage(msg);
}else{
flag = false;
b.putInt("start", start);
b.putInt("end", content.length());
Message msg = handler.obtainMessage(SHOWHIGH, b);
handler.sendMessage(msg);
}
Thread.sleep(1000); // 这是 一秒钟更新一次UI
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
下面是完整的MainActivity 中的代码
package com.TestLrc;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
protected static final int SHOWHIGH = 0;
private TextView tvShowLrcHighLight;
private int start = 0;
private int end = 1;
private boolean flag = true;
private String content =
"简单点说话的方式简单点,\n" +
"递进的情绪请省略,\n" +
"你又不是个演员,\n" +
"别设计那些情节,\n" +
"没意见我只想看看你怎么圆,\n" +
"你难过的太表面 像没天赋的演员,\n" +
"观众一眼能看见,\n" +
"该配合你演出的我演视而不见,\n" +
"在逼一个最爱你的人即兴表演,\n" +
"什么时候我们开始收起了底线,\n" +
"顺应时代的改变看那些拙劣的表演,\n" +
"可你曾经那么爱我干嘛演出细节,\n" +
"我该变成什么样子才能延缓厌倦,\n" +
"原来当爱放下防备后的这些那些,\n" +
"才是考验,\n" +
"没意见你想怎样我都随便,\n" +
"你演技也有限,\n" + "又不用说感言,\n" + "分开就平淡些!";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShowLrcHighLight = (TextView) findViewById(R.id.tvLRCHighLight);
final String[] split = content.split("[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?|-]");
new Thread(new Runnable() {
public void run() {
while (flag) {
Bundle b = new Bundle();
try {
for (int i = 0; i < split.length; i++) {
start = 0;
end += split[i].length() + start + 1;
if (end < content.length()) {
b.putInt("start", start);
b.putInt("end", end - 1);
Message msg = handler.obtainMessage(SHOWHIGH, b);
handler.sendMessage(msg);
} else {
flag = false;
b.putInt("start", start);
b.putInt("end", content.length());
Message msg = handler.obtainMessage(SHOWHIGH, b);
handler.sendMessage(msg);
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SHOWHIGH:
Bundle b = (Bundle) msg.obj;
int start = b.getInt("start");
int end = b.getInt("end");
SpannableStringBuilder ssb = new SpannableStringBuilder(content);
ssb.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tvShowLrcHighLight.setText(ssb);
break;
}
}
;
};
}