content: package com.allyn.lives.activity;
  
  import android.os.Bundle;
  import android.support.design.widget.AppBarLayout;
  import android.support.design.widget.CollapsingToolbarLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.view.animation.AlphaAnimation;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import com.allyn.lives.R;
  
  import butterknife.Bind;
  import butterknife.ButterKnife;
  import de.hdodenhof.circleimageview.CircleImageView;
  
  public class ConstMainActivity extends AppCompatActivity {
      // 控制ToolBar的变量
      private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR = 0.9f;
      private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS = 0.3f;
  
      private static final int ALPHA_ANIMATIONS_DURATION = 200;
  
      private boolean mIsTheTitleVisible = false;
      private boolean mIsTheTitleContainerVisible = true;
  
      @Bind(R.id.main_iv_placeholder)
      ImageView mIvPlaceholder; // 大图片
  
      @Bind(R.id.main_ll_title_container)
      LinearLayout mLlTitleContainer; // Title的LinearLayout
  
      @Bind(R.id.main_fl_title)
      FrameLayout mFlTitleContainer; // Title的FrameLayout
  
      @Bind(R.id.main_abl_app_bar)
      AppBarLayout mAblAppBar; // 整个可以滑动的AppBar
  
      @Bind(R.id.main_tv_toolbar_title)
      TextView mTvToolbarTitle; // 标题栏Title
  
      @Bind(R.id.main_tb_toolbar)
      Toolbar mTbToolbar; // 工具栏
  
      @Bind(R.id.civ)
      CircleImageView civ;
      @Bind(R.id.lvMenu)
      LinearLayout lvMenu;
  //    @Bind(R.id.civ)
  //    ImageView civ;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_const);
          ButterKnife.bind(this);
  
          mTbToolbar.setTitle("");
          setSupportActionBar(mTbToolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          mTbToolbar.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  onBackPressed();
              }
          });
          // AppBar的监听
          mAblAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
              @Override
              public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                  int maxScroll = appBarLayout.getTotalScrollRange();
                  float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
                  handleAlphaOnTitle(percentage);
                  handleToolbarTitleVisibility(percentage);
  
  
              }
          });
          initParallaxValues(); // 自动滑动效果
      }
  
      // 设置自动滑动的动画效果
      private void initParallaxValues() {
          CollapsingToolbarLayout.LayoutParams petDetailsLp =
                  (CollapsingToolbarLayout.LayoutParams) mIvPlaceholder.getLayoutParams();
  
          CollapsingToolbarLayout.LayoutParams petBackgroundLp =
                  (CollapsingToolbarLayout.LayoutParams) mFlTitleContainer.getLayoutParams();
  
          petDetailsLp.setParallaxMultiplier(0.9f);
          petBackgroundLp.setParallaxMultiplier(0.3f);
  
          mIvPlaceholder.setLayoutParams(petDetailsLp);
          mFlTitleContainer.setLayoutParams(petBackgroundLp);
      }
  
      // 处理ToolBar的显示
      private void handleToolbarTitleVisibility(float percentage) {
          if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
              if (!mIsTheTitleVisible) {
                  startAlphaAnimation(mTvToolbarTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                  mIsTheTitleVisible = true;
              }
          } else {
              if (mIsTheTitleVisible) {
                  startAlphaAnimation(mTvToolbarTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                  mIsTheTitleVisible = false;
              }
          }
      }
  
      // 控制Title的显示
      private void handleAlphaOnTitle(float percentage) {
          if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) {
              if (mIsTheTitleContainerVisible) {
                  startAlphaAnimation(mLlTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                  startAlphaAnimation(civ, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                  startAlphaAnimation(lvMenu, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                  mIsTheTitleContainerVisible = false;
              }
          } else {
              if (!mIsTheTitleContainerVisible) {
                  startAlphaAnimation(mLlTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                  startAlphaAnimation(civ, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                  startAlphaAnimation(lvMenu, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                  mIsTheTitleContainerVisible = true;
              }
          }
      }
  
      // 设置渐变的动画
      public static void startAlphaAnimation(View v, long duration, int visibility) {
          AlphaAnimation alphaAnimation = (visibility == View.VISIBLE)
                  ? new AlphaAnimation(0f, 1f)
                  : new AlphaAnimation(1f, 0f);
          alphaAnimation.setDuration(duration);
          alphaAnimation.setFillAfter(true);
          v.startAnimation(alphaAnimation);
      }
  }
  