Android 用ViewFlipper实现跑马灯效果的公告提示

1.代码部分
private void initViewFlipper(final HomepageListModel.Notice notice) {
for (int i = 0; i < notice.noticeTexts.size(); i++) {
TextView view = new TextView(mContext);
ViewFlipper.LayoutParams params = new ViewFlipper.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
view.setLayoutParams(params);
view.setText(notice.noticeTexts.get(i).text_title);
view.setTextColor(mContext.getResources().getColor(R.color.text_666));
view.setTextSize(12); final HomepageListModel.NoticeText noticeText = notice.noticeTexts.get(i);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, NoticeDetailsActivity.class);
intent.putExtra("ad_id", noticeText.click_id);
mContext.startActivity(intent);
}
});
noticeViewFlipper.addView(view);
}
//是否自动开始滚动
noticeViewFlipper.setAutoStart(true);
//滚动时间
noticeViewFlipper.setFlipInterval(2000);
//开始滚动
noticeViewFlipper.startFlipping();
//出入动画
noticeViewFlipper.setOutAnimation(mContext, R.anim.slide_out_top);
noticeViewFlipper.setInAnimation(mContext, R.anim.slide_in_bottom);
} 2.Xml部分
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#f1f1f1"
android:gravity="center_vertical"
android:orientation="horizontal"> <TextView
android:id="@+id/homepage_notice_tv"
android:layout_width="36dp"
android:layout_height="16dp"
android:layout_marginLeft="10dp"
android:background="@drawable/notice_tv_bg"
android:gravity="center"
android:text="公告"
android:textColor="#FA5E5E"
android:textSize="10sp" /> <ViewFlipper
android:id="@+id/notice_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"></ViewFlipper> </LinearLayout> 3.动画部分
slide_in_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="50%p"
android:toYDelta="0" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set> slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="0"
android:toYDelta="-50%p" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
上一篇:remote: Incorrect username or password ( access token )


下一篇:android中实现跑马灯效果以及AutoCompleteTestView与MultiAutoCompleteTextView的学习