2015-02-09 123 views
0

我在我的應用程序中使用一些圖像動畫我的相對佈局。當我編寫代碼在按鈕單擊時爲佈局設置動畫效果時,它首次顯示的不是動畫。然後在同一個按鈕上單擊我滑動佈局。之後,如果我點擊按鈕滑動動畫工作正常。其實我必須在onCreate本身而不是按鈕點擊下滑動下來的動畫。以下是我的代碼。誰能告訴我爲什麼不顯示動畫?查看下滑動畫不起作用

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.fadeview); 
     iconLayout=(RelativeLayout)findViewById(R.id.icon_ayout); 
     iconLayout.postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 

       iconLayout.setVisibility(View.VISIBLE); 
       SlideToDown(); 
      } 
     }, 500); 
    } 

public void SlideToAbove() { 
     Animation slide = null; 

     slide=new TranslateAnimation(0.0f, 0.0f, 0.0f,-iconLayout.getHeight()); 
     slide.setDuration(1000); 
     slide.setFillAfter(true); 
     slide.setFillEnabled(true); 
     iconLayout.startAnimation(slide); 

     slide.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 

       iconLayout.clearAnimation(); 

       RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
         iconLayout.getWidth(), iconLayout.getHeight()); 
       // lp.setMargins(0, 0, 0, 0); 
       //lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
       iconLayout.setLayoutParams(lp); 

      } 

     }); 

    } 
public void SlideToDown() { 
     Animation slide = null; 

     slide=new TranslateAnimation(0.0f, 0.0f, -iconLayout.getHeight(), 0.0f); 

     slide.setDuration(1000); 
     slide.setFillAfter(true); 
     slide.setFillEnabled(true); 
     iconLayout.startAnimation(slide); 

     slide.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 

       iconLayout.clearAnimation(); 

       RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
         iconLayout.getWidth(), iconLayout.getHeight()); 
       lp.setMargins(0,0, 0, 0); 
       lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
       iconLayout.setLayoutParams(lp); 

      } 

     }); 

    } 
+0

你有沒有按鈕點擊的代碼?您是否還嘗試在活動首次創建時對佈局進行動畫設置,然後在單擊按鈕時將其設置爲動畫效果? – 2015-02-09 11:42:43

回答

0

這是因爲你在onCreate()中開始動畫,在這個時候視圖是可用的,但它沒有實際繪製(沒有寬度和高度)。在這裏,iconLayout.getHeight()的值將返回值0.

解決方法是將Layoutlistener設置爲視圖,並且監聽器在繪製時不會看到您。之後,您可以開始動畫。

ViewTreeObserver loadingObserver = iconLayout.getViewTreeObserver(); 
loadingObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     iconLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     SlideToDown(); 
    } 
}); 
+0

試過這個,但沒有工作:( – 2015-02-09 12:06:58