Java tutorial
/* * Copyright 2015 Yan Zhenjie * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cnc.cad.rxjavatestapp; import android.content.Context; import android.content.res.TypedArray; import android.support.design.widget.CoordinatorLayout; import android.support.v4.view.ViewCompat; import android.support.v7.widget.Toolbar; import android.util.AttributeSet; import android.view.View; import de.hdodenhof.circleimageview.CircleImageView; /** * Created by Yan Zhenjie on 2016/9/24. */ public class AvatarImageBehavior extends CoordinatorLayout.Behavior<CircleImageView> { private Context mContext; private float mCustomFinalHeight; private int mStartXPosition; private float mStartToolbarPosition; private int mStartYPosition; private int mFinalYPosition; private int mStartHeight; private int mStartWidth; 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); mCustomFinalHeight = a.getDimension(R.styleable.AvatarImageBehavior_finalHeight, 0); a.recycle(); } } @Override public boolean layoutDependsOn(CoordinatorLayout parent, CircleImageView child, View dependency) { return dependency instanceof Toolbar; } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, CircleImageView child, View dependency) { maybeInitProperties(child, dependency); final int maxScrollDistance = (int) (mStartToolbarPosition); float expandedPercentageFactor = ViewCompat.getY(dependency) / maxScrollDistance; 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); ViewCompat.setX(child, mStartXPosition - distanceXToSubtract); ViewCompat.setY(child, 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); } else { float distanceYToSubtract = ((mStartYPosition - mFinalYPosition) * (1f - expandedPercentageFactor)) + (mStartHeight / 2); ViewCompat.setX(child, mStartXPosition - child.getWidth() / 2); ViewCompat.setY(child, mStartYPosition - distanceYToSubtract); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); lp.width = mStartWidth; lp.height = mStartHeight; child.setLayoutParams(lp); } return true; } private void maybeInitProperties(CircleImageView child, View dependency) { if (mStartYPosition == 0) mStartYPosition = (int) (dependency.getY()); if (mFinalYPosition == 0) mFinalYPosition = (dependency.getHeight() / 2); if (mStartHeight == 0) mStartWidth = mStartHeight = child.getHeight(); if (mStartXPosition == 0) mStartXPosition = (int) (ViewCompat.getX(child) + (child.getWidth() / 2)); if (mFinalXPosition == 0) mFinalXPosition = mContext.getResources().getDimensionPixelOffset(R.dimen.dp_25) + ((int) mCustomFinalHeight / 2); if (mStartToolbarPosition == 0) mStartToolbarPosition = ViewCompat.getY(dependency); if (mChangeBehaviorPoint == 0) { mChangeBehaviorPoint = (child.getHeight() - mCustomFinalHeight) / (2f * (mStartYPosition - mFinalYPosition)); } } }