那年我没做的滚动banner条

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

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


push.gif

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

banner.gif

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

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

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,用来贴代码还不错,看起来比富文本好多了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,288评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 我叫醒了黎明 于是我忙碌了整个白天 我不愿醒来 可是我想听喜欢的歌曲 我还想用文字写心情 于是我爬在床上等天明 我...
    田萍阅读 416评论 0 5
  • 喜欢一个人应该最先从喜欢他周围的事物开始。然后爱屋及乌,用你的心意,给万物符上魂魄,让它们变的活波而灵动,飞舞在他...
    半岛狸猫阅读 245评论 0 0
  • 第七届职通车High计划—读书组分享 —《人类简史》 作者尤瓦尔·赫拉利(Yuval Noah Harari),1...
    第七届职通车读书会阅读 579评论 0 2