2012-05-04 35 views
11

我已經完成按鈕翻譯動畫,一切工作後,點擊事件,因爲我除外,但只是問題的動畫後,該按鈕不響應點擊事件,請大家指正按鈕沒有響應翻譯動畫

Button b=(Button)findViewById(R.id.button1); 
    TranslateAnimation slide=new TranslateAnimation(0, 30, 0,-40); 
    slide.setDuration(500); 
    slide.setZAdjustment(TranslateAnimation.ZORDER_TOP); 
    slide.setFillAfter(true); 

    b.startAnimation(slide); 
+0

看起來,你忘記添加聽衆按鈕。 – RobinHood

回答

20

如果您在較低級別的API(HoneyComb下方)處理動畫,則您的動畫實際上不會移動該按鈕,而只會移動其圖像。它的點擊將放置在您將其放置在佈局中的相同位置。

+1

http://android-developers.blogspot.in/2011/02/animation-in-honeycomb。html –

+0

:然後翻譯後如何改變按鈕位置,目前我已經設置了動畫監聽器,並且在動畫結束回調中我已經把layoutparam設置爲它,但是它不會解決問題 – Bytecode

+0

關於動畫完成了你將不得不動態創建一個新按鈕並將其添加到該位置並將按鈕可見性更改爲view.gone。 –

4

動畫不會更改查看實際位置。即使你setfillAfter(true),它可以接受點擊事件的位置是原始位置。

0

嘗試使用此:

b.layout(0, -40, b.getWidth(), b.getHeight()); 
21

這將在Y方向平移對象:

ObjectAnimator mover = ObjectAnimator.ofFloat(v, "translationY", 0, 400); 
mover.start(); 
+0

您還可以使用AnimatorSet在x和y方向上合併翻譯動畫。 –

+1

我不在HoneyComb工作 – Bytecode

+0

你讓我的一天花花公子 –

5

我可以達到你想要的東西,但是你必須手動管理座標。 看看它是否適合你。

public class AnimationTestActivity extends Activity { 

    private Button mButton; 

    private LinearLayout.LayoutParams params; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mButton = (Button) findViewById(R.id.button); 
     mButton.setOnClickListener(mClickListener); 

     params = (LayoutParams) mButton.getLayoutParams(); 
    } 

    private android.view.View.OnClickListener mClickListener = new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 400); 
      animation.setDuration(2000); 
      animation.setAnimationListener(mAnimationListener); 
      v.startAnimation(animation); 
     } 
    }; 

    private AnimationListener mAnimationListener = new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      params.topMargin = params.topMargin + 400; 
      mButton.setLayoutParams(params); 
     } 
    }; 
} 
+0

嗨@Sushil,你的解決方案實際上適用於我,即使對於顯示/隱藏動畫,但我可以看到的最重要的問題是視圖在動畫結束時閃爍在屏幕的另一側,並且它是「持續」在其新的位置。你也觀察過這個嗎? – mdelolmo

5

我遇到了這個問題,我確實在第二次前修復了它。所以,我認爲我應該與你們分享我的解決方案。

  • 在動畫的XML文件,我刪除android:fillAfter="true"保持android:fillEnabled="true"時。

  • 註冊動畫監聽器,然後在onAnimationEnd()方法,我稱之爲View#Layout()改變視圖的位置。

       int newLeft = (int) (layoutContent.getLeft() + layoutContent.getWidth() * 0.8); 
           layoutContent.layout(newLeft, 
                layoutContent.getTop(), 
                newLeft + layoutContent.getMeasuredWidth(), 
                layoutContent.getTop() + layoutContent.getMeasuredHeight()); 
    

在我的情況下,動畫做的是滑動layoutContent到萊夫特賽德寬度的80%。

它工作正常。希望這可以幫助。

@更新:今天,您可以在Android 3.0 +上使用ObjectAnimator。如果您正在爲3.0下的android開發,可以在支持庫v.4中找到它。 ObjectAnimator是動畫的動物。

@ Update#2:您可以在android api更高版本上使用ViewPropertyAnimator。 它提供了更好的性能,並解決了點擊事件的問題。例如:

mButton.animate() 
       .setDuration(TIME) 
       .translationY(VALUE) 
       .start(); 
+1

ObjectAnimator不在支持庫中! – amiekuser

+0

+1巨大的幫助「在動畫xml文件中,當保留android:fillEnabled =」true「時,我刪除了android:fillAfter =」true「」 – Mazino

0

我發現了一個簡單的解決方案定義按鈕最終位置的佈局和一些位置開始播放動畫,也就是指定fromX或弗羅米,而不是把零

+0

這是否意味着不會接受任何答案?你也可以接受你自己的答案。 – Talha