怎么在Android中实现一个滚动条广告
這篇文章將為大家詳細講解有關怎么在Android中實現一個滾動條廣告,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
ViewSwitcher的介紹
ViewSwitcher 設置動畫
ViewSwitcher 代表了視圖切換組件, 本身繼承了FrameLayout ,可以將多個View疊在一起 ,每次只顯示一個組件,ViewSwitcher 支持指定動畫效果.我們自定義ViewSwitcher的時候,當程序控制從一個View切換到另個View時,我們可以可以重寫下面這兩個方法來設置組件切換動畫效果
setInAnimation(AnimationinAnimation) setOutAnimation(AnimationoutAnimation)
ViewSwitcher 設置view
給ViewSwitcher設置view的方法時是調用下面這個方法
setFactory(ViewFactoryfactory)
這個ViewFactory是一個接口,里面有一個makeview方法,正是通過這個方法我們構造并顯示在ViewSwitcher,當然我們自定義ViewSwitcher時候,這里是傳入一個布局id,這樣我們就可以自由的設置顯示布局啦~
/*給viewSwitch添加顯示的view,可以自由設置外部調用
*@paramlayoutId
*/
publicvoidaddView(finalintlayoutId){
setFactory(newViewFactory(){
@Override
publicViewmakeView(){
returnLayoutInflater.from(getContext()).inflate(layoutId,null);
}
});
}
實例介紹
實現原理還是比較簡單,我們可以直接看代碼,下面我們直接通過代碼來介紹這個控件的使用吧
里面都有詳細的注釋,相信都可以看得懂。
/**
*自由設置view的viewSwitcher
*CreatedbyAdministratoron2017/5/13.
*/
publicclassCarouselViewextendsViewSwitcher{
privateintmCutItem;
privateintloopTime;//循環時間
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,可以自由設置,外部調用
*@paramlayoutId自定義view的布局id
*/
publicvoidaddView(finalintlayoutId){
setFactory(newViewFactory(){
@Override
publicViewmakeView(){
returnLayoutInflater.from(getContext()).inflate(layoutId,null);
}
});
}
/**
*初始化進入和出去動畫
*/
privatevoidinitAnimation(){
setInAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.translate_in));
setOutAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.translate_out));
}
/**
*設置數據源并展示view,外部調用
*@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();
}
/**
*啟動輪播
*/
publicvoidstartLooping(){
if(null==listString||listString.size()<2){
return;
}
myHandler.removeMessages(0);
myHandler.sendEmptyMessageDelayed(0,loopTime);
}
/**
*停止輪播
*/
publicvoidstopLooping(){
myHandler.removeMessages(0);
}
/**
*在當前view上設置數據
*@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(),"你點擊了第"+position+"條廣告",Toast.LENGTH_SHORT).show();
}
});
}
/**
*@description主線程Handler
*@note因為存在定時任務,并且TextSwitcherView持有Activity的引用
*所以這里采用弱引用,主要針對內存回收的時候Activity泄露
**/
privatestaticclassMyHandlerextendsHandler{
privateWeakReference<CarouselView>mRef;
publicMyHandler(CarouselViewview){
mRef=newWeakReference<CarouselView>(view);
}
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
CarouselViewmView=this.mRef.get();
mView.showNextView();//展示下一條廣告,會調用shownext方法展示下一條廣告
mView.startLooping();//啟動輪播,間隔后展示下一條
}
}
OnClickItemListeneronClickItemListener;
/**
*定義一個接口回調,響應廣告點擊
*/
interfaceOnClickItemListener{
voidonClick(intposition);
}
publicvoidsetOnClickListener(OnClickItemListeneronClickListener){
this.onClickItemListener=onClickListener;
}
}
看完了代碼之后,接著我們來看一下外部的使用方法
外部使用方法
外部調用
carouselView.addView(R.layout.itemview);
carouselView.upDataListAndView(mList,3000);
carouselView.setOnClickListener(newCarouselView.OnClickItemListener(){
@Override
publicvoidonClick(intposition){
Toast.makeText(mContext,"你點擊了第"+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>
設置進入動畫
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>
設置出去動畫
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>
總結
以上是生活随笔為你收集整理的怎么在Android中实现一个滚动条广告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVC中业务层是否应该有个基类?它有什么
- 下一篇: Mysql 中的事件//定时任务