Android滾動(dòng)條廣告實(shí)現(xiàn)代碼示例
前言
幾乎每個(gè)上線的App上面都會(huì)有個(gè)滾動(dòng)條廣告,滾動(dòng)條廣告主要以文字標(biāo)題的形式存在,什么點(diǎn)開(kāi)文章你就能賺一百萬(wàn)啊、看完轉(zhuǎn)走這個(gè)你就能平安一生啊這樣的標(biāo)題,都是以標(biāo)題廣告的形式吸引人的,當(dāng)然開(kāi)個(gè)小玩笑啦,哈哈,畢竟是要賺錢(qián)的嘛,接上幾個(gè)廣告是正常滴~~
之前在項(xiàng)目中要求要做一個(gè)滾動(dòng)條輪播的展示,就是在滾動(dòng)條上放幾條廣告進(jìn)行輪播。一開(kāi)始知識(shí)覺(jué)得直接用TextSwitcher或者ViewSwitcher就可以了。雖然這樣也能滿(mǎn)足需求,但是項(xiàng)目里有好幾個(gè)地方都用到了滾動(dòng)條廣告。如果每個(gè)地方都寫(xiě)一套同樣的代碼的話,就有點(diǎn)浪費(fèi)了。況且代碼的設(shè)計(jì)原則其中之一就是復(fù)用,所以就寫(xiě)了一個(gè)自定義的viewSwitcher來(lái)直接用了,當(dāng)然寫(xiě)了自定義的,功能要求當(dāng)然要完善一點(diǎn)。這個(gè)viewSwitcher支持我們?cè)跐L動(dòng)條上自定義view。外部需要設(shè)置滾動(dòng)條上自定義的布局和設(shè)置數(shù)據(jù)源。我們先看下效果圖吧。
ViewSwitcher的介紹
ViewSwitcher 設(shè)置動(dòng)畫(huà)
ViewSwitcher 代表了視圖切換組件, 本身繼承了FrameLayout ,可以將多個(gè)View疊在一起 ,每次只顯示一個(gè)組件,ViewSwitcher 支持指定動(dòng)畫(huà)效果.我們自定義ViewSwitcher的時(shí)候,當(dāng)程序控制從一個(gè)View切換到另個(gè)View時(shí),我們可以可以重寫(xiě)下面這兩個(gè)方法來(lái)設(shè)置組件切換動(dòng)畫(huà)效果
setInAnimation(Animation inAnimation) setOutAnimation(Animation outAnimation)
ViewSwitcher 設(shè)置view
給ViewSwitcher設(shè)置view的方法時(shí)是調(diào)用下面這個(gè)方法
setFactory(ViewFactory factory)
這個(gè)ViewFactory是一個(gè)接口,里面有一個(gè)makeview方法,正是通過(guò)這個(gè)方法我們構(gòu)造并顯示在ViewSwitcher,當(dāng)然我們自定義ViewSwitcher時(shí)候,這里是傳入一個(gè)布局id,這樣我們就可以自由的設(shè)置顯示布局啦~
/* 給viewSwitch添加顯示的view,可以自由設(shè)置 外部調(diào)用 * @param layoutId */ public void addView(final int layoutId){ setFactory(new ViewFactory(){ @Override public View makeView() { return LayoutInflater.from(getContext()).inflate(layoutId,null); } }); }
實(shí)例介紹
實(shí)現(xiàn)原理還是比較簡(jiǎn)單,我們可以直接看代碼,下面我們直接通過(guò)代碼來(lái)介紹這個(gè)控件的使用吧
里面都有詳細(xì)的注釋?zhuān)嘈哦伎梢钥吹枚?/p>
/** * 自由設(shè)置view的viewSwitcher * Created by Administrator on 2017/5/13. */ public class CarouselView extends ViewSwitcher { private int mCutItem; private int loopTime;//循環(huán)時(shí)間 private MyHandler myHandler; private ArrayList<String> listString; public CarouselView(Context context) { this(context, null); } public CarouselView(Context context, AttributeSet attrs) { super(context, attrs); initData(); initAnimation(); } /** * 初始化一些變量 */ private void initData(){ listString = new ArrayList<>(); myHandler = new MyHandler(this); } /** * 給viewSwitch添加顯示的view,可以自由設(shè)置,外部調(diào)用 * @param layoutId 自定義view的布局id */ public void addView(final int layoutId){ setFactory(new ViewFactory(){ @Override public View makeView() { return LayoutInflater.from(getContext()).inflate(layoutId,null); } }); } /** * 初始化進(jìn)入和出去動(dòng)畫(huà) */ private void initAnimation(){ setInAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.translate_in)); setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.translate_out)); } /** * 設(shè)置數(shù)據(jù)源并展示view,外部調(diào)用 * @param mList * @param time */ public void upDataListAndView(ArrayList<String> mList,int time){ mCutItem = 0; loopTime = time; if (null == mList) { return; } listString.clear(); listString.addAll(mList); updataView(mList.get(0),getCurrentView(),mCutItem); } /** *展示下一條廣告 */ public void showNextView() { if (null == listString || listString.size() < 2) { return; } mCutItem = mCutItem == listString.size() - 1 ? 0 : mCutItem + 1; updataView(listString.get(mCutItem), getNextView(),mCutItem); showNext(); } /** * 啟動(dòng)輪播 */ public void startLooping() { if (null == listString || listString.size() < 2 ) { return; } myHandler.removeMessages(0); myHandler.sendEmptyMessageDelayed(0, loopTime); } /** * 停止輪播 */ public void stopLooping(){ myHandler.removeMessages(0); } /** * 在當(dāng)前view上設(shè)置數(shù)據(jù) * @param text * @param view */ private void updataView(String text,View view, final int mCutItem){ TextView textView = (TextView) view.findViewById(R.id.tv_carouse_text); textView.setText(text); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (null != onClickItemListener) { onClickItemListener.onClick(mCutItem); } //Toast.makeText(getContext(), "你點(diǎn)擊了第" + position + "條廣告", Toast.LENGTH_SHORT).show(); } }); } /** * @description 主線程Handler * @note 因?yàn)榇嬖诙〞r(shí)任務(wù),并且TextSwitcherView持有Activity的引用 * 所以這里采用弱引用,主要針對(duì)內(nèi)存回收的時(shí)候Activity泄露 **/ private static class MyHandler extends Handler { private WeakReference<CarouselView> mRef; public MyHandler(CarouselView view){ mRef = new WeakReference<CarouselView>(view); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); CarouselView mView = this.mRef.get(); mView.showNextView();//展示下一條廣告,會(huì)調(diào)用shownext方法展示下一條廣告 mView.startLooping();//啟動(dòng)輪播,間隔后展示下一條 } } OnClickItemListener onClickItemListener; /** * 定義一個(gè)接口回調(diào),響應(yīng)廣告點(diǎn)擊 */ interface OnClickItemListener{ void onClick(int position); } public void setOnClickListener(OnClickItemListener onClickListener){ this.onClickItemListener = onClickListener; } }
看完了代碼之后,接著我們來(lái)看一下外部的使用方法
外部使用方法
外部調(diào)用
carouselView.addView(R.layout.itemview); carouselView.upDataListAndView(mList, 3000); carouselView.setOnClickListener(new CarouselView.OnClickItemListener() { @Override public void onClick(int position) { Toast.makeText(mContext, "你點(diǎn)擊了第" + position + "條廣告",Toast.LENGTH_SHORT).show(); } });
itemview的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="25dp" android:layout_height="25dp" android:layout_marginLeft="10dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/tv_carouse_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="1" android:gravity="center" android:text="111"/> </LinearLayout>
設(shè)置進(jìn)入動(dòng)畫(huà)
translate_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:duration="2000" > <translate android:fromXDelta="0%" android:fromYDelta="100%" android:toXDelta="0%" android:toYDelta="0%"/> </set>
設(shè)置出去動(dòng)畫(huà)
translate_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:interpolator/linear" android:duration="1000"> <translate android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="0%" android:toYDelta="-100%"/> </set>
結(jié)語(yǔ)
實(shí)現(xiàn)就是這樣子的,外部設(shè)置view布局,不過(guò)給view布局里面的控件設(shè)置數(shù)據(jù)需要在里面調(diào)用看,具體看你設(shè)置怎么樣的布局,這種可以根據(jù)布局在里面靈活控制一下。動(dòng)畫(huà)也是在外邊自由設(shè)置。就寫(xiě)到這里啦
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)ListView分頁(yè)自動(dòng)加載數(shù)據(jù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView分頁(yè)自動(dòng)加載數(shù)據(jù)的方法,涉及Android生成listview列表的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android中View.post和Handler.post的關(guān)系
這篇文章主要介紹了Android中View.post和Handler.post的關(guān)系,View.post和Handler.post是Android開(kāi)發(fā)中經(jīng)常使用到的兩個(gè)”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細(xì)分析需要的小伙伴可以參考一下2022-06-06Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android ListView與RecycleView的對(duì)比使用解析
這篇文章主要介紹了Android ListView與RecycleView的對(duì)比使用解析,需要的朋友可以參考下2017-12-12Android實(shí)現(xiàn)圖片浮動(dòng)隨意拖拽效果
這篇文章主要介紹了Android的圖片在界面隨意拖動(dòng)的功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2018-04-04Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
本篇文章主要介紹了Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04Android中asset文件夾與raw文件夾的區(qū)別深入解析
本篇文章是對(duì)Android中的asset文件夾與raw文件夾區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn)
這篇文章主要介紹了Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11