我的MainActivity中有以下方法:
public void animationLogoFromCenterToTop(){
final TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_PARENT, CENTER_SCREEN_LOGO,
Animation.RELATIVE_TO_PARENT, 0f);
translate.setDuration(ANIMATION_DURATION);
translate.setFillAfter(true);
translate.setInterpolator(new DecelerateInterpolator() );
translate.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
imageViewMainLogo.invalidate();//Workaround that works
UtilLog.d(TAG, "onAnimationStart");
}
@Override
public void onAnimationRepeat(Animation animation) {
UtilLog.d(TAG, "onAnimationRepeat");
}
@Override
public void onAnimationEnd(Animation animation) {
UtilLog.d(TAG, "onAnimationEnd");
}
});
imageViewMainLogo.startAnimation(translate);
topBar.setVisibility(View.VISIBLE);
}
我在以下片段中调用该方法:
public class SplashFragment extends TrackedFragment{
private static final String TAG = SplashFragment.class.getSimpleName();
private static final int SPLASH_SCREEN_TIMER_IN_MILLISECONDS = 1000;
private Handler timerHandler = new Handler();
private Runnable timerRunnable = new Runnable() {
@Override//This runs in the UI Thread!
public void run() {
((MainActivity) getActivity()).animationLogoFromCenterToTop();
if( UtilStorage.getCurrentLoggedInUser() != null ){
UtilFragments.replaceFragmentStackingNewOneWithoutAddingToStack(getActivity(),
new StatusFragment(),
R.id.fragment_container,
R.anim.fadein_long, 0, 0, 0);
}else{
UtilFragments.replaceFragmentStackingNewOneWithoutAddingToStack(getActivity(),
new MainFragment(),
R.id.fragment_container,
R.anim.fadein_long, 0, 0, 0);
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View wholeFragmentLayout = inflater.inflate(R.layout.fragment_splash, container, false);
return wholeFragmentLayout;
}
@Override
public void onResume(){
super.onResume();
timerHandler.postDelayed(timerRunnable, SPLASH_SCREEN_TIMER_IN_MILLISECONDS);
}
@Override
public void onPause() {
super.onPause();
timerHandler.removeCallbacks(timerRunnable);
}
}
如果我删除 animationLogoFromCenterToTop()方法中的 imageViewMainLogo.invalidate(); 行,ImageView有时会完全消失!如果我进入手机上的“开发人员选项”菜单并选择“显示布局边界”选项,我可以确认视图不在那里,因为它应该是的地方甚至没有布局创建者绘制。
这种行为似乎是随机的。有时会出现ImageView,有时它不会出现,一旦ImageView消失,无论我做什么,它都不会回来(即使我杀了应用程序再次启动它,ImageView再也不会出现(即使动画是调用我可以用 UtilLog.d(TAG,“onAnimationStart”)行确认;
如果我将那个invalidate()解决方法留在那里,那么它总能工作。但是,我不明白为什么会这样。有人可以解释一下我为什么要手动调用imageViewMainLogo.invalidate()吗???
这个错误发生在我在这里的高端(Nexus 4,Galaxy S 3)和低端(LG Vodaphone)设备上,虽然在低端设备中看起来图像消失的速度要快得多。