那年我没做的滚动banner条

作者: 感冒没吃药 | 来源:发表于2016-11-27 16:12 被阅读1481次
又两个星期没写文了,感觉自己像条咸鱼。坚持果然不是件容易的事,特别是我这种小菜鸟

前些天在地铁翻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,用来贴代码还不错,看起来比富文本好多了。

相关文章

网友评论

  • hauibojek:顶一个 :sunglasses: 虽然我是搞iOS的
  • MichelLei:MarkDown 你写错了
    感冒没吃药:@Sunshine已存在 嗯呐,少敲了一个
  • 挡风的纱窗:那个动画里面代码是什么啊,还有怎么设置时间啊
    挡风的纱窗:学习了 非常赞
    感冒没吃药:@挡风的纱窗 就是普通的tween Animation 补间动画,我这里是用的xml
    进入动画是
    <set xmlns:android="http://schemas.android.com/apk/res/android&quot;>
    <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="2000"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
    </set>
    消失动画是
    <set xmlns:android="http://schemas.android.com/apk/res/android&quot;>
    <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="2000"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />
    </set>
    当然你也可以用代码写Animation,有两个方法,一个接收xml,一个接收Animation对象
  • smartphp:正想做这个呢?不过是用rn
    感冒没吃药:@smartphp 嗦嘎 :+1:
    smartphp: @感冒没吃药 react-native
    感冒没吃药:@smartphp m是啥?
  • 6fe69f64cc63:c++程序🐵表示这下又有东西可以和前台程序妹妹聊点什么了。很好
  • 努力生活的西鱼:我敲一下试试

本文标题:那年我没做的滚动banner条

本文链接:https://www.haomeiwen.com/subject/ngknpttx.html