Java Android:动画通过代码而不是xml工作
我有一个非常简单的动画:imageview必须一直上下移动。最初我尝试使用xml:Java Android:动画通过代码而不是xml工作,java,android,android-animation,android-xml,Java,Android,Android Animation,Android Xml,我有一个非常简单的动画:imageview必须一直上下移动。最初我尝试使用xml: <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" android:fillAfter="true"> <set android:interpolator="@android:anim/linear_interpolator" and
<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>
但它不起作用:我的imageview没有上升,只有下降。因此,我阅读了一个问题的答案,并通过代码制作了动画:
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动画不起作用呢?他们几乎一样!错误在哪里?我也遇到过同样的问题:动画是通过代码设置的,而不是XML设置的。简单的回答是,Android动画中存在错误/故意混淆的设计,其中一些XML元素被忽略 这是对一个相关问题的回答,它首先提示我,当以编程方式完成时,与通过XML完成时,有些事情是可行的:
我知道这不是最令人满意的答案,但简言之,即使XML是完美的,在某些情况下它也无法工作 如果出现错误,请检查日志并粘贴到此处occurs@EliasFazel没有错误
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);