我试图将一个addPauseListener添加到我的ObjectAnimator对象中,但这不会暂停-- object.This是我的代码:
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;
};
}使用此方法,我在以下位置得到一个错误:
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如何获得所需的操作,以便当设备被触摸时,动画暂停,当未被触摸的动画再次恢复时?
发布于 2014-09-11 16:14:30
你有2个anim对象。一个是本地的,另一个是全球的。您从不设置全局的,只在oncreate方法中定义本地的。因此出现空对象错误。
更改这一行:
ObjectAnimator anim = ObjectAnimator.ofFloat(txt,"translationY", -200,500);只是为了这个:
anim = ObjectAnimator.ofFloat(txt,"translationY", -200,500);另外,在oncreate方法中启动动画通常是不好的。我个人从来没有必要这样做,但我所读到的共识是在onWindowFocusChanged方法中这样做。
发布于 2014-09-11 13:10:25
anim.setRepeatCount(ObjectAnimator.INFINITE); //so that it's does not pause object.您可以将计数器设置为像10、20、30这样的动画,也可以按您想要的方式重复该对象。
anim.setRepeatCount(10); // 10 time repeat object after complete that will stop / pausehttps://stackoverflow.com/questions/25787981
复制相似问题