ue为负数时,moveStepValue*0.5f segment是往左移动,正数往右移动,这样动画效果才不会有bug,
moveAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, moveStepValue * 0.5f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
//下面的代码是计算动画的执行时间,如果不计算就会出现切换分段数量不一致,指示器的动画执行速度太快或太慢。
if (moveStepValue < 1) {
moveStepValue = moveStepValue * -1;
}
moveAnimation.setDuration(moveStepValue * 70);
moveAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
zhishiqi.clearAnimation();
//移动效果实现之后,还需要将移动segment分段的margin设置一次,如果不设置动画又会反回去,为什么这样我觉得应该是和android是使用xhtml布局的原因。
zhishiqilinearParams.setMargins((currentTopItemIndex - 1) * zhishiqiWidth, margin.topMargin, zhishiqiWidth, zhishiqilinearParams.height);
zhishiqi.setLayoutParams(zhishiqilinearParams);
}
});
zhishiqi.startAnimation(moveAnimation);
zhishiqi.setVisibility(View.VISIBLE);
}
}
|