Android仿Boss直聘我的界面滑動效果

最近在找工作著觉,我在使用boss投簡歷的時候,看到boss的我的界面蠻有意思的嘉涌,就想如何去實(shí)現(xiàn)它妻熊,可能是職業(yè)病吧,所以就打算仿一下仑最。先看下仿的效果扔役。

image

其實(shí)我們拿到這個效果的時候,看到滑動警医,折疊等效果就應(yīng)該想到了Material design亿胸,那么我們現(xiàn)在就可以基本的布局下。

activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_behavior="com.zwl.mybossdemo.AppBarLayoutOverScrollViewBehavior">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/mCollapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                android:id="@+id/topview"
                layout="@layout/layout_top_view"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.9" />


        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>


    <include layout="@layout/layout_content" />


    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:elevation="5dp"
        android:gravity="center"
        android:text="大白"
        android:textColor="@android:color/white"
        android:textSize="28sp"
        android:textStyle="bold"
        android:typeface="monospace" />
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0"
        android:background="@color/colorAccent"
        app:layout_collapseMode="pin" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

layout_top_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/colorAccent"
   android:orientation="vertical"
   android:paddingTop="40dp"
   app:layout_collapseMode="parallax">


   <RelativeLayout
       android:id="@+id/top_view_id"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/top_view">


       <TextView
           android:id="@+id/jianli"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_below="@+id/name"
           android:layout_marginLeft="30dp"
           android:paddingTop="35dp"
           android:paddingBottom="15dp"
           android:text="我的在線簡歷 "
           android:textColor="@android:color/white" />

       <de.hdodenhof.circleimageview.CircleImageView
           android:id="@+id/me_icon"
           android:layout_width="60dp"
           android:layout_height="60dp"
           android:layout_alignParentRight="true"
           android:layout_marginRight="30dp"
           android:layout_marginBottom="10dp"
           android:src="@drawable/boss_avatar_7" />
   </RelativeLayout>

   <LinearLayout
       android:id="@+id/top_view"
       android:layout_width="match_parent"
       android:layout_height="80dp"
       android:layout_alignParentBottom="true"
       android:gravity="center_vertical"
       android:orientation="horizontal"
       android:padding="15dp">

       <TextView
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:text="301\n溝通過"
           android:textColor="@android:color/white"
           android:textSize="16sp" />

       <TextView
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:text="301\n溝通過"
           android:textColor="@android:color/white"
           android:textSize="16sp" />

       <TextView
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:text="301\n溝通過"
           android:textColor="@android:color/white"
           android:textSize="16sp" />

       <TextView
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:text="301\n溝通過"
           android:textColor="@android:color/white"
           android:textSize="16sp" />

   </LinearLayout>


   <LinearLayout
       android:id="@+id/cardview"
       android:layout_width="match_parent"
       android:layout_height="120dp"
       android:layout_gravity="center_horizontal"
       android:gravity="center_horizontal|bottom">

       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/boss_card_bg" />
   </LinearLayout>

</LinearLayout>

layout_content.xml 這里就是一個圖片 截取Boss的

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/white"
   android:clickable="true"
   android:scrollbars="none"
   app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <ImageView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:src="@drawable/boss_user_bottom" />

</androidx.core.widget.NestedScrollView>

上面的activity_main.xml中可以看到 我自定義了一個AppBarLayout的behavior预皇, AppBarLayoutOverScrollViewBehavior 其實(shí)所有的效果都是這這里去實(shí)現(xiàn)的侈玄。

AppBarLayoutOverScrollViewBehavior

public class AppBarLayoutOverScrollViewBehavior extends AppBarLayout.Behavior {
   private int mAppBarHeight;
   private View mCardView;
   private boolean isAnimate;
   private float mTotalDy;
   private float mLastScale;
   private int mLastBottom;
   private int mCardViewHeight;
   private int mLimitHeight;
   private View mToolBar;
   private float scaleValue = 2f / 3;// 顯示卡片的三分之一 所以拋出三分之二
   private View mNameTitle;

   public AppBarLayoutOverScrollViewBehavior() {
   }

   public AppBarLayoutOverScrollViewBehavior(Context context, AttributeSet attrs) {
       super(context, attrs);
   }


