Skip to content

Commit

Permalink
Managed Y values instead margins
Browse files Browse the repository at this point in the history
  • Loading branch information
saulmm committed Aug 25, 2015
1 parent a8d11c5 commit c638ead
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
50 changes: 30 additions & 20 deletions app/src/main/java/saulmm/myapplication/AvatarImageBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,67 @@ public class AvatarImageBehavior extends CoordinatorLayout.Behavior<CircleImageV
private float mAvatarMaxSize;
private float mMarginTop;

private float mFinalLeftAvatarPadding;
private float mStartPosition;

public AvatarImageBehavior(Context context, AttributeSet attrs) {
mContext = context;
init();

mFinalLeftAvatarPadding = context.getResources().getDimension(
R.dimen.abc_action_bar_navigation_padding_start_material);
}

private void init() {
bindDimensions();
}

private void bindDimensions() {
mAvatarMaxSize = mContext.getResources().getDimension(R.dimen.image_width);
mMarginTop = mContext.getResources().getDimension(R.dimen.image_margin);
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, CircleImageView child, View dependency) {

return dependency instanceof Toolbar;
}

@Override
public boolean onMeasureChild(CoordinatorLayout parent, CircleImageView child,
int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {

return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed,
parentHeightMeasureSpec, heightUsed);
}
// Child startPosition
int startPosition = 0;

// Toolbar half position
int finalPosition = 0;

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, CircleImageView child, View dependency) {

// Called once
if (startPosition == 0)
startPosition = (int) (child.getY() + (child.getHeight() / 2));

if (finalPosition == 0)
finalPosition = (dependency.getHeight() /2);

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();

final int maxNumber = (int) (mMarginTop - getStatusBarHeight());

float percentageFactor = dependency.getY() / maxNumber;
int proportionalAvatarSize = (int) (mAvatarMaxSize * (percentageFactor));
// Assume that the final position will be at the top
final int topPosition = 0;

float childMarginTop = dependency.getY() - (child.getHeight() / 2);
float childMarginLeft = (dependency.getWidth() / 2) - (child.getWidth() / 2);
float pChildMarginLeft = childMarginLeft * percentageFactor;
float currentPositionY = child.getY() + child.getHeight()/2;
float expandedPercentageFactor = dependency.getY() / maxNumber;

int extraFinalPadding = (int) (EXTRA_FINAL_AVATAR_PADDING * (1f - percentageFactor));

if (percentageFactor >= MIN_AVATAR_PERCENTAGE_SIZE) {
child.setY(startPosition - (((startPosition - finalPosition) * (1f - expandedPercentageFactor)) + (child.getHeight()/2)));
int proportionalAvatarSize = (int) (mAvatarMaxSize * (expandedPercentageFactor));//
//
if (expandedPercentageFactor >= MIN_AVATAR_PERCENTAGE_SIZE) {
lp.width = proportionalAvatarSize;
lp.height = proportionalAvatarSize;
}

lp.setMargins(
(int) pChildMarginLeft + extraFinalPadding,
(int) childMarginTop + extraFinalPadding,
lp.rightMargin,
lp.bottomMargin
);

child.setLayoutParams(lp);
return true;
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
android:layout_gravity="bottom|center_horizontal"
android:background="@color/primary"
android:orientation="vertical"
app:layout_collapseMode="parallax">
app:layout_collapseMode="parallax"
>

<LinearLayout
android:id="@+id/main.linearlayout.title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
Expand All @@ -58,7 +60,8 @@
android:gravity="bottom|center"
android:text="@string/quila_name"
android:textColor="@android:color/white"
android:textSize="30sp" />
android:textSize="30sp"
/>

<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -125,6 +128,7 @@
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="@dimen/image_width"
android:layout_height="@dimen/image_width"
android:layout_gravity="center"
android:src="@drawable/quila"
app:border_color="@android:color/white"
app:border_width="2dp"
Expand Down

0 comments on commit c638ead

Please sign in to comment.