| 1 | /* |
| 2 | * This is the source code of Telegram for Android v. 5.x.x. |
| 3 | * It is licensed under GNU GPL v. 2 or later. |
| 4 | * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | * |
| 6 | * Copyright Nikolai Kudashov, 2013-2018. |
| 7 | */ |
| 8 | |
| 9 | package androidx.recyclerview.widget; |
| 10 | |
| 11 | import android.content.Context; |
| 12 | import android.graphics.PointF; |
| 13 | import androidx.annotation.Nullable; |
| 14 | import android.view.View; |
| 15 | import android.view.animation.DecelerateInterpolator; |
| 16 | import android.view.animation.LinearInterpolator; |
| 17 | |
| 18 | public class LinearSmoothScrollerEnd extends RecyclerView.SmoothScroller { |
| 19 | |
| 20 | private static final float MILLISECONDS_PER_INCH = 25f; |
| 21 | |
| 22 | private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000; |
| 23 | |
| 24 | private static final float TARGET_SEEK_EXTRA_SCROLL_RATIO = 1.2f; |
| 25 | |
| 26 | protected final LinearInterpolator mLinearInterpolator = new LinearInterpolator(); |
| 27 | |
| 28 | protected final DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator(1.5f); |
| 29 | |
| 30 | protected PointF mTargetVector; |
| 31 | |
| 32 | private final float MILLISECONDS_PER_PX; |
| 33 | |
| 34 | protected int mInterimTargetDx = 0, mInterimTargetDy = 0; |
| 35 | |
| 36 | public LinearSmoothScrollerEnd(Context context) { |
| 37 | MILLISECONDS_PER_PX = MILLISECONDS_PER_INCH / context.getResources().getDisplayMetrics().densityDpi; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected void onStart() { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { |
| 47 | final int dx = calculateDxToMakeVisible(targetView); |
| 48 | final int time = calculateTimeForDeceleration(dx); |
| 49 | if (time > 0) { |
| 50 | action.update(-dx, 0, Math.max(400, time), mDecelerateInterpolator); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, Action action) { |
| 56 | if (getChildCount() == 0) { |
| 57 | stop(); |
| 58 | return; |
| 59 | } |
| 60 | mInterimTargetDx = clampApplyScroll(mInterimTargetDx, dx); |
| 61 | mInterimTargetDy = clampApplyScroll(mInterimTargetDy, dy); |
| 62 | |
| 63 | if (mInterimTargetDx == 0 && mInterimTargetDy == 0) { |
| 64 | updateActionForInterimTarget(action); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onStop() { |
| 70 | mInterimTargetDx = mInterimTargetDy = 0; |
| 71 | mTargetVector = null; |
| 72 | } |
| 73 | |
| 74 | protected int calculateTimeForDeceleration(int dx) { |
| 75 | return (int) Math.ceil(calculateTimeForScrolling(dx) / .3356); |
| 76 | } |
| 77 | |
| 78 | protected int calculateTimeForScrolling(int dx) { |
| 79 | return (int) Math.ceil(Math.abs(dx) * MILLISECONDS_PER_PX); |
| 80 | } |
| 81 | |
| 82 | protected void updateActionForInterimTarget(Action action) { |
| 83 | // find an interim target position |
| 84 | PointF scrollVector = computeScrollVectorForPosition(getTargetPosition()); |
| 85 | if (scrollVector == null || (scrollVector.x == 0 && scrollVector.y == 0)) { |
| 86 | final int target = getTargetPosition(); |
| 87 | action.jumpTo(target); |
| 88 | stop(); |
| 89 | return; |
| 90 | } |
| 91 | normalize(scrollVector); |
| 92 | mTargetVector = scrollVector; |
| 93 | |
| 94 | mInterimTargetDx = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.x); |
| 95 | mInterimTargetDy = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.y); |
| 96 | final int time = calculateTimeForScrolling(TARGET_SEEK_SCROLL_DISTANCE_PX); |
| 97 | // To avoid UI hiccups, trigger a smooth scroll to a distance little further than the |
| 98 | // interim target. Since we track the distance travelled in onSeekTargetStep callback, it |
| 99 | // won't actually scroll more than what we need. |
| 100 | action.update((int) (mInterimTargetDx * TARGET_SEEK_EXTRA_SCROLL_RATIO) |
| 101 | , (int) (mInterimTargetDy * TARGET_SEEK_EXTRA_SCROLL_RATIO) |
| 102 | , (int) (time * TARGET_SEEK_EXTRA_SCROLL_RATIO), mLinearInterpolator); |
| 103 | } |
| 104 | |
| 105 | private int clampApplyScroll(int tmpDt, int dt) { |
| 106 | final int before = tmpDt; |
| 107 | tmpDt -= dt; |
| 108 | if (before * tmpDt <= 0) { |
| 109 | return 0; |
| 110 | } |
| 111 | return tmpDt; |
| 112 | } |
| 113 | |
| 114 | public int calculateDxToMakeVisible(View view) { |
| 115 | final RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 116 | if (layoutManager == null || !layoutManager.canScrollHorizontally()) { |
| 117 | return 0; |
| 118 | } |
| 119 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); |
| 120 | int left = layoutManager.getDecoratedLeft(view) - params.leftMargin; |
| 121 | int rigth = layoutManager.getDecoratedRight(view) + params.rightMargin; |
| 122 | int start = layoutManager.getPaddingLeft(); |
| 123 | int end = layoutManager.getWidth() - layoutManager.getPaddingRight(); |
| 124 | |
| 125 | if (left > start && rigth < end) { |
| 126 | return 0; |
| 127 | } |
| 128 | int boxSize = end - start; |
| 129 | int viewSize = rigth - left; |
| 130 | start = boxSize - viewSize; |
| 131 | end = start + viewSize; |
| 132 | final int dtStart = start - left; |
| 133 | if (dtStart > 0) { |
| 134 | return dtStart; |
| 135 | } |
| 136 | final int dtEnd = end - rigth; |
| 137 | if (dtEnd < 0) { |
| 138 | return dtEnd; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | @Nullable |
| 144 | public PointF computeScrollVectorForPosition(int targetPosition) { |
| 145 | RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 146 | if (layoutManager instanceof ScrollVectorProvider) { |
| 147 | return ((ScrollVectorProvider) layoutManager).computeScrollVectorForPosition(targetPosition); |
| 148 | } |
| 149 | return null; |
| 150 | } |
| 151 | } |
| 152 | |