Android 竖直滚动广告条、上下滚动广告条,View滚动广告条;

四种方式实现仿淘宝滚动广告条:

  • TextView+Handler延时动画 实现滚动效果;
  • 自定义ViewFlipper 实现滚动效果;(用法简单,推荐使用)
  • RecyclerView+子线程延时 实现滚动效果;
  • 竖直ViewPager+Handler延时 实现滚动效果;

Android 竖直滚动广告条、上下滚动广告条,View滚动广告条;_第1张图片

项目已上传Github:https://github.com/CuiChenbo/UpRollView;

其实最早写这个效果是在16年的时候,现在拿出来整理一下,同时也看一下自己之前的代码;

Android仿淘宝头条滚动广告条 , 之前写的没有传源码,也比较乱;

下面是一些实现功能的主要代码,看全部代码还是移步到UpRollView;

一、TextView+Handler延时动画

private void TextViewRollAd() {
        TVADPOSITION++;

        handler.removeMessages(TVADWITH);
        handler.sendEmptyMessageDelayed(TVADWITH,2500);
    }


    private void TvAnimation() {

        TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0,-tv.getHeight());
        downTranslateAnimation.setDuration(500);
        downTranslateAnimation.setFillAfter(true);
        AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
        alphaAnimation.setDuration(500);

        AnimationSet animationSetOut=new AnimationSet(true);
        animationSetOut.addAnimation(downTranslateAnimation);
        animationSetOut.addAnimation(alphaAnimation);
        tv.startAnimation(animationSetOut);

        animationSetOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation){
                int iii = TVADPOSITION% mList_Tv_Ad.size();
                tv.setText(mList_Tv_Ad.get(iii));
                topTranslateAnimation();
            }
            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
    }
    private void topTranslateAnimation(){

        TranslateAnimation topTranslateAnimation=new TranslateAnimation(0,0,tv.getHeight(),0);
        topTranslateAnimation.setDuration(500);
        topTranslateAnimation.setFillAfter(true);

        AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
        alphaAnimation.setDuration(500);


        AnimationSet animationSetIn=new AnimationSet(true);
        animationSetIn.addAnimation(topTranslateAnimation);
        animationSetIn.addAnimation(alphaAnimation);

        tv.startAnimation(animationSetIn);

        animationSetIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
            @Override
            public void onAnimationRepeat(Animation animation) {}
            @Override
            public void onAnimationEnd(Animation animation){
            }
        });
    }

二、自定义ViewFlipper(推荐使用,用法最简单)

public class UpRollView extends ViewFlipper {
 private Context content;
    public UpRollView(Context context) {
        super(context);
        init(context);
    }
    private int Interval = 3000;

    public UpRollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.content = context;
        setFlipInterval(Interval);//设置时间间隔
        setInAnimation(context, R.anim.in);
        setOutAnimation(context, R.anim.out);
    }
    public void setInterval(int i){
        Interval = i;
        setFlipInterval(Interval);//设置时间间隔
    }

    /**
     * 设置循环滚动的View数组
     */
    public void setViews(final List views) {
        if (views == null || views.size() == 0) return;
        removeAllViews();
        for ( int i = 0; i < views.size(); i++) {
            final int finalposition=i;
            //设置监听回调
            views.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(finalposition, views.get(finalposition));
                    }
                }
            });
            addView(views.get(i));
        }
        startFlipping();
    }
    
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }
}

三、RecyclerView+子线程延时

   new PagerSnapHelper().attachToRecyclerView(rv); //一次滑动一页

    private int cp;
    DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
    private void startRoll(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(3000);
                if (!thread.isInterrupted()) {
                    rv.smoothScrollBy(0, dp2px(60), decelerateInterpolator);
                    //                rv.smoothScrollToPosition(++cp);
                    this.run();
                }
            }
        });
        thread.start();
    }

四、竖直ViewPager+Handler延时

垂直viewpager:https://blog.csdn.net/qq_35605213/article/details/86467859

   private void init() {
        VAdapter vAdapter = new VAdapter();
        vvp.setAdapter(vAdapter);
        handler.postDelayed(runnable, 3000);
    }

    private int currentPosition;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            vvp.setCurrentItem(++currentPosition , true);
            handler.postDelayed(runnable, 3000);
        }
    };

Android 竖直滚动广告条、上下滚动广告条,View滚动广告条;_第2张图片

代码已上传,欢迎star:https://github.com/CuiChenbo/UpRollView

你可能感兴趣的:(Android)