我在Android中实现了老虎机。卷轴是ScrollViews,LinearLayouts作为子项,单个图块作为ImageViews垂直定位在LinearLayout内部。我使用ObjectAnimator滚动到ScrollView的末尾,使用ValueAnimator.RESTART和ValueAnimator.INFINITE。卷轴滚动很好而且流畅,但是当它重复时,它会暂停一秒钟,这会破坏效果。以下是我使用的代码:
public void spinReel(SlotReel reel) {
final int reelSize = this.context.getPixelsInDPs(64);
final SlotMachineView me = this;
final SlotReel myReel = reel;
ImageView[] tiles = reel.getTiles();
int delta = tiles.length - reel.currentTileNumber();
ObjectAnimator animator = ObjectAnimator.ofInt(reel, "scrollY", tiles.length * reelSize );
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(delta * 75);
animator.setStartDelay(0);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
myReel.scrollTo(0, reelSize * 3);
}
});
animator.start();
}
请注意,无论是否将scrollTo置于Repeat回调中,都会出现急动。
视频示例here:
答案 0 :(得分:0)