How to add an ImageView with the title in collapsingtoolbarlayout in Android

前端 未结 2 869
深忆病人
深忆病人 2021-01-16 04:24

I am using CoordinatorLayout to get this effect Here is the layout code.

    


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 04:54

    You can follow this link...

    https://github.com/saulmm/CoordinatorBehaviorExample

    MainActivity.java

    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.Menu;
    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;
    
    public class MainActivity extends AppCompatActivity
        implements AppBarLayout.OnOffsetChangedListener {
    
        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;
    
        private LinearLayout mTitleContainer;
        private TextView mTitle;
        private AppBarLayout mAppBarLayout;
        private Toolbar mToolbar;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        bindActivity();
    
        mAppBarLayout.addOnOffsetChangedListener(this);
    
        mToolbar.inflateMenu(R.menu.menu_main);
        startAlphaAnimation(mTitle, 0, View.INVISIBLE);
        }
    
        private void bindActivity() {
        mToolbar        = (Toolbar) findViewById(R.id.main_toolbar);
        mTitle          = (TextView) findViewById(R.id.main_textview_title);
        mTitleContainer = (LinearLayout) findViewById(R.id.main_linearlayout_title);
        mAppBarLayout   = (AppBarLayout) findViewById(R.id.main_appbar);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
        }
    
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(offset) / (float) maxScroll;
    
        handleAlphaOnTitle(percentage);
        handleToolbarTitleVisibility(percentage);
        }
    
        private void handleToolbarTitleVisibility(float percentage) {
            if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
    
            if(!mIsTheTitleVisible) {
                startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                mIsTheTitleVisible = true;
            }
    
            } else {
    
            if (mIsTheTitleVisible) {
                startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                mIsTheTitleVisible = false;
            }
            }
        }
    
        private void handleAlphaOnTitle(float percentage) {
        if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) {
            if(mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
            mIsTheTitleContainerVisible = false;
            }
    
        } else {
    
            if (!mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
            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);
        }
        }
    

    activity_main.xml

    
    
        
    
        
    
            
    
            
    
            
    
                
    
                
    
            
            
        
        
    
    
        
    
        
    
            
        
    
    
        
    
        
    
        
    
            
    
            
    
        
        
    
        
      
    

提交回复
热议问题