Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setLayoutParams() animation duration doesnt work

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);
like image 267
Tino Balint Avatar asked Sep 03 '26 12:09

Tino Balint


2 Answers

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

like image 51
Vyacheslav Avatar answered Sep 05 '26 01:09

Vyacheslav


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.

like image 43
laalto Avatar answered Sep 05 '26 02:09

laalto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!