android中的字体闪烁效果和跑马灯效果

时间:2022-08-25 12:58:14

android中的字体闪烁效果和跑马灯效果

代码:MainActivity   

package com.wsj.textviewdmeo;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv_text;
private int clo = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = (TextView) findViewById(R.id.tv_text);
shark();
}

private void shark() {
Timer timer = new Timer();
TimerTask taskcc = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (clo == 0) {
clo = 1;
tv_text.setTextColor(Color.TRANSPARENT);
} else {
if (clo == 1) {

clo = 2;
tv_text.setTextColor(Color.YELLOW);
} else if (clo == 2) {

clo = 3;
tv_text.setTextColor(Color.RED);

} else {
clo = 0;
tv_text.setTextColor(Color.BLUE);
}
}
}
});
}
};
timer.schedule(taskcc, 1, 100); //<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">第二个参数分别是delay(多长时间后执行),第三个参数是:duration(执行间隔)单位为:ms</span>
}
}

跑马灯效果:需要在TextView中设置宽度  当文本超过宽度的时候跑马灯

 <TextView
android:id="@+id/tv_news_navigat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text=""
android:layout_centerVertical="true"
android:textColor="@color/white" />