【问题标题】:Animation of View in onCreate() methodonCreate() 方法中的 View 动画
【发布时间】:2016-11-28 00:46:38
【问题描述】:

我想在活动启动时以onCreate() 方法为TextView 设置动画。但是,当我尝试获取视图的高度或宽度时,它始终为 0。在我的情况下,了解视图的高度或宽度很重要,因为我想为 Y 或 X 设置平移。例如,考虑以下代码:

   TextView text=(TextView)(findViewById(R.id.text));
    text.setText(R.string.some_text);
    text.setTranslationY(-text.getHeight());

    ObjectAnimator moveTextAnimator=ObjectAnimator.ofFloat(text,"translationY",-text.getHeight(),0);
    moveTextAnimator.setDuration(1000);
    ObjectAnimator fadeTextAnimator=ObjectAnimator.ofFloat(text,"alpha",0,1);
    fadeTextAnimator.setDuration(2000);


    AnimatorSet textAnimatorSet=new AnimatorSet();
    textAnimatorSet.playSequentially(moveTextAnimator,fadeTextAnimator);
    textAnimatorSet.start();

我想在 onCreate() 方法中运行这段代码。你知道如何解决这个问题吗?我相信解决方案之一就是等到视图被绘制出来。但是,如果我想为布局中的每个视图运行相同的动画怎么办?我需要等待每个视图吗?我该怎么做?

【问题讨论】:

    标签: android animation android-activity oncreate measurement


    【解决方案1】:

    只需使用 text.post(new Runnable(){});

    在runnable中调用text.getHeight()会得到正确的结果;

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多