【问题标题】:setLayoutParams() animation duration doesnt worksetLayoutParams() 动画持续时间不起作用
【发布时间】:2017-01-19 00:40:32
【问题描述】:

我有 LayoutParamsaddRule() 的转换。

我的视图改变了位置,但持续时间是即时的。

我在 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);

【问题讨论】:

    标签: android animation transition duration layoutparams


    【解决方案1】:

    这里,

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

    你的顺序是错误的。因为你应该先设置layoutParams,然后在你的view上调用requestLayout

    应该是这样的

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

    【讨论】:

    • 没关系。无论如何布局请求都是异步的,setLayoutParams() 在内部调用requestLayout()
    • @laalto 我同意。 requestLayout 被自动调用。它在 android 源代码中
    【解决方案2】:

    applyTransformation() 为每个动画帧调用,并且转换应使用interpolatedTime 参数来计算该帧的转换。您的 applyTransformation() 无条件应用更改,因此所有帧的结果都相同。

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

    【讨论】:

    • 我希望布局更改持续 3 秒,而不是瞬间完成
    • 然后在applyTransformation() 中计算中间步骤,或者使用Transitions framework
    • 我该如何从applyTransformation() 做到这一点,这就是我想要找出的。我的应用程序中的许多地方都有类似的转换,我不想将其切换到转换框架。我只想放慢速度,类似于beginDelayedTransition()
    【解决方案3】:

    在这个块内你什么都不做动画。 你就说:

    让这个对象在每一帧中都有我想要的约束。没有了。

    所以,我认为,如果你想为某些东西制作动画,你必须像这样使用 interpolatedTime: 在没有动画的情况下在 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);
    }
    

    或者按照here的描述使用TranslateAnimation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2018-06-15
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多