Android TranslateAnimation 动画
我有一个由一些按钮混合而成的复合视图,它通过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);
}
}
有什么想法吗?
I have a compound view mixed by some buttons on it that is attached on top-right corner of screen with RelativeLayout. I want this view to animate to right when I click on "open-close" button on it and stays there until user selects/clicks one of its buttons or clicks again on "open-close" button and then it should animate to right and become invisible. The problem is that it animates to left and then it moves back to its original place! What should I do to solve this problem?
Code:
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);
}
}
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,动画将对象重置为初始状态。您需要将
fillAfter
指定为true
:By default, the animation resets the object to the initial state. You need to specify
fillAfter
totrue
:为了使按钮在翻译后起作用,您还需要将其物理移动到新位置。您可以通过以编程方式更改布局位置来完成此操作。
设置动画监听器和动画监听器在 onAnimationEnd 中执行此操作 -
要了解有关 honeycomb API 之前存在的动画约束的更多信息,请阅读此内容 - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html
In order to make the button work after translation you need to shift it physically as well to the new position. You can do this by changing the layout position programatically.
Set an animation listener & Inside onAnimationEnd do this -
To know more about the animation constraints that exists prior to honeycomb API read this - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html