2015-10-21 82 views
0

我試圖從屏幕底部向上滑動一個視圖到頂部,並且它在做動畫,但最終它會回到屏幕底部。我無法弄清楚爲什麼。繼承人的代碼:Android - 動畫問題

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> 

回答

1

也許您正在使用舊的動畫工具和你剛纔介紹了其缺點之一。考慮使用Animator並更改視圖的TranslationX和TranslationY屬性。

一個可能的例子:

yourView.animate().translationY(newY).setDuration(duration).withEndAction(new Runnable() { 
    @Override 
    public void run() { 
     // your onAnimationEnd actions 
    } 
}).start(); 
+0

謝謝你的工作! – Mikerizzo

+0

@Mikerizzo不客氣 –

0

嘗試設置slide.setFillAfter(真)

如果fillAfter是真實的,那這個動畫執行它完成時將持續轉型。

或者如果可能的話,移動到屬性動畫(backold支持nineoldandroids) 然後它會容易得多。

+0

我試過了,沒有發生任何事情。仍然做了同樣的事情。使用財產轉讓是什麼意思?使用anim xml文件? – Mikerizzo

+0

你使用的是舊的視圖動畫實現。從SDK 13+開始,一個動畫「平臺」使用Animator類調用屬性動畫,並實際爲對象的屬性製作動畫。在您的情況下爲translationY和translationY屬性。在這裏看看更多的信息。 http://developer.android.com/guide/topics/graphics/prop-animation.htmloper.android.com/guide/topics/graphics/prop-animation.html – TalMihr