Android的TranslateAnimation动画 [英] Android TranslateAnimation animation
问题描述
我有一些关于它的按钮,连接与RelativeLayout的屏幕右上角混合复合视图。我想这个观点动画来权当我点击它开 - 关按钮,并在那里停留,直到用户选择/点击的按钮或打开 - 关闭按钮,再点击,然后它应该动画到右,变得不可见。的问题是,它动画至左,然后将其移回到其原始位置!我应该怎么做才能解决这个问题?
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);
}
}
任何想法?
推荐答案
默认情况下,动画重置对象到初始状态。您需要指定 fillAfter
到真正
:
By default, the animation resets the object to the initial state. You need to specify fillAfter
to true
:
animation.setFillAfter(true);
这篇关于Android的TranslateAnimation动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!