在我的Android应用程序中,我使用ObjectAnimator来翻译像这样的imageview:
ObjectAnimator transitionX = ObjectAnimator.ofFloat(v, "TranslationY", 190);
是否有像fillAfter这样的选项会在动画完成后将视图的位置重置为新位置?
答案 0 :(得分:4)
不,我会使用属性动画系统,假设v是一个视图:
v.animate().translationY(190).withEndAction(new Runnable() {
@Override
public void run() {
v.setTranslationY(v.getTranslationY()-190);
}
}).start();
答案 1 :(得分:2)
不,没有选择。但是,您可以使用侦听器实现相同的效果,您可以手动将其更改为onAnimationEnd()中的初始位置。以下是缩放imageview的示例:
scaleDown.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//Initial property values
tempIV.setScaleX(1f);
tempIV.setScaleY(1f);
tempIV.setAlpha(1f);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
答案 2 :(得分:0)
我认为处理此问题的正确方法是使用AnimationSet合并两个动画,一个用于翻译,另一个用于翻译视图。
像这样的东西
ObjectAnimator translateTo190= ObjectAnimator.ofFloat(v, "TranslationY", 190);
ObjectAnimator translateBack = ObjectAnimator.ofFloat(v, "TranslationY", 0);
AnimatorSet translate= new AnimatorSet();
translate.play(translateTo190).before(translateBack);
translate.start();
答案 3 :(得分:0)
我在写一张处理动画片的卡片时遇到了这样的问题,卡片从中央“套牌”快速处理。经过多次反复试验后,我的工作方式是以下列方式使用ObjectAnimator。
我在动画源处添加了一张“卡片”卡片,然后是我实际动画的第二张卡片。我使用以下的deal_from_drawpile.xml来为卡片设置动画两部分:第一部分将卡片返回到源头,第二部分缩放并旋转卡片,因为它被“处理”:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:ordering="sequentially">
<set android:interpolator="@android:anim/accelerate_interpolator"
android:ordering="together">
<objectAnimator
android:propertyName="translationX"
android:duration="10"
android:valueFrom="0.0f"
android:valueTo="0.0f"/>
<objectAnimator
android:propertyName="translationY"
android:duration="10"
android:valueFrom="0.0f"
android:valueTo="0.0f"/>
<objectAnimator
android:propertyName="scaleX"
android:duration="10"
android:valueFrom="0.75f"
android:valueTo="0.75f"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="10"
android:valueFrom="0.75f"
android:valueTo="0.75f"/>
</set>
<set android:ordering="together">
<objectAnimator
android:propertyName="rotation"
android:duration="300"
android:valueFrom="0.0f"
android:valueTo="360.0f"/>
<objectAnimator
android:propertyName="scaleX"
android:duration="300"
android:valueFrom="0.75f"
android:valueTo="0.5f"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="300"
android:valueFrom="0.75f"
android:valueTo="0.5f"/>
</set>
然后在代码中我添加实际的平移,这取决于在粗糙的半圆中排列了多少只手。然后将完整的动画序列添加到AnimatorSet中并在结束时触发。代码段;
fullScreenContent.addView(dealtCardView);
for (int iCard=0; iCard<numCards; iCard++) {
for (int iPlayer = 0; iPlayer < numPlayers; iPlayer++) {
dealtCardAnimator = dealtCardAnimator.clone();
dealtCardAnimator.setTarget(dealtCardView);
//offsets where the cards are dealt according to player
ObjectAnimator playerOffsetXAnimator = ObjectAnimator.ofFloat(dealtCardView, "TranslationX", mGame.getPlayer(iPlayer).getPlayerLayout().getTranslationX());
ObjectAnimator playerOffsetYAnimator = ObjectAnimator.ofFloat(dealtCardView, "TranslationY", mGame.getPlayer(iPlayer).getPlayerLayout().getTranslationY());
if (lastDealtCardAnimator == null) dealSet.play(dealtCardAnimator).with(playerOffsetXAnimator).with(playerOffsetYAnimator);
else dealSet.play(dealtCardAnimator).with(playerOffsetXAnimator).with(playerOffsetYAnimator).after(lastDealtCardAnimator);
//The card is returned to the home point with the first portion of deal_from_drawpile
lastDealtCardAnimator = dealtCardAnimator;
}//end for iPlayer
}//end for numCards
dealSet.start();
我现在可以修改XML中每个步骤的持续时间以获得我想要的效果。请注意,您必须在每个元素中嵌入持续时间,而不是在set部分(忽略它的位置)中,并且似乎使用了一些任意默认值。