布局animation不能在第一次运行

我有一个项目列表的活动,当你点击一个项目,我希望该项目的播放控件从屏幕的底部向上滑动,并成为可见的。 我为幻灯片定义了一个animation集,幻灯片出来,他们的工作。 我已经在我的活动中设置了我的animationListener,并开始在一个项目的animationonClick上的幻灯片。 我的问题是,我第一次运行应用程序,当我点击一个项目时,onClickcallback被执行,但animation不会发生。 第二次点击,animation幻灯片发生,但不是滑出。 第三次和以后,它按预期工作。 这是我的animation集。

vm_slide_in.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <translate android:fromYDelta="800" android:toYDelta="0" android:duration="600" /> </set> 

vm_slide_out.xml

  <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <translate android:fromYDelta="0" android:toYDelta="800" android:duration="600" /> </set> 

这是我的活动布局

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/AppBG"> <RelativeLayout style="@style/LogoBar" android:id="@+id/logo_bar" android:layout_alignParentTop="true"> <include layout="@layout/logobar"></include> </RelativeLayout> <RelativeLayout style="@style/TitleBar" android:id="@+id/title_bar" android:layout_below="@+id/logo_bar"> <include layout="@layout/titlebar"></include>" <Button style="@style/TitleBarButton" android:id="@+id/vm_speaker_btn" android:text="@string/vm_speaker_btn_label" android:layout_alignParentLeft="true" android:layout_margin="4dp"> </Button> <Button style="@style/TitleBarButton" android:id="@+id/vm_edit_btn" android:text="@string/vm_edit_btn_label" android:layout_alignParentRight="true" android:layout_margin="4dp"> </Button> </RelativeLayout> <ListView android:id="@+id/@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/title_bar"/> <RelativeLayout android:id="@+id/vm_control_panel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/footer" android:visibility="gone"> <include layout="@layout/vm_control"/> </RelativeLayout> <RelativeLayout android:id="@+id/footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <include layout="@layout/taskbar"/> </RelativeLayout> </RelativeLayout> 

这是包含的控件的布局

  <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:id="@+id/vm_control" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey"> <TextView android:id="@+id/vm_ctl_branding" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="@string/branding" android:textColor="@color/white"> </TextView> <TextView style="@style/TimeMark" android:id="@+id/vm_timestamp" android:layout_toLeftOf="@+id/vm_progress" android:layout_below="@+id/vm_ctl_branding" android:layout_marginRight="3dp" android:text="0:00"> </TextView> <SeekBar android:id="@+id/vm_progress" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@+id/vm_ctl_branding" android:layout_centerHorizontal="true"> </SeekBar> <TextView style="@style/TimeMark" android:id="@+id/vm_timeleft" android:layout_toRightOf="@+id/vm_progress" android:layout_below="@+id/vm_ctl_branding" android:layout_marginLeft="3dp" android:text="-0:00"> </TextView> <LinearLayout android:id="@+id/vm_action_button_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/vm_timestamp" android:layout_alignRight="@+id/vm_timeleft" android:orientation="horizontal" android:layout_below="@+id/vm_progress"> <TextView style="@style/Vm_Action_Button" android:background="@drawable/vm_action_btn_call" android:id="@+id/vm_callback_btn" android:layout_marginRight="5dp" android:text="@string/vm_action_btn_callback"> </TextView> <TextView style="@style/Vm_Action_Button" android:background="@drawable/vm_action_btn_delete" android:id="@+id/vm_delete_btn" android:layout_marginLeft="5dp" android:text="@string/vm_action_btn_delete"> </TextView> </LinearLayout> </RelativeLayout> </merge> 

从我的onCreate方法…

 // Handle list item selections ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final Voicemail vmItem = (Voicemail) vmla.getItem(position); Toast.makeText(ctx, "Name: " + vmItem.getCallerName() + " Position: " + position, Toast.LENGTH_SHORT) .show(); vVm_controls.startAnimation(mSlideIn); } }); 

和我的animationcallback…

 @Override public void onAnimationStart(Animation animation) { vm_controls_in = !vm_controls_in; if (vm_controls_in) { vVm_controls.setVisibility(View.GONE); } else { vVm_controls.setVisibility(View.VISIBLE); // vVm_controls.bringToFront(); } } @Override public void onAnimationEnd(Animation animation) { if (!vm_controls_in) { try { Thread.sleep(1000); vVm_controls.startAnimation(mSlideOut); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } 

正如你发现的那样,这个问题是可见性。 当视图初始设置为xml文件时,Android将不会呈现布局,直到可见性更改为可见或不可见。 如果您尝试在尚未呈现的视图上设置animation效果,则animation将在没有布局的视图上进行。 animation完成后,它将渲染它,并突然视图将显示没有animation。 它随后工作,因为视图有一个布局,即使设置了。

关键是将可见性设置为不可见,而不是放在xml文件中,以使其呈现出隐藏状态。 当animation第一次出现时,视图就会按预期移动。

发布这个问题当然不是20分钟,我想出了我的问题。 因为我将布局的可见性设置为“GONE”,所以当我尝试启动animation时,它不是第一次触发。 不知道为什么第二次和以后,但如果我开始animation之前,我把可见性设置为“可见”,它解决了这个问题。 这里是我改变它的代码…

 // Handle list item selections ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final Voicemail vmItem = (Voicemail) vmla.getItem(position); Toast.makeText(ctx, "Name: " + vmItem.getCallerName() + " Position: " + position, Toast.LENGTH_SHORT) .show(); vVm_controls.setVisibility(View.VISIBLE); vVm_controls.startAnimation(mSlideIn); } }); 

我不知道它是否仍然是最新的,但是,这是一个知名度的问题。 所以我将xml文件中的可见性设置为不可见,并在视图初始化的地方调用:

 view.post(new Runnable() { @Override public void run() { view.animate().translationY(view.getHeight()); } }; 

现在打电话

 view.animate().translationY(0).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); view.setVisibility(View.VISIBLE); } } 

作品也是第一次。

即使我将视图设置为在animation启动中可见,也有同样的问题。 在开始animation之前,我遵循了brockoli的build议并将其设置为可见,并且像魅力一样工作。

之前:

 Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_left); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { view.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } ); view.startAnimation(animation); 

后:

 Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_left); view.setVisibility(View.VISIBLE); view.startAnimation(animation); 

我有完全相同的问题,但通过使用ViewPropertyAnimator 。 比如你声明一个名为mGridViewGridView 。 所有你现在要做的就是调用mGridView.animate()与你想要的变化。

我的GridView的animation被触发,但由于未知的情况,没有实际的animation可见。 我也将所有View.GONE改为View.VISIBLE ,但是没有改变任何东西。 在我的情况下,我想通了,我只android:requiresFadingEdge="vertical"在这个特定的GridView XML中删除android:requiresFadingEdge="vertical" 。 可能无法使用Fading Edge和ViewPropertyAnimator

我在Android 4.4.3有类似的问题。 我尝试了大部分解决scheme,但只有一个解决scheme适合我。 您必须在窗口完成渲染后运行animation,最好的方法是在onWindowFocusChanged中运行它。 不要在可运行时使用延迟,因为这会影响高速手机的应用程序性能。

 @Override public void onWindowFocusChanged (boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) yourView.startAnimation(anim); } 

更多的信息可以在这篇文章中find: https : //damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/