我有一个复合视图混合了一些按钮,它附在屏幕的右上角,带有RelativeLayout。当我点击它上面的“打开 - 关闭”按钮并且保持在那里直到用户选择/点击其中一个按钮或再次点击“打开 - 关闭”按钮然后它应该动画到右边时,我希望此视图动画到右边。变得无形。问题是它向左移动,然后移回原来的位置!我该怎么做才能解决这个问题?
代码:
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);
}
}
有什么想法吗?
答案 0 :(得分:2)
默认情况下,动画会将对象重置为初始状态。您需要将fillAfter
指定为true
:
animation.setFillAfter(true);
答案 1 :(得分:1)
为了在翻译后使按钮工作,您需要将其物理移动到新位置。您可以通过以编程方式更改布局位置来完成此操作。
设置动画监听器&在onAnimationEnd内执行此操作 -
@Override
public void onAnimationEnd(Animation animation) {
yourLayout.layout(newLeft, newTop, newRight, newBottom);
}
要了解有关蜂窝API之前存在的动画约束的更多信息,请阅读此内容 - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html