Skip to content

Commit

Permalink
scroll with animator (makes testing easier)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbachelder committed May 17, 2015
1 parent 73cdd7a commit 29bc482
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.devmarvel.creditcardentry.internal;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
Expand All @@ -23,6 +26,7 @@
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
Expand Down Expand Up @@ -97,7 +101,6 @@ public CreditCardEntry(Context context, boolean includeExp, boolean includeSecur
setLayoutParams(params);
this.setHorizontalScrollBarEnabled(false);
this.setOnTouchListener(this);
this.setSmoothScrollingEnabled(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
container = new LinearLayout(context);
Expand Down Expand Up @@ -281,44 +284,14 @@ public void focusOnField(final CreditEntryFieldBase field) {
}

if (!scrolling) {
View childAt = getChildAt(0);
int childWidth = childAt == null ? 0 : childAt.getMeasuredWidth();
if (field instanceof CreditCardText) {
scrolling = true;
new CountDownTimer(300, 16) {
public void onTick(long millisUntilFinished) {
scrollTo((int) (millisUntilFinished), 0);
}

public void onFinish() {
scrollTo(0, 0);
field.requestFocus();
scrolling = false;
}
}.start();
} else if (getScrollX() + getWidth() < childWidth) {
scrolling = true;
// if we're not already scrolled all the way right
final int target = field.getLeft();
final int duration = 400;
new CountDownTimer(duration, 16) {
final int startingPoint = getScrollX();

public void onTick(long millisUntilFinished) {
long increment = target * (duration - millisUntilFinished) / duration;
long scrollTo = startingPoint + increment;
scrollTo((int) scrollTo, 0);
}

public void onFinish() {
scrollTo(target, 0);
field.requestFocus();
scrolling = false;
}
}.start();
} else {
field.requestFocus();
}
scrolling = true;
scrollToTarget(field instanceof CreditCardText ? 0 : field.getLeft(), new Runnable() {
@Override
public void run() {
scrolling = false;
field.requestFocus();
}
});
}

if (field instanceof SecurityCodeText) {
Expand All @@ -329,6 +302,36 @@ public void onFinish() {
}
}

private void scrollToTarget(int target, final Runnable after) {
int scrollX = getScrollX();
if(scrollX == target) {
if (after != null) after.run();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
ValueAnimator realSmoothScrollAnimation = ValueAnimator.ofInt(scrollX, target).setDuration(500);
realSmoothScrollAnimation.setInterpolator(new DecelerateInterpolator());
realSmoothScrollAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public void onAnimationUpdate(ValueAnimator animation) {
scrollTo((Integer) animation.getAnimatedValue(), 0);
}
});

realSmoothScrollAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (after != null) after.run();
}
});
realSmoothScrollAnimation.start();
} else {
smoothScrollTo(target, 0);
if (after != null) after.run();
}
}
}

@Override
public void focusOnPreviousField(CreditEntryFieldBase field) {
CreditEntryFieldBase view = prevFocusField.get(field);
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ In code:

# Version History

###5/16/2015
- use an animator to do the scroll

###5/14/2015
- don't call complete callback twice

Expand Down

0 comments on commit 29bc482

Please sign in to comment.