Android實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式匯總
方法一、使用系統(tǒng)控件ViewFlipper方式:
布局文件:
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:autoStart="true"
android:background="@drawable/warning_bg"
android:flipInterval="3000"
android:inAnimation="@anim/slide_in_bottom"
android:outAnimation="@anim/slide_out_top">
<TextView
android:id="@+id/tv_warning_content1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:gravity="center"
android:singleLine="true"
android:text="有預(yù)警信息有預(yù)警信息有預(yù)警信息"
android:textColor="#000000"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_warning_content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:gravity="center"
android:singleLine="true"
android:text="當(dāng)前天氣狀況當(dāng)前天氣狀況當(dāng)前"
android:textColor="#000000"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_warning_content3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:gravity="center"
android:singleLine="true"
android:text="123456465"
android:textColor="#000000"
android:textSize="16sp"/>
</ViewFlipper>
背景文件:warning_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#34000000"/>
<corners android:radius="80dp"/>
</shape>
切入動(dòng)畫:slide_in_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
切出動(dòng)畫:slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>
注意:如果不在布局文件里設(shè)置: android:autoStart="true", 可以在代碼中動(dòng)態(tài)設(shè)置開始循環(huán)mViewFlipper.startFlipping();
在Activity中顯示正常,但在fragment中可能會(huì)有重影的現(xiàn)象。
方法二、使用三方框架
Gradle:
compile 'com.sunfusheng:marqueeview:1.3.3'
屬性

XML
<com.sunfusheng.marqueeview.MarqueeView android:id="@+id/marqueeView" android:layout_width="match_parent" android:layout_height="30dp" app:mvAnimDuration="1000" app:mvDirection="bottom_to_top" app:mvInterval="3000" app:mvTextColor="@color/white" app:mvTextSize="14sp" app:mvSingleLine="true"/>
設(shè)置字符串列表數(shù)據(jù)
MarqueeView marqueeView = (MarqueeView) findViewById(R.id.marqueeView);
List<String> info = new ArrayList<>();
info.add("11111111111111");
info.add("22222222222222");
info.add("33333333333333");
info.add("44444444444444");
info.add("55555555555555");
info.add("66666666666666");
marqueeView.startWithList(info);
// 在代碼里設(shè)置自己的動(dòng)畫
marqueeView.startWithList(info, R.anim.anim_bottom_in, R.anim.anim_top_out);
設(shè)置字符串?dāng)?shù)據(jù)
String notice = "心中有陽(yáng)光,腳底有力量!心中有陽(yáng)光,腳底有力量!心中有陽(yáng)光,腳底有力量!"; marqueeView.startWithText(notice); // 在代碼里設(shè)置自己的動(dòng)畫 marqueeView.startWithText(notice, R.anim.anim_bottom_in, R.anim.anim_top_out);
設(shè)置事件監(jiān)聽
marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
@Override
public void onItemClick(int position, TextView textView) {
Toast.makeText(getApplicationContext(), String.valueOf(marqueeView1.getPosition()) + ". " + textView.getText(), Toast.LENGTH_SHORT).show();
}
});
重影問(wèn)題可參考以下解決方案
@Override
public void onStart() {
super.onStart();
marqueeView.startFlipping();
}
@Override
public void onStop() {
super.onStop();
marqueeView.stopFlipping();
}
注意:這個(gè)庫(kù)主要還是繼承了ViewFlipper,類似的庫(kù)還有MarqueeViewLibrary,實(shí)現(xiàn)方法基本類似,在Activity中顯示正常,但在fragment中可能會(huì)有重影的現(xiàn)象。
方法三、使用系統(tǒng)控件TextSwitcher實(shí)現(xiàn)
布局文件
<TextSwitcher
android:id="@+id/text_switcher"
android:layout_width="285dp"
android:layout_height="35dp"
android:background="@drawable/warning_bg"/>
背景文件:warning_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#34000000"/>
<corners android:radius="80dp"/>
</shape>
代碼:
private int index = 0;//textview上下滾動(dòng)下標(biāo)
private Handler handler = new Handler();
private boolean isFlipping = false; // 是否啟用預(yù)警信息輪播
private List<String> mWarningTextList = new ArrayList<>();
private void setTextSwitcher() {
mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));
mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView = new TextView(mContext);
textView.setSingleLine();
textView.setTextSize(12);//字號(hào)
textView.setTextColor(Color.parseColor("#ffffff"));
textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
textView.setSingleLine();
textView.setGravity(Gravity.CENTER);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
textView.setLayoutParams(params);
textView.setPadding(25, 0, 25, 0);
return textView;
}
});
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (!isFlipping) {
return;
}
index++;
mTextSwitcher.setText(mWarningTextList.get(index % mWarningTextList.size()));
if (index == mWarningTextList.size()) {
index = 0;
}
startFlipping();
}
};
//開啟信息輪播
public void startFlipping() {
if (mWarningTextList.size() > 1) {
handler.removeCallbacks(runnable);
isFlipping = true;
handler.postDelayed(runnable, 3000);
}
}
//關(guān)閉信息輪播
public void stopFlipping() {
if (mWarningTextList.size() > 1) {
isFlipping = false;
handler.removeCallbacks(runnable);
}
}
//設(shè)置數(shù)據(jù)
private void setData() {
if (mWarningTextList.size() == 1) {
mTextSwitcher.setText(mWarningTextList.get(0));
index = 0;
}
if (mWarningTextList.size() > 1) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
mTextSwitcher.setText(mWarningTextList.get(0));
index = 0;
}
}, 1000);
mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));
startFlipping();
}
}
@Override
public void onResume() {
super.onResume();
startFlipping();
}
@Override
public void onStop() {
super.onStop();
stopFlipping();
}
切入動(dòng)畫:slide_in_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
切出動(dòng)畫:slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>
注意:這種方法在Activity和Fragment中均使用正常,可以解決Android文字垂直滾動(dòng)、縱向走馬燈在Fragment中重疊的現(xiàn)象。
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式匯總,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
這篇文章主要介紹了Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android中協(xié)調(diào)滾動(dòng)布局的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中協(xié)調(diào)滾動(dòng)常用的布局實(shí)現(xiàn),類似這樣的協(xié)調(diào)滾動(dòng)布局,當(dāng)?shù)撞苛斜砘瑒?dòng)的時(shí)候,頂部的布局做響應(yīng)的動(dòng)作,我們都可以通過(guò)?AppBarLayout?和?MotionLayout?來(lái)實(shí)現(xiàn),本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
Android實(shí)現(xiàn)音量調(diào)節(jié)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁(yè)面布局及多媒體播放的設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
關(guān)于android studio升級(jí)4.1 某些插件使用不了的問(wèn)題(Mac)
這篇文章主要介紹了關(guān)于android studio升級(jí)4.1 某些插件使用不了的問(wèn)題(Mac),本文給大家分享解決方法供大家參考,感興趣的朋友跟隨小編一起看看吧2020-10-10
Android之仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫
本文主要介紹了Android仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫的實(shí)例方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
Android ViewPager撤消左右滑動(dòng)切換功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Android ViewPager撤消左右滑動(dòng)切換功能實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-04-04

