2012-01-23 5 views
0

저는 컴파운드 뷰가 화면의 오른쪽 상단 구석에 RelativeLayout으로 연결된 버튼이 섞여 있습니다. 이 뷰는 "닫힌 상태"버튼을 클릭 할 때 오른쪽으로 움직이기를 원하고 사용자가 해당 버튼 중 하나를 선택/클릭하거나 "닫은 상태"버튼을 다시 클릭 할 때까지 그대로 남아서 오른쪽으로 움직여야합니다. 보이지 않게된다. 문제는 왼쪽으로 움직 인 다음 다시 원래 위치로 이동한다는 것입니다. 이 문제를 해결하려면 어떻게해야합니까?Android TranslateAnimation animation

코드 :

public class AlarmCommandComponent extends LinearLayout { 

    ImageButton carOffButton; 
    ImageButton carOnButton; 
    ImageButton armButton; 
    ImageButton disArmButton; 
    ImageButton openCloseButton; 
    LayoutAnimationController controller; 
    Animation animation; 

    public AlarmCommandComponent(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.car_alarm_view, this); 

     openCloseButton = (ImageButton) findViewById(R.id.alarmOpenCloseButton); 

     AnimationSet set = new AnimationSet(true); 
     animation = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, //fromXType 
       0.0f,      //fromXValue 
       Animation.RELATIVE_TO_SELF, //toXType 
       -1.0f,      //toXValue 
       Animation.RELATIVE_TO_SELF, //fromYType 
       0.0f,      //fromYValue 
       Animation.RELATIVE_TO_SELF, //toYType 
       0.0f);      //toYValue 
     animation.setDuration(500); 
     set.addAnimation(animation); 
     LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f); 
     this.setLayoutAnimation(controller); 
     this.setPadding(0, 7, 7, 10); 
     this.setBackgroundColor(Color.BLACK); 

     openCloseButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       openCloseButton_onClick(v); 
      } 
     }); 
    } 

    public void openCloseButton_onClick(View v) { 
     this.startAnimation(animation); 
    } 

} 

어떤 생각?

답변

2

기본적으로 애니메이션은 개체를 초기 상태로 다시 설정합니다. 당신은 지정해야 fillAftertrue에 : 새 위치로 물리적으로뿐만 아니라 그것을 이동 할 필요가 번역 한 후 버튼을 작동하게하기 위해

animation.setFillAfter(true); 
+0

감사합니다 -

@Override public void onAnimationEnd(Animation animation) { yourLayout.layout(newLeft, newTop, newRight, newBottom); } 

이 API이 글을 읽을 벌집 이전에 존재하는 애니메이션 제약 조건에 대한 자세한 내용을 알고 -

은 내부 onAnimationEnd 이렇게 애니메이션 리스너 &를 설정! 작동하지만 내 "닫기 버튼"이이 상황에서 클릭에 응답하지 않는 것 같습니다. 내가 맞습니까? –

+0

프리 허니 콤 애니메이션 프레임 워크는 레이아웃의 시각적 부분 만 이동합니다. 물리적 위치는 동일하게 유지되므로 직접 이동해야합니다. Honeycomb은 Animator 클래스를 소개했습니다. 특히 PropertyAnimator는 객체를 움직이게하고 물리적으로 움직입니다. – DeeV

+0

Android 2.1에서 작업 중이므로 실제 객체를 애니메이션으로 이동하려면 어떻게해야합니까? –

1

. 프로그래밍 방식으로 레이아웃 위치를 변경하여이 작업을 수행 할 수 있습니다. http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

+0

나는 서랍을 슬라이딩하고 페이드 인 및 아웃 작업을했습니다! 프로그램의 레이아웃 작업은 중형 및 대규모 애플 리케이션에서 좋은 방법이 아니라고 생각합니다. –

+0

하지만 슬라이딩 서랍에는 아래에서 위로 및 오른쪽에서 왼쪽으로 토글 할 수있는 제약이 있습니다. 서랍이 왼쪽에서 오른쪽으로 또는 위에서 아래로 열려야하는 경우에는 작동하지 않습니다. 어쨌든 행운을 빌어 요! – alchemist