Adroid之自定义跑马灯(超简单)

一、XML 

1.一个图片,一个文字描述(需要什么控件自己选择)

 <!--跑马灯-->
    <RelativeLayout
        android:id="@+id/homepager_RelativeLayout_pao"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_50"
        android:layout_below="@+id/homepager_ViewPager_jiu_layout"
        android:orientation="horizontal">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/homepager_pao_SimpleDraweeView"
            android:layout_width="@dimen/dp_50"
            android:layout_height="@dimen/dp_50" />

        <TextView
            android:id="@+id/homepager_pao_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </RelativeLayout>

二、java代码

1.创建三个动画

第一个动画:y轴平移 坐标 :0,0;(中间展示区域)

第二个动画:y轴平移 坐标 :0,-50;(从中间到上方移动)

第三个动画:y轴平移 坐标 :50,0;(从下方到中间移动)

2.将动画添加到集合中(必须按照上面的顺序添加)

3.集合中的每个动画都执行两秒

4.开始执行

5.设置无限次播放动画(因为动画集合并不存在setRepeatCount()方法所以必须在

animatorSet.addListener(new Animator.AnimatorListener(){}监听中的结束方法中再次执行动画,这样每次结束后,就会再次执行,实现无限轮播)

6.设置内容(要在animatorTop的监听中去设置内容,当animatorTop执行结束后,重新给内容赋值,下面就会执行animatorBottom (从下方到中间)的动画,这样用户就不会看见内容改变的过程。只能看到一条新的信息从下方升上来。)

//跑马灯
    private void annimotion() {
        ObjectAnimator animatorTop = ObjectAnimator.ofFloat(homepager_relativeLayout_pao,"translationY",0,-50);
        ObjectAnimator animatorBottom = ObjectAnimator.ofFloat(homepager_relativeLayout_pao,"translationY",50,0);
        ObjectAnimator animator = ObjectAnimator.ofFloat(homepager_relativeLayout_pao,"translationY",0,0);
        //创建动画集合
        animatorSet = new AnimatorSet();
        animatorSet.playSequentially(animator,animatorTop,animatorBottom);
        //集合中的每个动画都执行两秒
        animatorSet.setDuration(2000);
        //开始执行动画
        animatorSet.start();

        //设置跑马灯内容
        animatorTop.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                position++;
                homepager_pao_textView.setText(jiuBeanList.get(position%jiuBeanList.size()).getName());
                homepager_pao_simpleDraweeView.setImageURI(jiuBeanList.get(position%jiuBeanList.size()).getIcon());
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        //设置无限播放动画
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //每次动画结束后在次执行
                animatorSet.setDuration(2000);
                animatorSet.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

猜你喜欢

转载自blog.csdn.net/qq_42234894/article/details/83831408