2014-01-13 25 views
3

我有一個圖像按鈕和關聯的點擊處理程序。Android不會檢測到點擊

當我動畫按鈕(使用翻譯動畫)的按鈕,顯然,改變它的位置在屏幕上。

但有一個問題:當我觸摸初始按鈕位置而不是當前時,Android檢測到點擊。

我該如何讓Android尊重按鈕的實際位置?

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <ImageButton 
     android:id="@+id/addButton" 
     android:src="@android:drawable/ic_input_add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="plusButtonClick"/> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends ActionBarActivity 
{ 
    public void plusButtonClick(View v) 
    { 
     Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show(); 
     animateButton(R.id.addButton, R.anim.anim_move); 
    } 

    private void animateButton(int buttonId, int animationId) 
    { 
     View button = findViewById(buttonId); 
     AnimationSet animationSet = (AnimationSet)AnimationUtils.loadAnimation(this, animationId); 
     animationSet.setAnimationListener(getStartingButtonsListener(button)); 
     button.setVisibility(View.VISIBLE); 
     button.startAnimation(animationSet); 
    } 

    private Animation.AnimationListener getStartingButtonsListener(final View v) 
    { 
     return new Animation.AnimationListener() 
     { 
      @Override 
      public void onAnimationEnd(Animation arg0) 
      { 
       v.setVisibility(View.GONE); 
      } 
     }; 
    } 
} 

anim_move.xml

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="true" 
    android:fillAfter="true" 
    android:duration="1000"> 

    <translate 
     android:fromXDelta="0" 
     android:toXDelta="180" 
     android:fromYDelta="0" 
     android:toYDelta="0"/> 
</set> 

回答

1

翻譯動畫並不實際移動你的對象,它只是移動它的形象。您應該自己在動畫結尾重新定位按鈕,方法是編輯它的LayoutParams