2010-12-21 52 views
35

我有一个包含项目列表的活动,当您单击某个项目时,我想让该项目的播放控件从屏幕底部向上滑动并变为可见。我为幻灯片定义了一个动画集,并将其滑出,并且它们可以工作。我在我的活动中设置了我的animationListener,并开始使用动画onClick的幻灯片。我的问题是,我第一次运行应用程序,当我点击一个项目,onClick回调被执行,但动画不会发生。第二次点击,动画中的幻灯片发生,但不是滑出。第三次及以后,它按预期工作。这是我的动画集。布局动画在第一次运行时不工作

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); 
    } 
}); 

与我的动画回调...

@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 

} 

回答

98

正如你发现的,问题是可见性。当视图初始设置为xml文件时,Android将不会呈现布局,直到可见性更改为可见或不可见。如果您尝试在尚未呈现的视图上设置动画效果,则动画将在没有布局的视图上进行。动画完成后,它将渲染它,并突然显示视图而不显示动画。随着时间的推移,它会工作,因为该视图即使设置为消失也具有布局。

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

9

当然没有张贴这个问题,20分钟后我想通了,我的问题。由于我将布局的可见性设置为“GONE”,因此当我试图在其上启动动画时,它不是第一次触发。不知道为什么它会在第二次及以后出现,但如果在开始动画之前将可见性设置为“可见”,它就解决了问题。这里是我改变它的代码...

// 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); 
    } 
}); 
+0

你的解决方案是不是“公开范围设置为不可见” – breceivemail 2013-02-24 07:07:31

+0

不,这不是因为你不必担心您的观点被定位成更好用户看不到跳转。如果动画是从视图的位置开始的,但没有将动画带到视图的位置(或其他偏移动画),则无需动画。 – 2013-10-10 16:44:04

0

我有完全相同的问题,但通过使用ViewPropertyAnimator。例如,您声明一个GridView,称为mGridView。你现在所要​​做的就是致电mGridView.animate()与你想要的变化。

我的GridView动画被触发,但由于未知情况,没有实际的动画可见。我也将所有View.GONE更改为View.VISIBLE,但它没有改变任何东西。在我的情况下,我想通了,我只需要删除这个特定GridView的XML中的android:requiresFadingEdge="vertical"。可能无法将衰落边缘与ViewPropertyAnimator结合使用。

3

我不知道它是否仍然是最新的,但是,这是一个与知名度有关的问题。所以我设置的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); 
    } 
} 

作品也首次。

0

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

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

更多的信息可以在这个帖子中找到: https://damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/

1

假如即使我在动画开始的视图设置为可见完全相同的问题。在开始动画之前,我遵循了brockoli的建议并将其设置为可见,并且它像魅力一样起作用。

BEFORE:

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); 

AFTER:

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