Last active
December 6, 2017 21:56
-
-
Save bananavoid/72529446849888907172a9f431767c5b to your computer and use it in GitHub Desktop.
Example of using CoordinatorLayout.Behavior for image animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lifeslice.app.views; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.support.design.widget.AppBarLayout; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import com.facebook.drawee.view.SimpleDraweeView; | |
import com.lifeslice.app.R; | |
@SuppressWarnings("unused") | |
public class CollapsingImageBehavior extends CoordinatorLayout.Behavior<SimpleDraweeView> { | |
private final static int X = 0; | |
private final static int Y = 1; | |
private final static int WIDTH = 2; | |
private final static int HEIGHT = 3; | |
private int mTargetId; | |
private int[] mView; | |
private int[] mTarget; | |
public CollapsingImageBehavior(Context context, AttributeSet attrs) { | |
if (attrs != null) { | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CollapsingImageBehavior); | |
mTargetId = a.getResourceId(R.styleable.CollapsingImageBehavior_targetId, 0); | |
a.recycle(); | |
} | |
if (mTargetId == 0) { | |
throw new IllegalStateException("targetId attribute not specified on view for behavior"); | |
} | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, SimpleDraweeView child, View dependency) { | |
return dependency instanceof AppBarLayout; | |
} | |
@Override | |
public boolean onDependentViewChanged(CoordinatorLayout parent, SimpleDraweeView child, View dependency) { | |
setup(parent, child); | |
AppBarLayout appBarLayout = (AppBarLayout) dependency; | |
int range = appBarLayout.getTotalScrollRange(); | |
float factor = -appBarLayout.getY() / range; | |
int left = mView[X] + (int) (factor * (mTarget[X] - mView[X])); | |
int top = mView[Y] + (int) (factor * (mTarget[Y] - mView[Y])); | |
int width = mView[WIDTH] + (int) (factor * (mTarget[WIDTH] - mView[WIDTH])); | |
int height = mView[HEIGHT] + (int) (factor * (mTarget[HEIGHT] - mView[HEIGHT])); | |
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); | |
lp.width = width; | |
lp.height = height; | |
child.setLayoutParams(lp); | |
child.setX(left); | |
child.setY(top); | |
return true; | |
} | |
private void setup(CoordinatorLayout parent, View child) { | |
if (mView != null) return; | |
mView = new int[4]; | |
mTarget = new int[4]; | |
mView[X] = (int) child.getX(); | |
mView[Y] = (int) child.getY(); | |
mView[WIDTH] = child.getWidth(); | |
mView[HEIGHT] = child.getHeight(); | |
View target = parent.findViewById(mTargetId); | |
if (target == null) { | |
throw new IllegalStateException("target view not found"); | |
} | |
mTarget[WIDTH] += target.getWidth(); | |
mTarget[HEIGHT] += target.getHeight(); | |
View view = target; | |
while (view != parent) { | |
mTarget[X] += (int) view.getX(); | |
mTarget[Y] += (int) view.getY(); | |
view = (View) view.getParent(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<data> | |
<import type="java.lang.String" /> | |
<import type="android.view.View" /> | |
<import type="com.lifeslice.app.utils.TextUtils" /> | |
<variable | |
name="profile" | |
type="com.lifeslice.app.models.user.User" /> | |
</data> | |
<android.support.design.widget.CoordinatorLayout | |
android:id="@+id/coordinator_lt" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/white" | |
android:fitsSystemWindows="false"> | |
<android.support.design.widget.AppBarLayout | |
android:id="@+id/app_bar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fitsSystemWindows="false" | |
android:theme="@style/AppTheme.AppBarOverlay"> | |
<android.support.design.widget.CollapsingToolbarLayout | |
android:id="@+id/toolbar_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/colorPrimary" | |
android:fitsSystemWindows="false" | |
app:contentScrim="?attr/colorPrimary" | |
app:layout_scrollFlags="scroll|exitUntilCollapsed"> | |
<LinearLayout | |
android:id="@+id/info_lt" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/colorPrimary" | |
android:orientation="vertical" | |
app:layout_collapseMode="parallax" | |
app:layout_collapseParallaxMultiplier="0.3"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="70dp"> | |
<TextView | |
android:id="@+id/following_count" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_centerVertical="true" | |
android:fontFamily="@string/font_normal" | |
android:gravity="center" | |
android:paddingLeft="55dp" | |
android:tag="following" | |
android:textColor="@android:color/white" | |
android:textSize="12dp" | |
app:friendsCount='@{profile.followingCount}' /> | |
<View | |
android:id="@+id/ph" | |
android:layout_width="@dimen/avatar_expanded_size" | |
android:layout_height="@dimen/avatar_expanded_size" | |
android:layout_centerHorizontal="true" | |
android:layout_gravity="center" /> | |
<RelativeLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_centerVertical="true"> | |
<TextView | |
android:id="@+id/fans_count" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginRight="70dp" | |
android:fontFamily="@string/font_normal" | |
android:gravity="center" | |
android:paddingRight="10dp" | |
android:tag="fans" | |
android:textColor="@android:color/white" | |
android:textSize="12dp" | |
app:friendsCount='@{profile.fansCount}' /> | |
<TextView | |
android:id="@+id/new_fans_counter" | |
android:layout_width="12dp" | |
android:layout_height="12dp" | |
android:layout_alignRight="@+id/fans_count" | |
android:background="@drawable/custom_orange_circle" | |
android:fontFamily="@string/font_normal" | |
android:gravity="center" | |
android:text='@{profile.newFansCount >= 10 ? "9+" : String.valueOf(profile.newFansCount)}' | |
android:textAlignment="center" | |
android:textColor="@android:color/white" | |
android:textSize="9dp" | |
android:textStyle="bold" | |
android:visibility='@{profile.newFansCount == 0 ? View.GONE : View.VISIBLE}' /> | |
</RelativeLayout> | |
</RelativeLayout> | |
<TextView | |
android:id="@+id/description" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:fontFamily="@string/font_normal" | |
android:gravity="top|center_horizontal" | |
android:paddingLeft="40dp" | |
android:paddingRight="40dp" | |
android:scrollbars="vertical" | |
android:text='@{profile.description}' | |
android:textAlignment="center" | |
android:textColor="@android:color/white" | |
android:textSize="14dp" | |
android:visibility='@{profile.description != null & !profile.description.empty ? View.VISIBLE : View.GONE}' /> | |
</LinearLayout> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:minHeight="?attr/actionBarSize" | |
app:layout_anchor="@+id/fab" | |
app:layout_collapseMode="pin" | |
app:popupTheme="@style/AppTheme.PopupOverlay"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v4.widget.Space | |
android:id="@+id/avatar_target" | |
android:layout_width="40dp" | |
android:layout_height="40dp" | |
android:layout_marginLeft="40dp" | |
android:layout_centerVertical="true"/> | |
<include | |
android:id="@+id/header_lt" | |
layout="@layout/profile_toolbar_header" /> | |
</RelativeLayout> | |
</android.support.v7.widget.Toolbar> | |
</android.support.design.widget.CollapsingToolbarLayout> | |
</android.support.design.widget.AppBarLayout> | |
<com.facebook.drawee.view.SimpleDraweeView | |
android:id="@+id/profile_image" | |
android:layout_width="@dimen/avatar_expanded_size" | |
android:layout_height="@dimen/avatar_expanded_size" | |
android:layout_gravity="top|center_horizontal" | |
android:layout_marginTop="70dp" | |
android:elevation="10dp" | |
app:layout_behavior="com.lifeslice.app.views.CollapsingImageBehavior" | |
app:roundAsCircle="true" | |
app:roundingBorderColor="@android:color/white" | |
app:roundingBorderWidth="2dp" | |
app:targetId="@id/avatar_target" | |
app:userImage='@{profile}' /> | |
<com.lifeslice.app.views.SuperRecyclerView | |
android:id="@+id/user_stories_list" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> | |
<android.support.design.widget.FloatingActionButton | |
android:id="@+id/fab" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="16dp" | |
android:src="@drawable/profile_icon_add_friends" | |
android:visibility="gone" | |
app:backgroundTint="@android:color/white" | |
app:layout_anchor="@id/app_bar" | |
app:layout_anchorGravity="bottom|right|end" /> | |
</android.support.design.widget.CoordinatorLayout> | |
</layout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where R.styleable.CollapsingImageBehavior - CollapsingImageBehavior_targetId
?