2014-01-13 2 views
3

이미지 버튼과 관련 클릭 핸들러가 있습니다.안드로이드가 클릭을 감지했습니다.

버튼을 움직이면 (애니메이션 번역 사용) 버튼이 분명히 화면의 위치를 ​​변경합니다.

하지만 문제가 있습니다. 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

0

버튼에 onTouchEvent를 사용합니다. 애니메이션을 끝낼 때 버튼의 위치를 ​​변경해야합니다. 또한 자바 파일을 통해이 onTouch 구현을 제공하지만 XML 레이아웃 파일은 제공하지 않습니다. 이것은 onClick 이벤트를 사용할 때보 다 훨씬 낫습니다. 보기 : Triggering event when Button is pressed down in Android

관련 문제