setLayoutParams()动画持续时间不起作用

3

我使用addRule()进行LayoutParams转换。

我的视图位置发生了改变,但持续时间很短。

我使用的是API 15,所以无法使用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);
3个回答

1

applyTransformation()会在每个动画帧中被调用,变换应该使用interpolatedTime参数来计算该帧的变换。你的applyTransformation()无条件地应用了更改,因此所有帧的结果都相同。

如果您只想在指定时间后更改布局,请使用layoutFalse.postDelayed()来发布一个Runnable,在延迟后应用更改。


我希望布局更改持续3秒钟,而不是瞬间完成。 - Tino Balint
然后,在您的 applyTransformation() 中计算中间步骤,或使用 Transitions framework - laalto
我想知道如何从applyTransformation()实现它。我的应用程序中有许多类似的转换,我不想将其切换到Transition框架。我只想像beginDelayedTransition()一样减慢它的速度。 - Tino Balint

1

在这个代码块中,你不需要进行任何动画。

你只需说:

让这个对象在每一帧中都按照我想要的限制出现。仅此而已。

所以,我认为,如果你想要做动画,你必须像这样使用插值时间:

要设置RelativeLayout内视图的位置而不进行动画:

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);
    }
}

在某处使用动画:
@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);
}

或者使用TranslateAnimation,如此处所述。


0

这里,

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

你的顺序错误了。因为你应该先设置layoutParams,然后再在你的view上调用requestLayout
应该如下:
layoutFalse.setLayoutParams(positionRules);
layoutFalse.requestLayout();

1
无所谓。布局请求本来就是异步的,而 setLayoutParams() 在内部调用了 requestLayout() - laalto
1
@laalto 我同意。requestLayout会自动调用。它在Android源代码中。 - Peter Chaula

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接