   @Override
   public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout abl, int layoutDirection) {
       boolean handled = super.onLayoutChild(parent, abl, layoutDirection);
       if (null == mCardView) {
           mCardView = parent.findViewById(R.id.cardview);
       }
       if (null == mToolBar) {
           mToolBar = parent.findViewById(R.id.toolbar);
       }
       if (null == mNameTitle) {
           mNameTitle = parent.findViewById(R.id.name);
       }

       init(abl);
       return handled;
   }


   @Override
   public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {
       if (velocityY > 100) {
           isAnimate = false;
       }
       return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
   }


   @Override
   public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int type) {
       super.onStopNestedScroll(coordinatorLayout, abl, target, type);
       //恢復(fù)位置
       if (abl.getBottom() > mLimitHeight) {
           recovery(abl);
       }
   }


   @Override
   public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
       //開始滾動了 就動畫歸位
       isAnimate = true;
       return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type);
   }


   @Override
   public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
       if (mCardView != null && ((dy <= 0 && child.getBottom() >= mLimitHeight) || (dy > 0 && child.getBottom() > mLimitHeight))) {
           scrollY(child, target, dy);
       } else {
           setViewAlpha(child, dy);
           super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
       }
   }


   /**
    * 初始化數(shù)據(jù)
    *
    * @param appBarLayout
    */
   private void init(final AppBarLayout appBarLayout) {
       appBarLayout.setClipChildren(false);
       //整個AppbarLayout高度
       mAppBarHeight = appBarLayout.getMeasuredHeight();
       //卡片的高度
       mCardViewHeight = mCardView.getMeasuredHeight();
       //折疊正常的高度
       mLimitHeight = mAppBarHeight - (int) (mCardViewHeight * scaleValue);

       //默認(rèn)1s折疊
       appBarLayout.postDelayed(new Runnable() {
           @Override
           public void run() {
               ValueAnimator anim = ValueAnimator.ofFloat(0, 1f).setDuration(200);
               anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                   @Override
                   public void onAnimationUpdate(ValueAnimator animation) {
                       float value = (float) animation.getAnimatedValue();
                       appBarLayout.setBottom((int) (mAppBarHeight - value * mCardViewHeight * scaleValue));
                   }
               });
               anim.start();
           }
       }, 1000);

   }


   /**
    * 混動
    *
    * @param child
    * @param target
    * @param dy
    */
   private void scrollY(AppBarLayout child, View target, int dy) {
       mTotalDy += -dy;
       mTotalDy = Math.min(mTotalDy, mLimitHeight);
       mLastScale = Math.max(1f, 1f + (mTotalDy / mLimitHeight));
       mLastBottom = mLimitHeight + (int) (mCardViewHeight * scaleValue * (mLastScale - 1));
       child.setBottom(mLastBottom);
       target.setScrollY(0);
   }


   /**
    * 根據(jù)滑動設(shè)置 toolbar  名字顯示效果
    *
    * @param target
    * @param dy
    */
   private void setViewAlpha(View target, int dy) {
       float percent = Math.abs(target.getY() / mLimitHeight);
       if (percent >= 1) {
           percent = 1f;
       }
       //設(shè)置toolbar的透明度
       mToolBar.setAlpha(percent);

       //設(shè)置名字縮放
       mNameTitle.setScaleX(Math.max(0.8f, 1 - percent));
       mNameTitle.setScaleY(Math.max(0.8f, 1 - percent));

       //設(shè)置名字平移
       int offset = mNameTitle.getTop() - mToolBar.getTop();
       mNameTitle.setTranslationY(-offset * percent);
   }

   /**
    * 恢復(fù)位置
    *
    * @param abl
    */
   private void recovery(final AppBarLayout abl) {
       if (mTotalDy >= 0) {
           mTotalDy = 0;
           if (isAnimate) {
               ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1f).setDuration(200);
               valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                   @Override
                   public void onAnimationUpdate(ValueAnimator animation) {
                       float value = (float) animation.getAnimatedValue();
                       int offsetY = abl.getBottom() - mLimitHeight;
                       abl.setBottom((int) (abl.getBottom() - (value * offsetY)));
                       abl.setScrollY(0);
                   }
               });
               valueAnimator.start();
           } else {
               abl.setBottom(mLimitHeight);
               abl.setScrollY(0);
           }
       }
   }


}

其實(shí)代碼很簡單 ,所以就不在講解了吟温,主要就是通過監(jiān)聽滑動距離來設(shè)置控件的屬性序仙,這里附上github鏈接,可以直接下載看下鲁豪。

github項(xiàng)目

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末潘悼,一起剝皮案震驚了整個濱河市洋丐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌挥等,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異掷漱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)榄檬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門海雪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人湾宙,你說我怎么就攤上這事侠鳄∥岸瘢” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長静盅。 經(jīng)常有香客問我明垢,道長痊银,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮抖单,結(jié)果婚禮上矛绘,老公的妹妹穿的比我還像新娘羹应。我一直安慰自己园匹,他們只是感情好掖桦,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著雀久,像睡著了一般。 火紅的嫁衣襯著肌膚如雪越庇。 梳的紋絲不亂的頭發(fā)上涩惑,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天碰纬,我揣著相機(jī)與錄音寿桨,去河邊找鬼。 笑死预烙,一個胖子當(dāng)著我的面吹牛扁掸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播最域,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼谴分,長吁一口氣:“原來是場噩夢啊……” “哼镀脂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起薄翅,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后怀大,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蓖康,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡垒手,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年蒜焊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片榜掌。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡优妙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出憎账,到底是詐尸還是另有隱情套硼,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布胞皱,位于F島的核電站邪意,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏朴恳。R本人自食惡果不足惜抄罕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望于颖。 院中可真熱鬧,春花似錦嚷兔、人聲如沸森渐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽同衣。三九已至,卻和暖如春壶运,著一層夾襖步出監(jiān)牢的瞬間耐齐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留埠况,地道東北人耸携。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像辕翰,于是被迫代替她去往敵國和親夺衍。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354