用TextSwitche实现滚动式小广播是项目中比较常见的做法,首先看效果图:

创建两个anim,用于控制文字进出的方式:
in_swithcer.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="800"
android:fromYDelta="100%p"
android:toYDelta="0%p" />
</set>
out_switcher.xml:
?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="800"
android:fromYDelta="0%p"
android:toYDelta="-100%p" />
</set>
然后是mainactivity布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#e3e3e3"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:background="#ffffff"
android:layout_height="50dp"
>
<ImageView
android:id="@+id/horn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@mipmap/horn" />
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:foregroundGravity="center"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/horn"
android:inAnimation="@anim/in_switcher"
android:outAnimation="@anim/out_switcher"
android:textAlignment="center"/>
</RelativeLayout>
</RelativeLayout>
下面贴出MainActivity 全部代码:
public class MainActivity extends AppCompatActivity {
/*展示滚动字幕的控件*/
private TextSwitcher mTextSwitcher;
private int count;
/*滚动字幕*/
private String[] text = {"尾号5449,成功借款3000元",
"尾号2346,成功借款4000元",
"尾号4578,成功借款2000元",
"尾号9587,成功借款5000元",
"尾号7512,成功借款7000元",
"尾号1258,成功借款5000元",
"尾号3597,成功借款8000元",
"尾号7568,成功借款3000元",
"尾号2135,成功借款2000元",
"尾号5449,成功借款3000元"
};
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initEvents();
}
private void initEvents() {
setTextSwitcher();
handler.post(runnable);
}
private void setTextSwitcher() {
mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView = new TextView(MainActivity.this);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
});
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (count == 3 * text.length) {
count = 0;
}
/*取出数组里的数据*/
String str = text[count++ % text.length];
/*定义一个SpannableString对象
用于设置TextView中不同字体的颜色
* */
SpannableString span = new SpannableString(str);
span.setSpan(new ForegroundColorSpan(Color.RED), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextSwitcher.setText(span);
/**设置每隔两秒时间切换一次显示数据*/
handler.postDelayed(runnable, 2000);
}
};
private void initViews() {
mTextSwitcher = findViewById(R.id.textSwitcher);
}
@Override
protected void onDestroy() {
super.onDestroy();
/**
* 当activity销毁的时候
* 销毁线程
* 销毁handler
* */
handler.removeCallbacks(runnable);
}