[android源码] smarttablayout源码解析

1: demo的入口,自己推测用一个绝对或者相对布局写死,但是发现竟然用enum来写的,有点儿出乎意料

enum可以有构造体,可以有函数,还能产生序列的对应值

如果不用enum,那么需要需要用类来封装,然后放到map里面,这里enum内部直接生成了map





下面看对enum的常量的访问:

    for (Demo demo : Demo.values()) {
      demoAdapter.add(getString(demo.titleResId));
    }
  将enmu类型中的demo的常量提取出来


2: smarttab tabview 是一个linearlayout, 更像是一个容器,主要负责画焦点的indicator和OverLine和UnderLine

自定义view如果要走自己的onDraw那么需要设置setViewNotDraw(false);

onDraw:
if (selectionOffset > 0f && selectedPosition < (getChildCount() - 1)) {
    int nextColor = tabColorizer.getIndicatorColor(selectedPosition + 1);
    if (color != nextColor) {
      color = blendColors(nextColor, color, selectionOffset);
    }

    // Draw the selection partway between the tabs
    float startOffset = indicationInterpolator.getLeftEdge(selectionOffset);
    float endOffset = indicationInterpolator.getRightEdge(selectionOffset);
    float thicknessOffset = indicationInterpolator.getThickness(selectionOffset);

    View nextTab = getChildAt(selectedPosition + 1);
    int nextStart = Utils.getStart(nextTab, indicatorWithoutPadding);
    int nextEnd = Utils.getEnd(nextTab, indicatorWithoutPadding);
    if (isLayoutRtl) {
      left = (int) (endOffset * nextEnd + (1.0f - endOffset) * left);
      right = (int) (startOffset * nextStart + (1.0f - startOffset) * right);
    } else {
      left = (int) (startOffset * nextStart + (1.0f - startOffset) * left);
      right = (int) (endOffset * nextEnd + (1.0f - endOffset) * right);
    }
    thickness = thickness * thicknessOffset;
  }

  drawIndicator(canvas, left, right, height, thickness, color);

}

if (!indicatorInFront) {
  drawOverline(canvas, 0, width);
  drawUnderline(canvas, 0, getWidth(), height);
}

