Android Translate анимация анимации - PullRequest
0 голосов
/ 24 января 2012

У меня составное представление, смешанное несколькими кнопками, которое прикреплено в верхнем правом углу экрана с помощью 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);
    }

}

Есть идеи?

Ответы [ 2 ]

2 голосов
/ 24 января 2012

По умолчанию анимация сбрасывает объект в исходное состояние. Вам необходимо указать fillAfter до true:

animation.setFillAfter(true);
1 голос
/ 05 июля 2012

Для того, чтобы кнопка заработала после перевода, вам необходимо также физически переместить ее на новую позицию.Вы можете сделать это, изменяя положение макета программно.

Установите прослушиватель анимации & Inside onAnimationEnd и сделайте это -

  @Override
  public void onAnimationEnd(Animation animation) {

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

  }

Чтобы узнать больше об ограничениях анимации, которые существуют до чтения сота APIthis - http://android -developers.blogspot.in / 2011/02 / animation-in-honeycomb.html

...