功能是上个项目实现的,今天闲来无事就觉得应该把它写成笔记记录下来,写成单独例子,搜集到我自己的功能库中方便以后使用和查阅(其实此功能也是从官方的Demo里面复制代码下来实现的)。感觉自己有点强迫症,一有时间总是想要把自己之前做过项目的一些常用的功能抽离出来保存着以后直接cp,虽然功能很菜 ~O(∩_∩)O哈哈~
使用ViewFlipper实现滚动广告,效果如下所示:
首先就是从做着玩玩开始的,实现写死的静态数据的滚动啦~
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="5dp"> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="#f5f5f5" > <ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="27dp" android:flipInterval="2000" android:inAnimation="@anim/push_up_in" android:outAnimation="@anim/push_up_out" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="70dp" android:gravity="center_vertical" android:text="写点评砸金蛋,100%中奖!" android:textColor="#ff0000" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="70dp" android:ellipsize="end" android:gravity="center_vertical" android:singleLine="true" android:text="央行:九项金融服务国标6月起实施!" android:textColor="#ff0000" android:textSize="16sp" /> </ViewFlipper> </RelativeLayout> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_centerVertical="true" android:src="@drawable/img_hot" /> </RelativeLayout> </RelativeLayout>静态的使用代码非常简单:
ViewFlipper mFlipper = ((ViewFlipper) findViewById(R.id.flipper)); mFlipper.startFlipping();动画的代码也可以在使用的代码中设置:
mFlipper.setInAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.push_up_in)); mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.push_up_out));当然实际项目中还是主要用根据后台服务器返回的数据动态显示啦~
动态加载就更加简单了,直接加载控件即可
xml:
<ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="wrap_content" android:inAnimation="@anim/push_up_in" android:outAnimation="@anim/push_up_out" android:flipInterval="2000" > </ViewFlipper>使用: ViewFlipper mFlipper = ((ViewFlipper) heard_four.findViewById(R.id.flipper));mFlipper.startFlipping(); 解析完数据再直接即可啦~ mFlipper.addView(view_adv);记录完毕~ demo下载:http://download.csdn.net/detail/lxlyhm/9682872