Android TranslateAnimation animation

荒凉一梦 提交于 2020-01-07 00:34:31

问题


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?


回答1:


By default, the animation resets the object to the initial state. You need to specify fillAfter to true:

animation.setFillAfter(true);



回答2:


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 -

  @Override
  public void onAnimationEnd(Animation animation) {

yourLayout.layout(newLeft, newTop, newRight, newBottom);

  }

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



来源:https://stackoverflow.com/questions/8978036/android-translateanimation-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!