Example #1
0
  @Override
  public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
    // Translate overlay and image
    float flexibleRange = mFlexibleSpaceImageHeight - mActionBarSize;
    int minOverlayTransitionY = mActionBarSize - mOverlayView.getHeight();
    ViewHelper.setTranslationY(
        mOverlayView, ScrollUtils.getFloat(-scrollY, minOverlayTransitionY, 0));
    ViewHelper.setTranslationY(
        mImageView, ScrollUtils.getFloat(-scrollY / 2, minOverlayTransitionY, 0));

    // Change alpha of overlay
    ViewHelper.setAlpha(mOverlayView, ScrollUtils.getFloat((float) scrollY / flexibleRange, 0, 1));

    // Scale title text
    float scale =
        1
            + ScrollUtils.getFloat(
                (flexibleRange - scrollY) / flexibleRange, 0, MAX_TEXT_SCALE_DELTA);
    ViewHelper.setPivotX(mTitleView, 0);
    ViewHelper.setPivotY(mTitleView, 0);
    ViewHelper.setScaleX(mTitleView, scale);
    ViewHelper.setScaleY(mTitleView, scale);

    // Translate title text
    int maxTitleTranslationY = (int) (mFlexibleSpaceImageHeight - mTitleView.getHeight() * scale);
    int titleTranslationY = maxTitleTranslationY - scrollY;
    ViewHelper.setTranslationY(mTitleView, titleTranslationY);

    // Translate FAB
    int maxFabTranslationY = mFlexibleSpaceImageHeight - mFab.getHeight() / 2;
    float fabTranslationY =
        ScrollUtils.getFloat(
            -scrollY + mFlexibleSpaceImageHeight - mFab.getHeight() / 2,
            mActionBarSize - mFab.getHeight() / 2,
            maxFabTranslationY);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      // On pre-honeycomb, ViewHelper.setTranslationX/Y does not set margin,
      // which causes FAB's OnClickListener not working.
      FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFab.getLayoutParams();
      lp.leftMargin = mOverlayView.getWidth() - mFabMargin - mFab.getWidth();
      lp.topMargin = (int) fabTranslationY;
      mFab.requestLayout();
    } else {
      ViewHelper.setTranslationX(mFab, mOverlayView.getWidth() - mFabMargin - mFab.getWidth());
      ViewHelper.setTranslationY(mFab, fabTranslationY);
    }

    // Show/hide FAB
    if (fabTranslationY < mFlexibleSpaceShowFabOffset) {
      hideFab();
    } else {
      showFab();
    }
  }
  private void translateTab(int scrollY, boolean animated) {
    int flexibleSpaceImageHeight =
        getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height);
    int tabHeight = getResources().getDimensionPixelSize(R.dimen.tab_height);
    View imageView = findViewById(R.id.image);
    View overlayView = findViewById(R.id.overlay);
    TextView titleView = (TextView) findViewById(R.id.title);

    // Translate overlay and image
    float flexibleRange = flexibleSpaceImageHeight - getActionBarSize();
    int minOverlayTransitionY = tabHeight - overlayView.getHeight();
    ViewHelper.setTranslationY(
        overlayView, ScrollUtils.getFloat(-scrollY, minOverlayTransitionY, 0));
    ViewHelper.setTranslationY(
        imageView, ScrollUtils.getFloat(-scrollY / 2, minOverlayTransitionY, 0));

    // Change alpha of overlay
    ViewHelper.setAlpha(overlayView, ScrollUtils.getFloat((float) scrollY / flexibleRange, 0, 1));

    // Scale title text
    float scale =
        1
            + ScrollUtils.getFloat(
                (flexibleRange - scrollY - tabHeight) / flexibleRange, 0, MAX_TEXT_SCALE_DELTA);
    setPivotXToTitle(titleView);
    ViewHelper.setPivotY(titleView, 0);
    ViewHelper.setScaleX(titleView, scale);
    ViewHelper.setScaleY(titleView, scale);

    // Translate title text
    int maxTitleTranslationY = flexibleSpaceImageHeight - tabHeight - getActionBarSize();
    int titleTranslationY = maxTitleTranslationY - scrollY;
    ViewHelper.setTranslationY(titleView, titleTranslationY);

    // If tabs are moving, cancel it to start a new animation.
    ViewPropertyAnimator.animate(mSlidingTabLayout).cancel();
    // Tabs will move between the top of the screen to the bottom of the image.
    float translationY =
        ScrollUtils.getFloat(
            -scrollY + mFlexibleSpaceHeight - mTabHeight, 0, mFlexibleSpaceHeight - mTabHeight);
    if (animated) {
      // Animation will be invoked only when the current tab is changed.
      ViewPropertyAnimator.animate(mSlidingTabLayout)
          .translationY(translationY)
          .setDuration(200)
          .start();
    } else {
      // When Fragments' scroll, translate tabs immediately (without animation).
      ViewHelper.setTranslationY(mSlidingTabLayout, translationY);
    }
  }
 public void testGetFloat() {
   Assert.assertEquals(1.0f, ScrollUtils.getFloat(1, 0, 2));
   assertEquals(0.0f, ScrollUtils.getFloat(-1, 0, 2));
   assertEquals(2.0f, ScrollUtils.getFloat(3, 0, 2));
 }