那年我没做的滚动banner条

又两个星期没写文了,感觉自己像条咸鱼。坚持果然不是件容易的事,特别是我这种小菜鸟

前些天在地铁翻apiDemo的时候,看到

《那年我没做的滚动banner条》 push.gif

看起来很眼熟,想起实习的时候看facebook的banner广告,好像就是这么个效果,类似向上翻页。嘛,那时候不会做,所以只画了个静态页,广告内容挤在一个banner里,内容过多就用省略号。
实际上,我需要的是这样的效果

《那年我没做的滚动banner条》 banner.gif

因为我是模拟器录的gif,而模拟器是横屏的,所以整个条比较长。

So,来翻翻apidemo怎么实现的
实际上就是一个控件ViewFlipper。

《那年我没做的滚动banner条》 Paste_Image.png

用于切换多个view,并且支持动画,在xml里用的时候,有两个特别的属性可以配置
flipInterval 循环区间,单位是ms 。autoStart是否自动开启,Boolean值

上面那个滚动的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设置了autostart = true的话,就不需要调用startflipping方法了,当然你也可能是想要控制他什么时候开始切换view,那么就autostart置为false,然后在需要的地方调用startflipping方法

当然,这个动画效果是很多种的,偏移量,透明度,放大缩小,旋转,自己设置,这里说一下持续时间

整个viewflipper的动画间隔,受flipInterval 的控制,就是说如果flipinterval设置为2s,那么2s切换一次view。和咱动画里的duration没啥关系,意思是,就算duration是10s,2s后动画还没跑完,view也会切换。

还记得那时候用了悬浮窗,在各种界面弹弹弹广告,想想觉得自己好无耻,哈哈哈

第一次用markdow,用来贴代码还不错,看起来比富文本好多了。

    原文作者:感冒没吃药
    原文地址: https://www.jianshu.com/p/9d8a2715fa8d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