3

I have LayoutParams transition with addRule().

My view changes position but duration is instant.

I work on API 15 so I cant use beginDelayedTransition().

Animation a = new Animation() {

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        final RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(layoutFalse.getWidth(), layoutFalse.getHeight());

        positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        positionRules.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        layoutFalse.requestLayout();
        layoutFalse.setLayoutParams(positionRules);
    }
};

a.setDuration(3000);
layoutFalse.startAnimation(a);
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66

3 Answers3

1

applyTransformation() gets called for each animation frame and the transformation should use the interpolatedTime param to calculate transformations for that frame. Your applyTransformation() applies the changes unconditionally so the result is the same for all frames.

If you just want to change the layout after a specified time, use layoutFalse.postDelayed() to post a Runnable that applies the changes after a delay.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I want for the layout change to last for 3 seconds, not to be be instant – Tino Balint Sep 11 '16 at 11:58
  • Then either compute the intermediate steps in your `applyTransformation()`, or use the [Transitions framework](https://developer.android.com/training/transitions/index.html) – laalto Sep 11 '16 at 12:02
  • How do I do it from `applyTransformation()` thats what I am trying to find out. I have similar transitions on many places in my app and I dont want to switch it to Transition framework. I would just like to slow it down similar to `beginDelayedTransition()` – Tino Balint Sep 11 '16 at 12:20
1

Inside this block you do animate nothing. You just say:

Let this object be in each frame with the contrains I want. No more.

So, I think, If you want to animate something you have to use interpolatedTime like this: To setup position of view inside RelativeLayout without animation:

public void setTopLeftCornerF(float leftMargin, float topMargin) {
    if (this.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.getLayoutParams();
        params.width = (int)_width;//w();
        params.height = (int)_height;//h();
        params.setMargins((int)leftMargin, (int)topMargin, (int)-leftMargin, (int)-topMargin);
        this.setLayoutParams(params);
    }
}

to use animation somewhere:

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    // TODO Auto-generated method stub
    super.applyTransformation(interpolatedTime, t);
    float posX = (float) (mFromX + ((mToX - mFromX) * interpolatedTime));
    float posY = (float) (mFromY + ((mToY - mFromY) * interpolatedTime));

    t.getMatrix().setTranslate(posX,posY);
}

Or use TranslateAnimation as described here

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Here ,

layoutFalse.requestLayout();
layoutFalse.setLayoutParams(positionRules);

Your sequence is wrong. Because you should first set layoutParams and then call requestLayout on your view.

It should be as follows

layoutFalse.setLayoutParams(positionRules);
layoutFalse.requestLayout();
Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • 1
    Doesn't really matter. Layout requests are asynchronous anyway, and `setLayoutParams()` calls `requestLayout()` internally. – laalto Sep 11 '16 at 11:50
  • 1
    @laalto I agree. `requestLayout` is called automatically. It's in the android sources – Peter Chaula Sep 11 '16 at 11:57