我试图实现的是用户将打开第一页的视图分页,和视图分页将弹跳到第二页的一半,并弹回拳头页面,表明有更多的页面滚动到。在我的代码中,它只显示下半页。它工作得很好。但是弹跳的动画不起作用。
private int animFactor;
private ValueAnimator animator = new ValueAnimator();
AnimatorSet set = new AnimatorSet();
// private Animation animation1= new TranslateAnimation()
private void animateViewPager(final ViewPager pager, final int offset, final int delay) {
if (!animator.isRunning()) {
animator.removeAllUpdateListeners();
animator.removeAllListeners();
//Set animation
animator.setIntValues(0, -offset);
animator.setDuration(delay);
animator.setRepeatCount(1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setInterpolator(new BounceInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = animFactor * (Integer) animation.getAnimatedValue();
if (!pager.isFakeDragging()) {
pager.beginFakeDrag();
}
pager.fakeDragBy(value);
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
animFactor = 1;
}
@Override
public void onAnimationEnd(Animator animation) {
pager.endFakeDrag();
}
@Override
public void onAnimationRepeat(Animator animation) {
animFactor = -1;
}
});
set.play(animator);
set.start();
}
}发布于 2017-01-20 13:39:01
private void animateViewPager(final ViewPager pager, final int offset, final int delay) {
if (!animator.isRunning()) {
animator.removeAllUpdateListeners();
animator.removeAllListeners();
//Set animation
animator.setIntValues(0, -offset);
animator.setDuration(delay);
animator.setRepeatCount(1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setInterpolator(new BounceInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = animFactor * (Integer) animation.getAnimatedValue();
if (!pager.isFakeDragging()) {
pager.beginFakeDrag();
}
pager.fakeDragBy(value);
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
animFactor = 1;
}
@Override
public void onAnimationEnd(Animator animation) {
pager.endFakeDrag();
}
@Override
public void onAnimationRepeat(Animator animation) {
animFactor = -1;
}
});
set.play(animator);
set.start();
}
}并使用以下方式调用此方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
if (articleDetailPager.getCurrentItem() >= 0) {
Context context = ArticleDetailActivity.this;
String filename = "Init";
SharedPreferences stats;
stats = context.getSharedPreferences(filename, 0);
int appOpen = stats.getInt("appOpen", 0);
if (appOpen == 0) {
animateViewPager(articleDetailPager, 20, 300);
appOpen += 1;
SharedPreferences.Editor editor = stats.edit();
editor.putInt("appOpen", appOpen);
editor.commit();
}
}
}
};
handler.postDelayed(r, 1000);
}
}https://stackoverflow.com/questions/41677478
复制相似问题