2011-11-21 70 views
1

我使用了一些我找到的用於滑動面板的代碼,基本上它可以工作,但有一個小問題。TranslateAnimation - 動畫僅在第二次後才起作用

動畫在面板第一次打開時不起作用。

這裏是動畫的代碼:

TranslateAnimation anim = null; 


    m_isOpen = !m_isOpen; 

    if (m_isOpen) { 
     setVisibility(View.VISIBLE); 
     anim = new TranslateAnimation(0.0f, 0.0f, getHeight(), 0.0f); 
    } else { 
     anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, getHeight()); 
     anim.setAnimationListener(new Animation.AnimationListener() { 
       public void onAnimationEnd(Animation animation) { 
        setVisibility(View.GONE); 
       } 

       public void onAnimationRepeat(Animation animation) { 
        // not needed 
       } 

       public void onAnimationStart(Animation animation) { 
        // not needed 
       } 
     }); 

    } 

    anim.setDuration(300); 
    anim.setInterpolator(new AccelerateInterpolator(1.0f)); 
    startAnimation(anim); 

爲什麼我第一次打開面板沒有動畫,但所有其他的人存在?

+0

您確定它不起作用嗎?嘗試調試它的一些日誌。當你的面板不可見或者正在創建視圖時,Mabye會啓動嗎? – ania

+0

它不起作用。只有在第一次打開後,動畫才能用於打開和關閉 – piojo

+0

我發現了一種解決方案:不是將面板的可見性設置爲「GONE」,而是在應用程序啓動時關閉面板 – piojo

回答

6

你打電話給這個?它可能只是在「第一次之後」這樣做,因爲面板在第一次調用時尚未渲染,並且getHeight()正在返回0.請嘗試等待,直到getHeight()有值,然後開始動畫。你也可以嘗試對價值進行硬編碼,以測試我的理論是否正確。

+0

您正確。我嘗試了'getMeasuredHeight()',我認爲它會在渲染之前提供高度,但顯然它不是。有沒有比我在上面評論中寫的更好的解決方案? – piojo

+0

我不知道你的上下文(大概這是一個小部件?),如果你正在做一個自定義的視圖,你可以看看[這個問題](http://stackoverflow.com/questions/2864331/how-to -get-an-android-widgets-size-after-layout-is-calculated)建議的替代方案。 – dmon

+0

對此問題的此答案對我的方案無效。當應用程序啓動並運行時調用'getHeight()'。 – piojo

0

我做的,代碼工作的方式:


final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.panel); 
viewGroup.setVisibility(View.VISIBLE); 
final ViewTreeObserver treeObserver = viewGroup.getViewTreeObserver(); 

if (treeObserver.isAlive()) { 
    final OnPreDrawListener l = new OnPreDrawListener() { 
     @Override 
     public boolean onPreDraw() { 
      treeObserver.removeOnPreDrawListener(this); 
      TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, viewGroup.getHeight(), 0.0f); 
      anim.setDuration(1000); 
      anim.setInterpolator(new AccelerateInterpolator(1.0f)); 
      viewGroup.startAnimation(anim); 

      return false; 
      } 
     }; 
    treeObserver.addOnPreDrawListener(l); 
} 

這就是它的精華所在。希望能幫助到你。

0

如果只有一個,就是你想要進行動畫視圖,那麼您可以編寫動畫XML像這樣:

<translate 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_interpolator" 
android:fromYDelta="-100%" 
     (or)  
android:toYDelta="-100%" 
android:duration="300"/> 

這樣,你不需要使用getHeight()。那麼Java部分看起來是這樣的:

final View flashTitle = findViewById(R.id.flash_title); 
    Animation slideDownOnscreen = AnimationUtils.loadAnimation 
      (this, R.anim.slide_down_onscreen); 
    flashTitle.setAnimation(slideDownOnscreen); 
    flashTitle.setVisibility(View.VISIBLE); 

另外,如果你需要結合動畫若干意見,做了以上所述與第一視圖和動畫在post runnable所以後續的觀點,即getHeight()返回正確的高度第一種觀點,像這樣的:

flashTitle.post(new Runnable() { 
     @Override 
     public void run() { 
      System.out.println("NOTE flashTitle height: "+flashTitle.getHeight()); 
      Animation slideDown = new TranslateAnimation(0, 0, 
        -flashTitle.getHeight(),0); 
      slideDown.setDuration(300); 
      flashLine.setVisibility(View.VISIBLE); 
      flashLine.startAnimation(slideDown); 
      //(or)flashLine.animate().translationYBy(flashTitle.getHeight()).setDuration(300); 
     } 
    }); 

您還需要添加accelerate_interpolator作爲Android的默認值是accelerate_decelerate_interpolator

+0

如果您還需要移動當前可見的視圖,則需要在'onCreate()'中計算高度。看看[這個答案](http://stackoverflow.com/a/10134260/1902059)如何。 – willlma

相關問題