Android - 动画问题

时间:2015-10-21 14:34:38

标签: android animation android-animation

我正在尝试将视图从屏幕底部向上滑动到顶部并执行动画,但最后它会回到屏幕底部。我无法弄清楚为什么。下面是代码:

public void slideUp() {

    Animation slide = null;
    slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, -5.0f);

    slide.setDuration(4000);
    SubviewManager.getSelectedRouteView().startAnimation(slide);

    slide.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {

            SubviewManager.getSelectedRouteView().clearAnimation();

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    SubviewManager.getSelectedRouteView().getWidth(), SubviewManager.getSelectedRouteView().getHeight());
            Log.e("Rules", "Rules " + lp.getRules().toString());
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            SubviewManager.getSelectedRouteView().setLayoutParams(lp);

        }

    });
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/selected_route_layout"
        android:padding="10dip"
        android:background="#FFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/time_label"
            android:textColor="#1D1C18"
            android:layout_marginRight="10dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/eta_label"
            android:textColor="#7EC82F"
            android:layout_alignBottom="@+id/time_label"
            android:layout_toRightOf="@+id/time_label"
            android:layout_toEndOf="@+id/time_label" />

        <LinearLayout
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/time_label"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/route_breakdown"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/via_label"
            android:textColor="#BDBDBD"
            android:layout_below="@+id/route_breakdown"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:id="@+id/arrow_up"
            android:padding="15dip"
            android:src="@drawable/transit_symbol_up_arrow"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

继承人xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/selected_route_layout"
        android:padding="10dip"
        android:background="#FFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/time_label"
            android:textColor="#1D1C18"
            android:layout_marginRight="10dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/eta_label"
            android:textColor="#7EC82F"
            android:layout_alignBottom="@+id/time_label"
            android:layout_toRightOf="@+id/time_label"
            android:layout_toEndOf="@+id/time_label" />

        <LinearLayout
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/time_label"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/route_breakdown"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/via_label"
            android:textColor="#BDBDBD"
            android:layout_below="@+id/route_breakdown"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:id="@+id/arrow_up"
            android:padding="15dip"
            android:src="@drawable/transit_symbol_up_arrow"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

可能你正在使用旧工具制作动画,你刚才描述了它的一个缺点。考虑使用Animator并更改视图的TranslationX和TranslationY属性。

可能的一个例子:

fact :: Integer -> IO (Integer, Integer)
fact n = do
  ref <- newIORef (1,1)
  while (lessOrEqualN ref) (step ref)
  (_,acc) <- readIORef ref
  return $ (n, acc)
  where
     lessOrEqualN ref = do
       (i,_) <- readIORef ref
       return $ i <= n
     step ref = do
       (i,acc) <- readIORef ref
       writeIORef ref (i+1, acc * i)

答案 1 :(得分:0)

尝试设置slide.setFillAfter(true)

如果fillAfter为true,则此动画执行的转换将在完成时保持不变。

如果可能的话,可以转移到属性动画(九个镜头用于背部支撑) 那会更容易。