簡(jiǎn)單實(shí)現(xiàn)Android滾動(dòng)公告欄
本文實(shí)現(xiàn)的效果,是一個(gè)滾動(dòng)的公告欄,是這樣的:
可以看到這個(gè)公告欄一方面是滾動(dòng),另外一方面是可點(diǎn)擊。
實(shí)現(xiàn)的思路:
1.textView放在ViewFlipper中實(shí)現(xiàn)滑動(dòng)效果(可設(shè)置左右、或者上下滾動(dòng)),很明顯這應(yīng)該是自定義view;
2.利用textView的點(diǎn)擊事件即可實(shí)現(xiàn)點(diǎn)擊;
OK,先看看自定義view的代碼:
public class MarqueeTextView extends LinearLayout { private Context mContext; private ViewFlipper viewFlipper; private View marqueeTextView; private String[] textArrays; private MarqueeTextViewClickListener marqueeTextViewClickListener; public MarqueeTextView(Context context) { super(context); mContext = context; initBasicView(); } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initBasicView(); } public void setTextArraysAndClickListener(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) {//1.設(shè)置數(shù)據(jù)源;2.設(shè)置監(jiān)聽(tīng)回調(diào)(將textView點(diǎn)擊事件傳遞到目標(biāo)界面進(jìn)行操作) this.textArrays = textArrays; this.marqueeTextViewClickListener = marqueeTextViewClickListener; initMarqueeTextView(textArrays, marqueeTextViewClickListener); } public void initBasicView() {//加載布局,初始化ViewFlipper組件及效果 marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.marquee_textview_layout, null); LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); addView(marqueeTextView, layoutParams); viewFlipper = (ViewFlipper) marqueeTextView.findViewById(R.id.viewFlipper); viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));//設(shè)置上下的動(dòng)畫(huà)效果(自定義動(dòng)畫(huà),所以改左右也很簡(jiǎn)單) viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top)); viewFlipper.startFlipping(); } public void initMarqueeTextView(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) { if (textArrays.length == 0) { return; } int i = 0; viewFlipper.removeAllViews(); while (i < textArrays.length) { TextView textView = new TextView(mContext); textView.setText(textArrays[i]); textView.setOnClickListener(marqueeTextViewClickListener); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); viewFlipper.addView(textView, lp); i++; } } public void releaseResources() { if (marqueeTextView != null) { if (viewFlipper != null) { viewFlipper.stopFlipping(); viewFlipper.removeAllViews(); viewFlipper = null; } marqueeTextView = null; } } }
然后,主Activity異常簡(jiǎn)單(還是封裝得好):
public class MainActivity extends AppCompatActivity { private MarqueeTextView marqueeTv; private String [] textArrays = new String[]{"this is content No.1","this is content No.2","this is content No.3"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); marqueeTv = (MarqueeTextView) findViewById(R.id.marqueeTv); marqueeTv.setTextArraysAndClickListener(textArrays, new MarqueeTextViewClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this,AnotherActivity.class)); } }); } @Override protected void onDestroy() { marqueeTv.releaseResources(); super.onDestroy(); } }
Git地址>>https://github.com/ganshenml/MarqueeTextViewApp
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android開(kāi)發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- android實(shí)現(xiàn)上下滾動(dòng)的TextView
- android TextView不用ScrollViewe也可以滾動(dòng)的方法
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁(yè)的Gridview實(shí)例源碼
- android 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼
- android開(kāi)發(fā)教程之文本框加滾動(dòng)條scrollview
- android ListView自動(dòng)滾動(dòng)方法
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- Android實(shí)現(xiàn)字幕滾動(dòng)的方法
- android listview 水平滾動(dòng)和垂直滾動(dòng)的小例子
相關(guān)文章
android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
這篇文章主要介紹了android MediaRecorder錄屏?xí)r帶錄音功能實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04android實(shí)現(xiàn)計(jì)步功能初探
這篇文章主要介紹了android實(shí)現(xiàn)計(jì)步功能初探,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10為Android系統(tǒng)添加config.xml 新配置的設(shè)置
這篇文章主要介紹了為Android系統(tǒng)添加config.xml 新配置的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03輕松實(shí)現(xiàn)Android語(yǔ)音識(shí)別功能
這篇文章主要為初學(xué)者介紹了輕松實(shí)現(xiàn)Android語(yǔ)音識(shí)別功能的代碼,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法,詳細(xì)分析了Android針對(duì)圖片的讀取、縮放及保存等操作技巧,需要的朋友可以參考下2016-06-06Android開(kāi)發(fā)中的MVC設(shè)計(jì)模式淺析
這篇文章主要介紹了Android開(kāi)發(fā)中的MVC設(shè)計(jì)模式淺析,本文講解了對(duì)Android開(kāi)發(fā)中的MVC設(shè)計(jì)模式的理解,需要的朋友可以參考下2015-06-06導(dǎo)入takephoto庫(kù)編譯失敗與glide庫(kù)沖突應(yīng)排除依賴(lài)
今天小編就為大家分享一篇關(guān)于導(dǎo)入takephoto庫(kù)編譯失敗與glide庫(kù)沖突應(yīng)排除依賴(lài)的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果
這篇文章主要介紹了Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果,本文給大家介紹非常詳細(xì)包括實(shí)現(xiàn)原理和實(shí)例代碼,需要的朋友參考下吧2017-08-08