Stopping animations in Android Studio
I am trying to make an animation in android studio that will start when I double click the screen. Since I am using a scroll view and that takes up the whole screen I used that as the view to detect the double tap.
I wanted 2 things to happen: 1. When ever I double tap the screen, the animation should start and stop. And 2. When I play with the volume rockers it will speed up the animation, even if the animation already started.
So far here is my code for the double tap method:
private void doubleTapToScroll() { findViewById(R.id.minchaScroll).setOnTouchListener(new View.OnTouchListener() { private GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDoubleTap(MotionEvent e) {
mScrollView.post(new Runnable() {
@Override
public void run() {
// while(mScrollView.getScrollY() <= findViewById(R.id.alenuPart3).getBottom()){
//
// audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
//
//
// ObjectAnimator.ofInt(mScrollView, "scrollY", (findViewById(R.id.minchaLinearLayout)).getBottom()).setDuration(currentVolume*100000).start();
//
// }
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
ObjectAnimator animator = ObjectAnimator.ofInt(mScrollView, "scrollY", (findViewById(R.id.minchaLinearLayout)).getBottom()).setDuration(currentVolume * 10000);
autoScrollOn = !autoScrollOn;
if (animator.isRunning()) {
animator.start();
} else {
animator.cancel();
}
Log.d("In Run Method", String.valueOf(autoScrollOn));
}
});
return super.onDoubleTap(e);
}
});
I am working on part 1 for now, for some reason the animation will not pause.
Also for part 2, I already take in the volume level and if I change it before the animation starts it works at different speeds, I am in the process of trying to make the speed change while the animation is on with a while loop and restarting the animation, but I don't think that would be the ideal way, any ideas?
Thanks!
Update: I tried, .pause, .end and mScrollView.clearAnimation(); (not sure if that is even a good thought)
I figured it out.
It was because in the line of :
ObjectAnimator animator = ObjectAnimator.ofInt(mScrollView, "scrollY", (findViewById(R.id.minchaLinearLayout)).getBottom()).setDuration(currentVolume * 10000);
I was creating a new animator object
every single double click, so when I went to pause the animator on the second double click. I was creating a new animator object and pauseing that one.
I fixed it by making a boolean
of firstDoubleClick
and then making it false
right there after. The next time around, it didn't make a new object
rather paused
the old one.
上一篇: 如何检测视图上的doubletap?
下一篇: 停止Android Studio中的动画