Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 19, 2020 20:19
AnimatedLayout part 5: update LinearLayout with parallaxValue
public void applyParallax(float translateValue) {
mBackgroundImg.setTranslationY(translateValue - this.getHeight() / 8f);
mTitleTV.setTranslationY(+translateValue * 2);
mProfileCircleIV.setTranslationY(translateValue * 2);
mWeatherIcon.setTranslationY(translateValue * 2);
}
@yuriyskulskiy
yuriyskulskiy / CustomLinearLayout.java
Created August 19, 2020 20:04
AnimatedLayout part 5: update LinearLayout with parallaxValue
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
RecyclerView.State state) {
...
if (parentLayout != null) {
animatedLayout = parentLayout.findViewById(R.id.transformItem);
float translateValue = computeCurrentParallaxOffset(parentLayout);
animatedLayout.applyParallax(translateValue);
}
...
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 17, 2020 19:39
AnimatedLayout part 4: fix incorrect text width bug
public class AnimatedLayout extends ConstraintLayout {
...
private void applySummerTitleTV() {
mTitleTV.setBackgroundResource(R.drawable.summer_text_background);
mTitleTV.setTextColor(Color.BLACK);
mTitleTV.setText("WINTER IS COMING...");
// fix bug: text view with wrap_content layout param
// does not resize its width and
// last word of new text will not displayed if new string is longer
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 17, 2020 18:57
AnimatedLayout part 4: update AnimatedLayout.java
...
private int mIdleState = SUMMER_STATE; // set initial state as SUMMER_STATE
...
public void animateBy(int dy) {
float newOffset;
if (dy > 0) {
newOffset = (mOffsetValue - dy) < 0 ? 0 : mOffsetValue - dy;
} else {
//scroll to the left
@yuriyskulskiy
yuriyskulskiy / CustomLinearLayout.java
Last active August 17, 2020 18:48
AnimatedLayout part 4: implement CustomLinearLayout
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 17, 2020 03:02
AnimatedLayout part 3: implement fling end listener
private void init() {
...
mFling.addEndListener(new DynamicAnimation.OnAnimationEndListener() {
@Override
public void onAnimationEnd(DynamicAnimation animation, boolean canceled, float value, float velocity) {
if (!canceled) {
if (mOffsetValue == 0 || mOffsetValue == getWidth()) {
//correct ending
mCurrentAnimation = IDLE_ANIMATION_STATE;
if (mOffsetValue == 0) {
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 17, 2020 02:35
AnimatedLayout part 3: update onTouch() method
@Override
public boolean onTouchEvent(MotionEvent event) {
isTouching = true;
mFling.cancel();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mPreviousTouchX = event.getX();
if (mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity
// of a motion.
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 20, 2020 22:52
AnimatedLayout part 3: add additional const and fields
private float MIN_FLING_START_VELOCITY_DP = 500;
private float mCurrentMinFlingVelocityPx;
private VelocityTracker mVelocityTracker;
private boolean isTouching = false;
private FlingAnimation mFling = new FlingAnimation(this,
new FloatPropertyCompat<AnimatedLayout>("offset") {
@Override
public float getValue(AnimatedLayout object) {
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 14, 2020 17:49
AnimatedLayout_part_2 update child during scrolling
private void updateOffset(float newOffsetValue) {
...
updateIconView();
}
private void updateIconView() {
float oldTranslatePosition = mWeatherIcon.getTranslationX();
float newTranslatePosition;
float newAlpha;
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 14, 2020 17:16
AnimatedLayout_part_2 exclude from screenshot and clipping
public class AnimatedLayout extends ConstraintLayout {
...
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
if (canvas instanceof ScreenShotCanvas) {
if (isChildExcluded(child)) {
// do not draw this child to the screenshot canvas
return false;
}
return super.drawChild(canvas, child, drawingTime);