首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新设置Animator?

如何重新设置Animator?
EN

Stack Overflow用户
提问于 2012-05-17 14:27:28
回答 2查看 939关注 0票数 2

我使用单个Animator实例来平滑地向下或向右滚动视图。首先,我在自定义View实现中使用公共参数对其进行设置:

代码语言:javascript
复制
private ObjectAnimator animator = new ObjectAnimator();
{
    animator.setTarget(this);
    animator.setDuration(moveDuration);
}

然后我使用两种方法向下和向右移动

代码语言:javascript
复制
public void moveRight() {

    if( animator.isRunning() ) animator.end();

    animator.setPropertyName("scrollX");
    animator.setIntValues(getScrollX(), getScrollX() + cellWidth);
    animator.start();
}

public void moveDown() {

    if( animator.isRunning() ) animator.end();

    animator.setPropertyName("scrollY");
    animator.setIntValues(getScrollY(), getScrollY() + cellHeight);
    animator.start();
}

我发现,只有第一次调用的方法才能正确工作。另一个错误,就像第一次运行的方法在动画对象中留下了一些痕迹。

如何删除这些痕迹和重新编程动画从右向下滚动,反之亦然?

EN

回答 2

Stack Overflow用户

发布于 2012-12-10 18:34:21

我也有这个问题。由于某些原因,第二次使用setPropertyName()似乎并不能清除之前的属性和/或值。

相反,使用setProperty()可以解决这个问题,例如:

代码语言:javascript
复制
Property<View, Float> mTranslationXProperty = Property.of(View.class, 
   Float.class, "translationX");
Property<View, Float> mScaleXProperty = Property.of(View.class, 
   Float.class, "scaleX");
...
animator.setProperty(mTranslationXProperty);
animator.setFloatValues(500f, 0f); 
animator.start();
...
animator.setProperty(mScaleXProperty);
animator.setFloatValues(1f, 0f); 
animator.start();
票数 1
EN

Stack Overflow用户

发布于 2013-07-25 17:15:48

我可以使用下面的代码来重置值:

代码语言:javascript
复制
final AnimatorSet animate = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_move);
List<Animator> animations = animate.getChildAnimations();
for (int i = 0; i < animations.size(); i++) {
    ObjectAnimator animator = (ObjectAnimator) animations.get(i);
    if (animator.getPropertyName().contentEquals("y")) {
        animator.setFloatValues(0f, 500f);
    }
}
animate.setTarget(starlightImageView);
animate.start();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10630883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档