我有一个复合视图混合了一些按钮,它与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);
}
}
任何想法?
谢谢!它的作品,但似乎我的“开闭”按钮在这种情况下不响应点击?我对吗? –
Pre-Honeycomb动画框架仅移动布局的视觉部分。物理位置保持不变,您必须自行移动它。 Honeycomb引入了Animator类,更具体地说是PropertyAnimator,它将动画和物理移动对象。 – DeeV
我正在使用Android 2.1,因此我应该如何使用动画移动实际对象,而不仅仅是快照? –