2012-01-23 199 views
0

我有一个复合视图混合了一些按钮,它与RelativeLayout附着在屏幕的右上角。当我点击它上面的“打开关闭”按钮并保持在那里,直到用户选择/点击其中一个按钮或再次点击“打开关闭”按钮时,我希望这个视图具有动画效果,然后它应该向右移动,变得无形。问题是,它动画离开,然后它回到原来的地方!我该怎么做才能解决这个问题?Android翻译动画动画

代码:

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

谢谢!它的作品,但似乎我的“开闭”按钮在这种情况下不响应点击?我对吗? –

+0

Pre-Honeycomb动画框架仅移动布局的视觉部分。物理位置保持不变,您必须自行移动它。 Honeycomb引入了Animator类,更具体地说是PropertyAnimator,它将动画和物理移动对象。 – DeeV

+0

我正在使用Android 2.1,因此我应该如何使用动画移动实际对象,而不仅仅是快照? –

1

为了使转换后需要物理以及其转移到新的位置按钮的工作。您可以通过编程方式更改布局位置来完成此操作。

设置动画听众&里面onAnimationEnd做到这一点 -

​​

为了更多地了解之前存在于蜂窝状API阅读本动画的限制 - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

+0

我用滑动抽屉做了这个工作,并且淡入淡出!我认为在程序中使用布局,在大中型应用程序中不是一个好的做法。 –

+0

但滑动抽屉有一个限制,它可以从下到上和从右到左切换。如果您需要抽屉从左至右或从上至下打开,则不起作用。无论如何,祝你好运! – alchemist