使用属性动画简单使用示例如下
ObjectAnimator animatorSX = ObjectAnimator.ofFloat(mTakePhoto,View.SCALE_X,1.0f,0.625f);
ObjectAnimator animatorSY = ObjectAnimator.ofFloat(mTakePhoto,View.SCALE_Y,1.0f,0.625f);
ObjectAnimator animatorTY = ObjectAnimator.ofFloat(mTakePhoto,View.TRANSLATION_Y,0,getResources().getDimensionPixelOffset(R.dimen.x50));
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(250);
animatorSet.setStartDelay(50);
animatorSet.playTogether(animatorSX,animatorSY,animatorTY);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation, boolean isReverse) {
mTvImport.setVisibility(visible);
mEffectView.setVisibility(visible);
mTvEffect.setVisibility(visible);
mBeautyView.setVisibility(visible);
if(animatorListenerAdapter != null){
animatorListenerAdapter.onAnimationStart(animation);
}
}
@Override
public void onAnimationEnd(Animator animation) {
mTakePhoto.setVisibility(visible);
if(animatorListenerAdapter != null){
animatorListenerAdapter.onAnimationEnd(animation);
}
}
});
animatorSet.start();
粗略一看没有任何问题,而且在大部分手机上也是正常的的,但是有一天一个测试拿着Redmi 3S跑过来说这个地方没有动画而且控件也没有隐藏好大的bug,没多想我觉得应该没问题啊,怎么会出现这种情况大部分手机都是好的这个手机怎么回事,后来经过调试发现原来是回调方法重写错了
AnimatorListenerAdapter有很多回调方法而且他也有start和end的回调,但是他同时实现了Animator.AnimatorListener接口这个接口也雨两个回调方法
问题就出在这两个方法的重写上,这里我们重新AnimatorListenerAdapter本身的OnAnimationStart(Animator animation)就可以了
@Override
public void onAnimationStart(Animator animation, boolean isReverse) {
//没有回调
}
@Override
public void onAnimationStart(Animator animation) {
//有回调
mTvImport.setVisibility(visible);
mEffectView.setVisibility(visible);
mTvEffect.setVisibility(visible);
mBeautyView.setVisibility(visible);
if(animatorListenerAdapter != null){
animatorListenerAdapter.onAnimationStart(animation);
}
}