又兩個(gè)星期沒寫文了,感覺自己像條咸魚。堅(jiān)持果然不是件容易的事,特別是我這種小菜鳥
前些天在地鐵翻apiDemo的時(shí)候,看到

看起來很眼熟,想起實(shí)習(xí)的時(shí)候看facebook的banner廣告,好像就是這么個(gè)效果,類似向上翻頁。嘛,那時(shí)候不會(huì)做,所以只畫了個(gè)靜態(tài)頁,廣告內(nèi)容擠在一個(gè)banner里,內(nèi)容過多就用省略號。
實(shí)際上,我需要的是這樣的效果

因?yàn)槲沂悄M器錄的gif,而模擬器是橫屏的,所以整個(gè)條比較長。
So,來翻翻apidemo怎么實(shí)現(xiàn)的
實(shí)際上就是一個(gè)控件ViewFlipper。

用于切換多個(gè)view,并且支持動(dòng)畫,在xml里用的時(shí)候,有兩個(gè)特別的屬性可以配置
flipInterval 循環(huán)區(qū)間,單位是ms 。autoStart是否自動(dòng)開啟,Boolean值
上面那個(gè)滾動(dòng)的banner,布局如下
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:autoStart="true"
android:background="#ffffff"
android:flipInterval="3000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:src="@drawable/logo" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="5dp"
android:text="This is the test Ad"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingLeft="5dp"
android:text="The ad content you want to explain,you also can set another tv below this one"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:src="@drawable/logo" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="5dp"
android:text="The ad content in the second view, maybe your content is so much"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</ViewFlipper>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#60B515"
android:text="install now"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="visible" />
</LinearLayout>
然后在代碼里拿到viewflipper
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper);
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_out));
// flipper.startFlipping();
如果我們給viewflipper設(shè)置了autostart = true的話,就不需要調(diào)用startflipping方法了,當(dāng)然你也可能是想要控制他什么時(shí)候開始切換view,那么就autostart置為false,然后在需要的地方調(diào)用startflipping方法
當(dāng)然,這個(gè)動(dòng)畫效果是很多種的,偏移量,透明度,放大縮小,旋轉(zhuǎn),自己設(shè)置,這里說一下持續(xù)時(shí)間
整個(gè)viewflipper的動(dòng)畫間隔,受flipInterval 的控制,就是說如果flipinterval設(shè)置為2s,那么2s切換一次view。和咱動(dòng)畫里的duration沒啥關(guān)系,意思是,就算duration是10s,2s后動(dòng)畫還沒跑完,view也會(huì)切換。
還記得那時(shí)候用了懸浮窗,在各種界面彈彈彈廣告,想想覺得自己好無恥,哈哈哈
第一次用markdow,用來貼代碼還不錯(cuò),看起來比富文本好多了。