怎么在Android中实现一个滚动条广告
這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中實(shí)現(xiàn)一個(gè)滾動(dòng)條廣告,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
ViewSwitcher的介紹
ViewSwitcher 設(shè)置動(dòng)畫
ViewSwitcher 代表了視圖切換組件, 本身繼承了FrameLayout ,可以將多個(gè)View疊在一起 ,每次只顯示一個(gè)組件,ViewSwitcher 支持指定動(dòng)畫效果.我們自定義ViewSwitcher的時(shí)候,當(dāng)程序控制從一個(gè)View切換到另個(gè)View時(shí),我們可以可以重寫下面這兩個(gè)方法來設(shè)置組件切換動(dòng)畫效果
setInAnimation(AnimationinAnimation) setOutAnimation(AnimationoutAnimation)
ViewSwitcher 設(shè)置view
給ViewSwitcher設(shè)置view的方法時(shí)是調(diào)用下面這個(gè)方法
setFactory(ViewFactoryfactory)
這個(gè)ViewFactory是一個(gè)接口,里面有一個(gè)makeview方法,正是通過這個(gè)方法我們構(gòu)造并顯示在ViewSwitcher,當(dāng)然我們自定義ViewSwitcher時(shí)候,這里是傳入一個(gè)布局id,這樣我們就可以自由的設(shè)置顯示布局啦~
/*給viewSwitch添加顯示的view,可以自由設(shè)置外部調(diào)用
*@paramlayoutId
*/
publicvoidaddView(finalintlayoutId){
setFactory(newViewFactory(){
@Override
publicViewmakeView(){
returnLayoutInflater.from(getContext()).inflate(layoutId,null);
}
});
}
實(shí)例介紹
實(shí)現(xiàn)原理還是比較簡(jiǎn)單,我們可以直接看代碼,下面我們直接通過代碼來介紹這個(gè)控件的使用吧
里面都有詳細(xì)的注釋,相信都可以看得懂。
/**
*自由設(shè)置view的viewSwitcher
*CreatedbyAdministratoron2017/5/13.
*/
publicclassCarouselViewextendsViewSwitcher{
privateintmCutItem;
privateintloopTime;//循環(huán)時(shí)間
privateMyHandlermyHandler;
privateArrayList<String>listString;
publicCarouselView(Contextcontext){
this(context,null);
}
publicCarouselView(Contextcontext,AttributeSetattrs){
super(context,attrs);
initData();
initAnimation();
}
/**
*初始化一些變量
*/
privatevoidinitData(){
listString=newArrayList<>();
myHandler=newMyHandler(this);
}
/**
*給viewSwitch添加顯示的view,可以自由設(shè)置,外部調(diào)用
*@paramlayoutId自定義view的布局id
*/
publicvoidaddView(finalintlayoutId){
setFactory(newViewFactory(){
@Override
publicViewmakeView(){
returnLayoutInflater.from(getContext()).inflate(layoutId,null);
}
});
}
/**
*初始化進(jìn)入和出去動(dòng)畫
*/
privatevoidinitAnimation(){
setInAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.translate_in));
setOutAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.translate_out));
}
/**
*設(shè)置數(shù)據(jù)源并展示view,外部調(diào)用
*@parammList
*@paramtime
*/
publicvoidupDataListAndView(ArrayList<String>mList,inttime){
mCutItem=0;
loopTime=time;
if(null==mList){
return;
}
listString.clear();
listString.addAll(mList);
updataView(mList.get(0),getCurrentView(),mCutItem);
}
/**
*展示下一條廣告
*/
publicvoidshowNextView(){
if(null==listString||listString.size()<2){
return;
}
mCutItem=mCutItem==listString.size()-1?0:mCutItem+1;
updataView(listString.get(mCutItem),getNextView(),mCutItem);
showNext();
}
/**
*啟動(dòng)輪播
*/
publicvoidstartLooping(){
if(null==listString||listString.size()<2){
return;
}
myHandler.removeMessages(0);
myHandler.sendEmptyMessageDelayed(0,loopTime);
}
/**
*停止輪播
*/
publicvoidstopLooping(){
myHandler.removeMessages(0);
}
/**
*在當(dāng)前view上設(shè)置數(shù)據(jù)
*@paramtext
*@paramview
*/
privatevoidupdataView(Stringtext,Viewview,finalintmCutItem){
TextViewtextView=(TextView)view.findViewById(R.id.tv_carouse_text);
textView.setText(text);
textView.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
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泄露
**/
privatestaticclassMyHandlerextendsHandler{
privateWeakReference<CarouselView>mRef;
publicMyHandler(CarouselViewview){
mRef=newWeakReference<CarouselView>(view);
}
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
CarouselViewmView=this.mRef.get();
mView.showNextView();//展示下一條廣告,會(huì)調(diào)用shownext方法展示下一條廣告
mView.startLooping();//啟動(dòng)輪播,間隔后展示下一條
}
}
OnClickItemListeneronClickItemListener;
/**
*定義一個(gè)接口回調(diào),響應(yīng)廣告點(diǎn)擊
*/
interfaceOnClickItemListener{
voidonClick(intposition);
}
publicvoidsetOnClickListener(OnClickItemListeneronClickListener){
this.onClickItemListener=onClickListener;
}
}
看完了代碼之后,接著我們來看一下外部的使用方法
外部使用方法
外部調(diào)用
carouselView.addView(R.layout.itemview);
carouselView.upDataListAndView(mList,3000);
carouselView.setOnClickListener(newCarouselView.OnClickItemListener(){
@Override
publicvoidonClick(intposition){
Toast.makeText(mContext,"你點(diǎn)擊了第"+position+"條廣告",Toast.LENGTH_SHORT).show();
}
});
itemview的布局
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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)畫
translate_in.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns: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)畫
translate_out.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns: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é)
以上是生活随笔為你收集整理的怎么在Android中实现一个滚动条广告的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVC中业务层是否应该有个基类?它有什么
- 下一篇: win10如何关闭defender sm