首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObjectAnimator不暂停

ObjectAnimator不暂停
EN

Stack Overflow用户
提问于 2014-09-11 12:53:42
回答 2查看 1.2K关注 0票数 0

我试图将一个addPauseListener添加到我的ObjectAnimator对象中,但这不会暂停-- object.This是我的代码:

代码语言:javascript
复制
public class MyActivity extends Activity {

TextView txt;
ObjectAnimator anim;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    txt = (TextView) findViewById(R.id.scroll_message);

    ObjectAnimator anim = ObjectAnimator.ofFloat(txt,"translationY", -200,500);
    anim.setDuration(30000);
    anim.setRepeatCount(ObjectAnimator.INFINITE);
    anim.setRepeatMode(ObjectAnimator.REVERSE);
    anim.start();
    //anim.addPauseListener(pauseListener);

}

    /*Animator.AnimatorPauseListener pauseListener = new Animator.AnimatorPauseListener() {
        public void onAnimationPause(Animator animation) {
               animation.pause() ;
        }
        public void onAnimationResume(Animator animation) {
                animation.start();
         }
    };*/

    public boolean onTouchEvent(MotionEvent event) {
        int eventAction = event.getAction();

        if (MotionEvent.ACTION_DOWN==eventAction){
            anim.pause();
        }
        if (MotionEvent.ACTION_UP==eventAction){
            anim.resume();
        }
    return true;
    };
 }

使用此方法,我在以下位置得到一个错误:

代码语言:javascript
复制
09-11 10:02:39.431  17385-17385/com.example.rs.myapplication E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.animation.ValueAnimator.pause()' on a null object reference

如何获得所需的操作,以便当设备被触摸时,动画暂停,当未被触摸的动画再次恢复时?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-11 16:14:30

你有2个anim对象。一个是本地的,另一个是全球的。您从不设置全局的,只在oncreate方法中定义本地的。因此出现空对象错误。

更改这一行:

代码语言:javascript
复制
ObjectAnimator anim = ObjectAnimator.ofFloat(txt,"translationY", -200,500);

只是为了这个:

代码语言:javascript
复制
anim = ObjectAnimator.ofFloat(txt,"translationY", -200,500);

另外,在oncreate方法中启动动画通常是不好的。我个人从来没有必要这样做,但我所读到的共识是在onWindowFocusChanged方法中这样做。

票数 1
EN

Stack Overflow用户

发布于 2014-09-11 13:10:25

代码语言:javascript
复制
    anim.setRepeatCount(ObjectAnimator.INFINITE); //so that it's does not pause object.

您可以将计数器设置为像10、20、30这样的动画,也可以按您想要的方式重复该对象。

代码语言:javascript
复制
    anim.setRepeatCount(10); // 10 time repeat object after complete that will stop / pause
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25787981

复制
相关文章

相似问题

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