3: 关于draw indicator的interplator需要看具体的偏移
02-18 18:41:04.035    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.0074074073 startoffset is1.6519517E-13the endOffset is 0.043629594left is0   the right is316
02-18 18:41:04.050    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.037962962 startoffset is2.9933709E-9the endOffset is 0.20722358left is0   the right is360
02-18 18:41:04.070    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.075 startoffset is1.7797856E-7the endOffset is 0.3736019left is0   the right is404
02-18 18:41:04.085    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.12222222 startoffset is3.3335045E-6the endOffset is 0.5425881left is0   the right is449
02-18 18:41:04.100    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.15833333 startoffset is1.575557E-5the endOffset is 0.64449894left is0   the right is476
02-18 18:41:04.120    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.18703704 startoffset is4.2812015E-5the endOffset is 0.7113148left is0   the right is494
02-18 18:41:04.135    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.21851853 startoffset is1.0887535E-4the endOffset is 0.77222174left is0   the right is510
02-18 18:41:04.150    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.24722221 startoffset is2.2831002E-4the endOffset is 0.8180295left is0   the right is522
02-18 18:41:04.170    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.28425926 startoffset is5.275793E-4the endOffset is 0.86555815left is0   the right is535
02-18 18:41:04.185    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.31574073 startoffset is9.907947E-4the endOffset is 0.8973582left is0   the right is543
02-18 18:41:04.200    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.34814814 startoffset is0.0017806742the endOffset is 0.9232826left is0   the right is550
02-18 18:41:04.220    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.3712963 startoffset is0.0026201347the endOffset is 0.9382444left is0   the right is554
02-18 18:41:04.235    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.3935185 startoffset is0.0037135645the endOffset is 0.95023715left is1   the right is557
02-18 18:41:04.255    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.41296297 startoffset is0.0049598287the endOffset is 0.9590746left is1   the right is560
02-18 18:41:04.265    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.42962962 startoffset is0.006288764the endOffset is 0.9655696left is1   the right is561
02-18 18:41:04.285    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.44814816 startoffset is0.008100834the endOffset is 0.97175545left is2   the right is563
02-18 18:41:04.300    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.46944445 startoffset is0.010702994the endOffset is 0.9776959left is3   the right is565
02-18 18:41:04.315    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.48703703 startoffset is0.0133466385the endOffset is 0.98178136left is4   the right is566
02-18 18:41:04.335    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.50555557 startoffset is0.016696038the endOffset is 0.98538816left is5   the right is567
02-18 18:41:04.350    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.5324074 startoffset is0.022775322the endOffset is 0.98954785left is6   the right is568
02-18 18:41:04.365    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.5537037 startoffset is0.028818056the endOffset is 0.992098left is8   the right is568
02-18 18:41:04.385    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.5833333 startoffset is0.039400402the endOffset is 0.9947672left is12   the right is569
02-18 18:41:04.400    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.6064815 startoffset is0.049762856the endOffset is 0.99628645left is15   the right is570
02-18 18:41:04.415    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.6462963 startoffset is0.07287694the endOffset is 0.99804187left is22   the right is570
02-18 18:41:04.435    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.70092595 startoffset is0.11858583the endOffset is 0.9992844left is36   the right is570
02-18 18:41:04.450    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.74814814 startoffset is0.17535801the endOffset is 0.99974483left is53   the right is570
02-18 18:41:04.465    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.77962965 startoffset is0.2245588the endOffset is 0.9998855left is68   the right is570
02-18 18:41:04.485    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.8074074 startoffset is0.2770488the endOffset is 0.999949left is84   the right is570
02-18 18:41:04.500    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.8342593 startoffset is0.33713686the endOffset is 0.99997926left is102   the right is570
02-18 18:41:04.515    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.837963 startoffset is0.34621748the endOffset is 0.9999819left is105   the right is570
02-18 18:41:04.530    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.8564815 startoffset is0.39473706the endOffset is 0.99999124left is120   the right is570
02-18 18:41:04.550    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.875 startoffset is0.44879532the endOffset is 0.9999962left is136   the right is570
02-18 18:41:04.565    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.8925926 startoffset is0.50573117the endOffset is 0.99999845left is154   the right is570
02-18 18:41:04.580    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.90833336 startoffset is0.56165755the endOffset is 0.9999994left is171   the right is570
02-18 18:41:04.600    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9212963 startoffset is0.6114993the endOffset is 0.99999976left is186   the right is570
02-18 18:41:04.615    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.93425924 startoffset is0.6649736the endOffset is 0.99999994left is202   the right is570
02-18 18:41:04.630    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9444444 startoffset is0.7096732the endOffset is 1.0left is216   the right is571
02-18 18:41:04.650    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9527778 startoffset is0.748083the endOffset is 1.0left is228   the right is571
02-18 18:41:04.665    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9611111 startoffset is0.78820944the endOffset is 1.0left is240   the right is571
02-18 18:41:04.680    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9675926 startoffset is0.8206448the endOffset is 1.0left is250   the right is571
02-18 18:41:04.695    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.97314817 startoffset is0.8493248the endOffset is 1.0left is259   the right is571
02-18 18:41:04.715    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9777778 startoffset is0.8738582the endOffset is 1.0left is266   the right is571
02-18 18:41:04.730    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9824074 startoffset is0.89897937the endOffset is 1.0left is274   the right is571
02-18 18:41:04.745    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9851852 startoffset is0.91433907the endOffset is 1.0left is278   the right is571
02-18 18:41:04.765    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.98888886 startoffset is0.93515784the endOffset is 1.0left is285   the right is571
02-18 18:41:04.780    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9907407 startoffset is0.94571453the endOffset is 1.0left is288   the right is571
02-18 18:41:04.800    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.99351853 startoffset is0.9617359the endOffset is 1.0left is293   the right is571
02-18 18:41:04.815    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.99444443 startoffset is0.96712613the endOffset is 1.0left is294   the right is571
02-18 18:41:04.830    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9962963 startoffset is0.97798246the endOffset is 1.0left is298   the right is571
02-18 18:41:04.845    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.99722224 startoffset is0.9834488the endOffset is 1.0left is299   the right is571
02-18 18:41:04.865    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.99814814 startoffset is0.9889402the endOffset is 1.0left is301   the right is571
02-18 18:41:04.895    7109-7109/com.ogaclejapan.smarttablayout.demo D/testoffset﹕ the offset interpolator selectionOffse is+0.9990741 startoffset is0.9944574the endOffset is 1.0left is303   the right is571

再比较偏移计算公式就能理解了:
 
          left = (int) (startOffset * nextStart + (1.0f - startOffset) * left);
          right = (int) (endOffset * nextEnd + (1.0f - endOffset) * right);


4: Tablayout顶部tab的定制view
    我的想法:是tab是linearlayout动态增加,每个view的子view不同,在初始化的时候加入到linearlayout中,
    可以借鉴的想法: 使用layout来做注入,实现动态增加;定制化view,一般不直接建立view和data之间的关系,而是中间加一层provider, 用于适配view和data
 
  private static class SimpleTabProvider implements TabProvider {

    private final LayoutInflater inflater;
    private final int tabViewLayoutId;
    private final int tabViewTextViewId;

    private SimpleTabProvider(Context context, int layoutResId, int textViewId) {
      inflater = LayoutInflater.from(context);
      tabViewLayoutId = layoutResId;
      tabViewTextViewId = textViewId;
    }

    @Override
    public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
      View tabView = null;
      TextView tabTitleView = null;

      if (tabViewLayoutId != NO_ID) {
        tabView = inflater.inflate(tabViewLayoutId, container, false);
      }

      if (tabViewTextViewId != NO_ID && tabView != null) {
        tabTitleView = (TextView) tabView.findViewById(tabViewTextViewId);
      }

      if (tabTitleView == null && TextView.class.isInstance(tabView)) {
        tabTitleView = (TextView) tabView;
      }

      if (tabTitleView != null) {
        tabTitleView.setText(adapter.getPageTitle(position));
      }

      return tabView;
    }

  }

特殊indicator的变化,一张图片变薄,再恢复原装的加速器为:
    return 1f / (1.0f - getLeftEdge(offset) + getRightEdge(offset));


 
 
 
 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值