170 lines
7.4 KiB
Java
170 lines
7.4 KiB
Java
package androidx.recyclerview.widget;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.PointF;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.animation.DecelerateInterpolator;
|
|
import android.view.animation.LinearInterpolator;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
public class LinearSmoothScroller extends RecyclerView.SmoothScroller {
|
|
private static final boolean DEBUG = false;
|
|
private static final float MILLISECONDS_PER_INCH = 25.0f;
|
|
public static final int SNAP_TO_ANY = 0;
|
|
public static final int SNAP_TO_END = 1;
|
|
public static final int SNAP_TO_START = -1;
|
|
private static final float TARGET_SEEK_EXTRA_SCROLL_RATIO = 1.2f;
|
|
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
|
|
public final DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator();
|
|
private final DisplayMetrics mDisplayMetrics;
|
|
private boolean mHasCalculatedMillisPerPixel = false;
|
|
public int mInterimTargetDx = 0;
|
|
public int mInterimTargetDy = 0;
|
|
public final LinearInterpolator mLinearInterpolator = new LinearInterpolator();
|
|
private float mMillisPerPixel;
|
|
public PointF mTargetVector;
|
|
|
|
public LinearSmoothScroller(Context context) {
|
|
this.mDisplayMetrics = context.getResources().getDisplayMetrics();
|
|
}
|
|
|
|
private int clampApplyScroll(int i, int i2) {
|
|
int i3 = i - i2;
|
|
if (i * i3 <= 0) {
|
|
return 0;
|
|
}
|
|
return i3;
|
|
}
|
|
|
|
private float getSpeedPerPixel() {
|
|
if (!this.mHasCalculatedMillisPerPixel) {
|
|
this.mMillisPerPixel = calculateSpeedPerPixel(this.mDisplayMetrics);
|
|
this.mHasCalculatedMillisPerPixel = true;
|
|
}
|
|
return this.mMillisPerPixel;
|
|
}
|
|
|
|
public int calculateDtToFit(int i, int i2, int i3, int i4, int i5) {
|
|
if (i5 == -1) {
|
|
return i3 - i;
|
|
}
|
|
if (i5 == 0) {
|
|
int i6 = i3 - i;
|
|
if (i6 > 0) {
|
|
return i6;
|
|
}
|
|
int i7 = i4 - i2;
|
|
if (i7 < 0) {
|
|
return i7;
|
|
}
|
|
return 0;
|
|
} else if (i5 == 1) {
|
|
return i4 - i2;
|
|
} else {
|
|
throw new IllegalArgumentException("snap preference should be one of the constants defined in SmoothScroller, starting with SNAP_");
|
|
}
|
|
}
|
|
|
|
public int calculateDxToMakeVisible(View view, int i) {
|
|
RecyclerView.LayoutManager layoutManager = getLayoutManager();
|
|
if (layoutManager == null || !layoutManager.canScrollHorizontally()) {
|
|
return 0;
|
|
}
|
|
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
|
|
return calculateDtToFit(layoutManager.getDecoratedLeft(view) - ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin, layoutManager.getDecoratedRight(view) + ((ViewGroup.MarginLayoutParams) layoutParams).rightMargin, layoutManager.getPaddingLeft(), layoutManager.getWidth() - layoutManager.getPaddingRight(), i);
|
|
}
|
|
|
|
public int calculateDyToMakeVisible(View view, int i) {
|
|
RecyclerView.LayoutManager layoutManager = getLayoutManager();
|
|
if (layoutManager == null || !layoutManager.canScrollVertically()) {
|
|
return 0;
|
|
}
|
|
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
|
|
return calculateDtToFit(layoutManager.getDecoratedTop(view) - ((ViewGroup.MarginLayoutParams) layoutParams).topMargin, layoutManager.getDecoratedBottom(view) + ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin, layoutManager.getPaddingTop(), layoutManager.getHeight() - layoutManager.getPaddingBottom(), i);
|
|
}
|
|
|
|
public float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
|
|
return MILLISECONDS_PER_INCH / ((float) displayMetrics.densityDpi);
|
|
}
|
|
|
|
public int calculateTimeForDeceleration(int i) {
|
|
return (int) Math.ceil(((double) calculateTimeForScrolling(i)) / 0.3356d);
|
|
}
|
|
|
|
public int calculateTimeForScrolling(int i) {
|
|
return (int) Math.ceil((double) (((float) Math.abs(i)) * getSpeedPerPixel()));
|
|
}
|
|
|
|
public int getHorizontalSnapPreference() {
|
|
PointF pointF = this.mTargetVector;
|
|
if (pointF != null) {
|
|
float f = pointF.x;
|
|
if (f != 0.0f) {
|
|
return f > 0.0f ? 1 : -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public int getVerticalSnapPreference() {
|
|
PointF pointF = this.mTargetVector;
|
|
if (pointF != null) {
|
|
float f = pointF.y;
|
|
if (f != 0.0f) {
|
|
return f > 0.0f ? 1 : -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.SmoothScroller
|
|
public void onSeekTargetStep(int i, int i2, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
|
|
if (getChildCount() == 0) {
|
|
stop();
|
|
return;
|
|
}
|
|
this.mInterimTargetDx = clampApplyScroll(this.mInterimTargetDx, i);
|
|
int clampApplyScroll = clampApplyScroll(this.mInterimTargetDy, i2);
|
|
this.mInterimTargetDy = clampApplyScroll;
|
|
if (this.mInterimTargetDx == 0 && clampApplyScroll == 0) {
|
|
updateActionForInterimTarget(action);
|
|
}
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.SmoothScroller
|
|
public void onStart() {
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.SmoothScroller
|
|
public void onStop() {
|
|
this.mInterimTargetDy = 0;
|
|
this.mInterimTargetDx = 0;
|
|
this.mTargetVector = null;
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.SmoothScroller
|
|
public void onTargetFound(View view, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
|
|
int calculateDxToMakeVisible = calculateDxToMakeVisible(view, getHorizontalSnapPreference());
|
|
int calculateDyToMakeVisible = calculateDyToMakeVisible(view, getVerticalSnapPreference());
|
|
int calculateTimeForDeceleration = calculateTimeForDeceleration((int) Math.sqrt((double) ((calculateDyToMakeVisible * calculateDyToMakeVisible) + (calculateDxToMakeVisible * calculateDxToMakeVisible))));
|
|
if (calculateTimeForDeceleration > 0) {
|
|
action.update(-calculateDxToMakeVisible, -calculateDyToMakeVisible, calculateTimeForDeceleration, this.mDecelerateInterpolator);
|
|
}
|
|
}
|
|
|
|
public void updateActionForInterimTarget(RecyclerView.SmoothScroller.Action action) {
|
|
PointF computeScrollVectorForPosition = computeScrollVectorForPosition(getTargetPosition());
|
|
if (computeScrollVectorForPosition == null || (computeScrollVectorForPosition.x == 0.0f && computeScrollVectorForPosition.y == 0.0f)) {
|
|
action.jumpTo(getTargetPosition());
|
|
stop();
|
|
return;
|
|
}
|
|
normalize(computeScrollVectorForPosition);
|
|
this.mTargetVector = computeScrollVectorForPosition;
|
|
this.mInterimTargetDx = (int) (computeScrollVectorForPosition.x * 10000.0f);
|
|
this.mInterimTargetDy = (int) (computeScrollVectorForPosition.y * 10000.0f);
|
|
action.update((int) (((float) this.mInterimTargetDx) * TARGET_SEEK_EXTRA_SCROLL_RATIO), (int) (((float) this.mInterimTargetDy) * TARGET_SEEK_EXTRA_SCROLL_RATIO), (int) (((float) calculateTimeForScrolling(TARGET_SEEK_SCROLL_DISTANCE_PX)) * TARGET_SEEK_EXTRA_SCROLL_RATIO), this.mLinearInterpolator);
|
|
}
|
|
}
|