ViewSwitcher的源码学习

类继承关系

这里写图片描述
从如上图的继承关系上看,主要是利用了继承的策略来实现种(扩展)View切换的功能

代码分析

ViewAnimator

  • 继承了FrameLayout类再聚合了两个动画实例,具备了基本的View的切换能力
  • 提供对View的插入、删除、显示的方法
  • 提供对View的切换出场与入场动画的getter与setter
  • 提供顺序的显示View的方法showNext()
  • 对于的View的插入是没有个数限制的
  • showOnly是View切换显示的核心逻辑(子类也是使用该方法来切换View);
/**
    * Shows only the specified child. The other displays Views exit the screen,
    * optionally with the with the {@link #getOutAnimation() out animation} and
    * the specified child enters the screen, optionally with the
    * {@link #getInAnimation() in animation}.
    *
    * @param childIndex The index of the child to be shown.
    * @param animate Whether or not to use the in and out animations, defaults
    *            to true.
    */
   void showOnly(int childIndex, boolean animate) {
       final int count = getChildCount();
       for (int i = 0; i < count; i++) {
           final View child = getChildAt(i);
           if (i == childIndex) {
               if (animate && mInAnimation != null) {
                   child.startAnimation(mInAnimation);
               }
               child.setVisibility(View.VISIBLE);
               mFirstTime = false;
           } else {
               if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {
                   child.startAnimation(mOutAnimation);
               } else if (child.getAnimation() == mInAnimation)
                   child.clearAnimation();
               child.setVisibility(View.GONE);
           }
       }
   }

对应的流程如下:
这里写图片描述

ViewSwitcher

ViewSwitcher是ViewAnimator的直接子类之一,该类是思想是让两个View来回切换,实现视窗切换。
另外他提供了抽象工厂的策略来添加两个子View.
* 添加第三个子View会抛出崩溃;
* 除了设置抽象工厂的实例来添加两个了View外,还是可以使用addView的方法来添加子View;
* 显示(切换)View使用父类的方法showNextView等;

TextSwitcher

TextSwitcher是ViewSwitcher的直接子类之一,该类扩展(限定)子View只能是TextView 同时提供了TextVeiw的数据更新的方法

/**
     * Sets the text of the next view and switches to the next view. This can
     * be used to animate the old text out and animate the next text in.
     *
     * @param text the new text to display
     */
    public void setText(CharSequence text) {
        final TextView t = (TextView) getNextView();
        t.setText(text);
        showNext();
    }

ImageSwitcher

ImageSwitcher是ViewSwitcher的直接子类之一,该类扩展(限定)子View只能是ImageView同时提供了ImageView的数据更新的方法

public void setImageResource(@DrawableRes int resid)
   {
       ImageView image = (ImageView)this.getNextView();
       image.setImageResource(resid);
       showNext();
   }

   public void setImageURI(Uri uri)
   {
       ImageView image = (ImageView)this.getNextView();
       image.setImageURI(uri);
       showNext();
   }

   public void setImageDrawable(Drawable drawable)
   {
       ImageView image = (ImageView)this.getNextView();
       image.setImageDrawable(drawable);
       showNext();
   }

* 扩展ViewSwitcher时可参考setText、setImageResource等代码来写切换View时的数据更新逻辑 *

ViewFlipper

ViewFlipper是ViewAnimator直接子类之一,它具有自动轮播的功能(当我们想实现自动轮播的功能是可以参考其中的代码)。

private final Handler mHandler = new Handler() {
       @Override
       public void handleMessage(Message msg) {
           if (msg.what == FLIP_MSG) {
               if (mRunning) {
                   showNext();
                   msg = obtainMessage(FLIP_MSG);
                   sendMessageDelayed(msg, mFlipInterval);
               }
           }
       }
   };

猜你喜欢

转载自blog.csdn.net/scholar_ii/article/details/80633762