那年我沒做的滾動(dòng)banner條

又兩個(gè)星期沒寫文了,感覺自己像條咸魚。堅(jiān)持果然不是件容易的事,特別是我這種小菜鳥

前些天在地鐵翻apiDemo的時(shí)候,看到


push.gif

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

banner.gif

因?yàn)槲沂悄M器錄的gif,而模擬器是橫屏的,所以整個(gè)條比較長。

So,來翻翻apidemo怎么實(shí)現(xiàn)的
實(shí)際上就是一個(gè)控件ViewFlipper。

Paste_Image.png

用于切換多個(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ò),看起來比富文本好多了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,667評論 4 61
  • 我叫醒了黎明 于是我忙碌了整個(gè)白天 我不愿醒來 可是我想聽喜歡的歌曲 我還想用文字寫心情 于是我爬在床上等天明 我...
    田萍閱讀 445評論 0 5
  • 喜歡一個(gè)人應(yīng)該最先從喜歡他周圍的事物開始。然后愛屋及烏,用你的心意,給萬物符上魂魄,讓它們變的活波而靈動(dòng),飛舞在他...
    半島貍貓閱讀 316評論 0 0
  • 第七屆職通車High計(jì)劃—讀書組分享 —《人類簡史》 作者尤瓦爾·赫拉利(Yuval Noah Harari),1...
    第七屆職通車讀書會(huì)閱讀 638評論 0 2

友情鏈接更多精彩內(nèi)容