公告消息跑马灯效果,参考:https://github.com/sfsheng0322/MarqueeView 开源项目,根据自己业务需求修改成了定时3秒推送一条新的公告消息到滚动栏上显示
<com.aplk.marquee.MarqueeView
android:id="@+id/marqueeView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:background="#88303F9F"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:mvAnimDuration="800"
app:mvDirection="bottom_to_top"
app:mvGravity="left"
app:mvInterval="2500"
app:mvSingleLine="true"
app:mvTextColor="#ffffff"
app:mvTextSize="14sp"/>
private Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
try {
if (msg.what == 100) {
if (num < 100) {
num++;
}else{
num=0;
}
String notice = "现在开始播送广告:" + num;
mReceiveInfo.add(notice);
if (mInfo != null) {
if (mInfo.size() < 10) {
mInfo.add(notice);
} else {
mInfo.remove(0);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
private Runnable mRunnable = new Runnable() {
public void run() {
mhandler.sendEmptyMessage(100);
mhandler.postDelayed(mRunnable, 3000); //给自己发送消息,自运行
}
};
@Override
public void onPause() {
super.onPause();
mhandler.removeCallbacks(mRunnable);
}
@Override
public void onResume() {
super.onResume();
mhandler.post(mRunnable);
}
@Override
public void onDestroy() {
mhandler.removeCallbacks(mRunnable);
super.onDestroy();
}
mInfo = new ArrayList<>();
mInfo.add("准备开始推送公告了,请注意!");
mMarquee.startWithList(mInfo, R.anim.anim_bottom_in, R.anim.anim_top_out);
mMarquee.setAnimationEndListener(new MarqueeView.AnimationEndListener() {
@Override
public void onAnimationEnd() {
if (mReceiveInfo != null && mReceiveInfo.size() > 0) {
mMarquee.createNextTextView(mReceiveInfo.get(0));
mReceiveInfo.remove(0);
}else{
mMarquee.createNextTextView("");
}
}
});
mMarquee.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
@Override
public void onItemClick(CharSequence text, TextView textView) {
Toast.makeText(MainActivity.this,"点击了公告:"+text,Toast.LENGTH_SHORT).show();
}
});
@Override
public void onStart() {
super.onStart();
mMarquee.startFlipping();
}
@Override
public void onStop() {
super.onStop();
mMarquee.stopFlipping();
}