【问题标题】:Android: animation works by code but not by xmlAndroid:动画通过代码而不是 xml 工作
【发布时间】:2017-06-06 17:51:45
【问题描述】:

我有一个非常简单的动画:一个图像视图必须一直上升然后下降。最初我尝试使用 xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="false"
 android:fillAfter="true">
<set
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="-1"
    android:repeatMode="reverse">
    <translate
        android:fromYDelta="0"
        android:toYDelta="10"
        android:duration="300">
    </translate>
</set>

Animation mAnimation= AnimationUtils.loadAnimation(this, R.anim.myanimation);
immagineNemico.startAnimation(mAnimation);

但它不起作用:我的图像视图没有上升,只有下降。所以我阅读了一个问题的答案,并通过代码制作了动画:

    immagineNemico.setVisibility(View.VISIBLE);
    TranslateAnimation mAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0.02f);
    mAnimation.setDuration(300);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mAnimation.setInterpolator(new LinearInterpolator());
    immagineNemico.setAnimation(mAnimation);

它奏效了。但是为什么xml动画不起作用?他们几乎是一样的!哪里出错了?

【问题讨论】:

  • 检查日志如果出现错误就粘贴到这里
  • @EliasFazel 没有错误

标签: java android android-animation android-xml


【解决方案1】:

我遇到过同样的问题:动画在由代码设置但不是在 XML 中设置时有效。简短的回答是,Android 动画中存在错误/故意混淆设计,其中一些 XML 元素被忽略。

这个对相关问题的回答首先让我知道,当以编程方式完成而不是通过 XML 完成时,有些事情会起作用:https://stackoverflow.com/a/6351812/8003750

我知道这不是最令人满意的答案,但简而言之——即使 XML 是完美的,在某些情况下它也不起作用。

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多