Created
February 11, 2017 04:25
-
-
Save patrykpoborca/8bd4985f1676dcb427da1f1f6e61242b to your computer and use it in GitHub Desktop.
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 saulmm.myapplication; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.v7.widget.Toolbar; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.animation.AccelerateInterpolator; | |
import android.view.animation.DecelerateInterpolator; | |
import android.view.animation.Interpolator; | |
@SuppressWarnings("unused") | |
public class AvatarImageBehavior extends CoordinatorLayout.Behavior<WrapperTextView> { | |
private final static float MIN_AVATAR_PERCENTAGE_SIZE = 0.3f; | |
private final static int EXTRA_FINAL_AVATAR_PADDING = 80; | |
private final static String TAG = "behavior"; | |
private Context mContext; | |
private float mCustomFinalYPosition; | |
private float mCustomStartXPosition; | |
private float mCustomStartToolbarPosition; | |
private float mCustomStartHeight; | |
private float mCustomFinalHeight; | |
private float mAvatarMaxSize; | |
private float mFinalLeftAvatarPadding; | |
private float mStartPosition; | |
private int mStartXPosition; | |
private float mStartToolbarPosition; | |
private int mStartYPosition; | |
private int mFinalYPosition; | |
private int mStartHeight; | |
private int mFinalXPosition; | |
private float mChangeBehaviorPoint; | |
public AvatarImageBehavior(Context context, AttributeSet attrs) { | |
mContext = context; | |
if (attrs != null) { | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AvatarImageBehavior); | |
mCustomFinalYPosition = a.getDimension(R.styleable.AvatarImageBehavior_finalYPosition, 0); | |
mCustomStartXPosition = a.getDimension(R.styleable.AvatarImageBehavior_startXPosition, 0); | |
mCustomStartToolbarPosition = a.getDimension(R.styleable.AvatarImageBehavior_startToolbarPosition, 0); | |
mCustomStartHeight = a.getDimension(R.styleable.AvatarImageBehavior_startHeight, 0); | |
mCustomFinalHeight = a.getDimension(R.styleable.AvatarImageBehavior_finalHeight, 0); | |
a.recycle(); | |
} | |
init(); | |
mFinalLeftAvatarPadding = context.getResources().getDimension( | |
R.dimen.spacing_normal); | |
} | |
private void init() { | |
bindDimensions(); | |
} | |
private void bindDimensions() { | |
mAvatarMaxSize = mContext.getResources().getDimension(R.dimen.image_width); | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, WrapperTextView child, View dependency) { | |
return dependency instanceof Toolbar; | |
} | |
Interpolator interpolator = new DecelerateInterpolator(); | |
Interpolator slowInterpolator = new AccelerateInterpolator(); | |
@Override | |
public boolean onDependentViewChanged(CoordinatorLayout parent, WrapperTextView child, View dependency) { | |
maybeInitProperties(child, dependency); | |
final float ratio = 1f - (child.getY() / mStartYPosition); | |
final int maxScrollDistance = (int) (mStartToolbarPosition); | |
float expandedPercentageFactor = dependency.getY() / maxScrollDistance; | |
child.setScaleY(1 - ratio); | |
child.setScaleX(1 - ratio); | |
float distanceXToSubtract = ((mStartXPosition - mFinalXPosition) | |
* slowInterpolator.getInterpolation(ratio)) + (child.getHeight()/2); | |
float distanceYToSubtract = ((mStartYPosition - mFinalYPosition) | |
* interpolator.getInterpolation(1f - expandedPercentageFactor)) + (child.getHeight()/2); | |
child.setX(mStartXPosition - distanceXToSubtract); | |
child.setY(mStartYPosition - distanceYToSubtract); | |
// if (expandedPercentageFactor < mChangeBehaviorPoint) { | |
// float heightFactor = (mChangeBehaviorPoint - expandedPercentageFactor) / mChangeBehaviorPoint; | |
// | |
// float distanceXToSubtract = ((mStartXPosition - mFinalXPosition) | |
// * heightFactor) + (child.getHeight()/2); | |
// float distanceYToSubtract = ((mStartYPosition - mFinalYPosition) | |
// * (1f - expandedPercentageFactor)) + (child.getHeight()/2); | |
// | |
// child.setX(mStartXPosition - distanceXToSubtract); | |
// child.setY(mStartYPosition - distanceYToSubtract); | |
// | |
// float heightToSubtract = ((mStartHeight - mCustomFinalHeight) * heightFactor); | |
// | |
//// CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); | |
//// lp.width = (int) (mStartHeight - heightToSubtract); | |
//// lp.height = (int) (mStartHeight - heightToSubtract); | |
//// child.setLayoutParams(lp); | |
// | |
// float ratio = Math.max(0.20f, 1 - heightFactor); | |
// child.setScaleY(ratio); | |
// child.setScaleX(ratio); | |
// | |
// } else { | |
// float distanceYToSubtract = ((mStartYPosition - mFinalYPosition) | |
// * (1f - expandedPercentageFactor)) + (mStartHeight/2); | |
// | |
// child.setX(mStartXPosition - child.getWidth()/2); | |
// child.setY(mStartYPosition - distanceYToSubtract); | |
// | |
// CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); | |
// lp.width = (int) (mStartHeight); | |
// lp.height = (int) (mStartHeight); | |
// child.setLayoutParams(lp); | |
// } | |
return true; | |
} | |
private void maybeInitProperties(WrapperTextView child, View dependency) { | |
if (mStartYPosition == 0) | |
mStartYPosition = (int) (dependency.getY()); | |
if (mFinalYPosition == 0) | |
mFinalYPosition = (dependency.getHeight() /2); | |
if (mStartHeight == 0) | |
mStartHeight = child.getHeight(); | |
if (mStartXPosition == 0) | |
mStartXPosition = (int) (child.getX() + (child.getWidth() / 2)); | |
if (mFinalXPosition == 0) | |
mFinalXPosition = mContext.getResources().getDimensionPixelOffset(R.dimen.abc_action_bar_content_inset_material) + ((int) mCustomFinalHeight / 2); | |
if (mStartToolbarPosition == 0) | |
mStartToolbarPosition = dependency.getY(); | |
if (mChangeBehaviorPoint == 0) { | |
mChangeBehaviorPoint = (child.getHeight() - mCustomFinalHeight) / (2f * (mStartYPosition - mFinalYPosition)); | |
} | |
} | |
public int getStatusBarHeight() { | |
int result = 0; | |
int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
result = mContext.getResources().getDimensionPixelSize(resourceId); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment