安卓布局上简单的翻转动画第二次不起作用。

4


我的 xml 文件如下:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<!-- top -->

  <LinearLayout
    android:id="@+id/top_container"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="60"
    android:background="@drawable/xxx" >

  </LinearLayout>

<!-- bottom -->

  <TableLayout
    android:id="@+id/bottom_container"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="40"
    android:background="@drawable/yyy"
    android:padding="20dip"
    android:stretchColumns="1"
    android:weightSum="2" >

    <TableRow>

     </TableRow>
   </TableLayout>
 </LinearLayout>

动画文件 trans_from_bottom.xml
 <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1000"
    android:fromYDelta="100%"
    android:toYDelta="0%" />

动画文件trans_from_top.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1000"
    android:fromYDelta="-100%"
    android:toYDelta="0%" />

trans_back_to_top.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1000"
    android:fillAfter="true"
    android:fromYDelta="0%"
    android:toYDelta="-100%" />

trans_back_to_bottom.xml

 <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1000"
    android:fillAfter="true"
    android:fromYDelta="0%"
    android:toYDelta="100%" />

以下是简单的类代码:

以下是简单的类代码

    topCont =(LinearLayout)findViewById(R.id.top_container);
    botCont =(TableLayout)findViewById(R.id.bottom_container);

    Animation   slidedown = AnimationUtils.loadAnimation(A.this, R.anim.trans_from_top);
    topCont.setAnimation(slidedown);

    Animation   slideup = AnimationUtils.loadAnimation(A.this, R.anim.trans_from_bottom);
    botCont.setAnimation(slideup);


    topCont.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {

            Animation   slideup = AnimationUtils.loadAnimation(A.this, R.anim.trans_back_to_top);
            topCont.setAnimation(slideup);


            Animation   slidedown = AnimationUtils.loadAnimation(A.this, R.anim.trans_back_to_bottom);
            botCont.setAnimation(slidedown);



        }
    });

我试图给一个水平进出类型的动画,但点击事件动画没有达到预期结果。请建议哪里出了问题?


5
使用 startAnimation(),而不是 setAnimation()。 - Randroid
谢谢 Raghav,我刚才把它搞对了。 - ganesh
1个回答

17

我得到了答案,不要使用setAnimation而是使用startAnimation,所有东西都按照预期正常工作。


4
感谢答案。这让我省去了几个小时的工作,因为事情实际上很简单。 - Mihai Boisteanu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接