为什么翻译动画不起作用?

时间:2014-10-10 03:49:06

标签: android animation

我有一个活动,其contentView布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/schedule_item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/item_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<LinearLayout
    android:id="@+id/bottom_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone" >
<!-- some buttons  -->
</LinearLayout>

</LinearLayout>

我想显示并隐藏底部线性布局,以使其像底部菜单一样,我创建一个这样的函数:

 private void showBottomMenuView()
{
    if (mBottomMenuView.getVisibility() != View.VISIBLE) {
        Animation slideinAnimation = new TranslateAnimation(0.0f, 0.0f, 1.0f, 0.0f);
        slideinAnimation.setDuration(300);
        mBottomMenuView.startAnimation(slideinAnimation);
        mBottomMenuView.setVisibility(View.VISIBLE);
    }
}

但是当我想显示底部布局(mBottomMenuView)时,它不起作用。视图不会从底部顺利滑入,只是突然出现

但是当我从xml加载动画时,它确实有效。

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromYDelta="100%"
    android:toYDelta="0%"
    android:duration="300"/>
</set>


private void showBottomMenuView()
{
    if (mBottomMenuView.getVisibility() != View.VISIBLE) {
        Animation slideinAnimation =  AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in);
        mBottomMenuView.startAnimation(slideinAnimation);
        mBottomMenuView.setVisibility(View.VISIBLE);
    }
}

为什么以前的新动画不起作用?

1 个答案:

答案 0 :(得分:3)

您想测量屏幕高度,然后使用

Animation slideinAnimation = new TranslateAnimation(0.0f, 0.0f, mesureHeight, 0.0f);

Animation slideinAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);