我尝试在用户输入活动时执行动画, 但是因为我从onCreate()调用动画,所以它有点滞后......
已经尝试过onCreate,onStart和onResume。关于何时/如何在进入活动时开始制作动画的想法?
我的代码:
public void onCreate(Bundle savedInstanceState) {
....
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slidedown);
slider.setVisibility(View.VISIBLE);
slider.startAnimation(slide);
....
}
答案 0 :(得分:0)
等到布局完成后再开始动画。在此代码中,view
是您的活动的根视图:
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@TargetApi(16)
@Override
public void onGlobalLayout() {
// Start animation here
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
else
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
答案 1 :(得分:0)
在onWindowFocusChanged()
中启动动画@Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
//start animation here
}