End

ViewFlipper 淘宝头条 轮播 自动切换

ViewFlipper介绍

ViewFilpper类继承于ViewAnimator,而ViewAnimator类继承于FrameLayout。
   
ViewAnimator:
Base class for a FrameLayout container that will perform执行 animations when switching切换 between its views.

ViewFilpper:
Simple ViewAnimator that will animate between two or more views that have been added to it.  Only one child is shown at a time.  If requested, can automatically flip自动翻转 between each child at a regular interval间隔.

常用API

查看ViewAnimator类的源码可以看出此类的作用主要是为其中的View切换提供动画效果。该类有如下几个和动画相关的方法:
  • setInAnimation(Animation)与setInAnimation(Context, resourceID):设置View进入屏幕时候使用的动画
  • setOutAnimationgetInAnimation、getOutAnimation:动画
  • showNext、showPrevious:显示FrameLayout里面的下/一个View
  • getCurrentView:当前的View。Returns the View corresponding to the currently displayed child.
  • setDisplayedChild、getDisplayedChild:正在显示的View的序号。Returns the index of the currently displayed child view.
  • setAnimateFirstView(boolean animate)、getAnimateFirstViewTrue to animate the current View the first time it is displayed, false otherwise.
  • 此外还有各种removeView方法。

查看ViewFlipper的源码可以看到,ViewFlipper主要用来实现View的自动切换。该类提供了如下几个主要的方法:

  • setFilpInterval:设置View切换的时间间隔,参数为毫秒
  • startFlipping:开始进行View的切换,切换会循环进行
  • stopFlipping:停止View切换
  • isFlipping: 用来判断View切换是否正在进行 。Returns true if the child views are flipping.
  • setAutoStart:设置是否自动开始,如果设置为true,当ViewFlipper显示的时候View的切换会自动开始
  • isAutoStart:是否为自动开始,Returns true if this view automatically calls startFlipping() when it becomes attached to a window.
一般情况下,我们都会使用ViewFilpper类实现View的切换,而不使用它的父类ViewAnimator类。

案例

public class MainActivity extends Activity {
    private ViewFlipper flipper1flipper2;
    private List<TBean> mList = new ArrayList<TBean>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        flipper1 = new ViewFlipper(this);
        flipper1.setBackgroundColor(0x330000ff);
        flipper2 = new ViewFlipper(this);
        flipper2.setBackgroundColor(0x33ff0000);
        layout.addView(flipper1);
        layout.addView(flipper2);
        initFlipper(flipper1, R.anim.anim_in, R.anim.anim_out);
        initFlipper(flipper2, R.anim.anim_in2, R.anim.anim_out2);
        setContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }
    private void initFlipper(ViewFlipper flipper, int animIn, int animOut) {
        mList.add(new TBean("疯传  家人给2岁孩子喝这个,孩子智力倒退10岁""https://www.baidu.com/"));
        mList.add(new TBean("哈哈  日本玩家33万甩卖15万张游戏王卡""http://www.sina.com.cn/"));
        mList.add(new TBean("头条  内疚逃命时没带够,回废墟狂挖30小时""https://www.google.com.hk/"));
        int size = mList.size();
        for (int i = 0; i < size; i++) {
            final int num = i;
            TextView tv = new TextView(this);
            tv.setText(mList.get(i).Title);
            tv.setTextColor(0xff424954);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mList.get(num).PageUrl)));
                }
            });
            flipper.addView(tv);
        }
        flipper.setFlipInterval(3000);//间隔时间
        flipper.setInAnimation(this, animIn);
        flipper.setOutAnimation(this, animOut);
        flipper.startFlipping();
    }
    public static class TBean {
        public TBean(String title, String pageUrl) {
            Title = title;
            PageUrl = pageUrl;
        }
        public String Title;
        public String PageUrl;
    }
}

附件列表

     

    posted @ 2017-03-13 11:57  白乾涛  阅读(727)  评论(0编辑  收藏  举报