为什么动画会在计时器上重新开始?

时间:2012-11-26 18:54:12

标签: android animation chronometer

我有一个带动画和计时器视图的课程。这是我的onCreate方法的内容:

    chronometer = ((Chronometer) findViewById(R.id.clock_time));
    chronometer.setOnChronometerTickListener(this);

    chronometer.start();

    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    final SurfaceView surfaceViewTimer = (SurfaceView) findViewById(R.id.surface_view_timer);

    final RelativeLayout timeView = (RelativeLayout) findViewById(R.id.time_view);
    ViewTreeObserver observer = timeView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int animationDuration = getIntent().getIntExtra(
                    MainActivity.TOTAL_TIME, -1);
            TranslateAnimation surfaceGrowingAnimation = new TranslateAnimation(
                    0, 0, timeView.getHeight(), 0);

            surfaceGrowingAnimation.setDuration(animationDuration * 1000);          

            surfaceViewTimer.startAnimation(surfaceGrowingAnimation);

我对计时器有另外一种方法:

@Override
public void onChronometerTick(Chronometer chronometer) {

    int intervalOfVibration = getIntent().getIntExtra(
            MainActivity.VIBRATION_INTERVAL, -1);

    long elapsedMillis = (SystemClock.elapsedRealtime() - ((Chronometer) findViewById(R.id.clock_time))
            .getBase()) / 1000;

    if (((elapsedMillis % intervalOfVibration) == 0) && (elapsedMillis > 0))
        v.vibrate(300);

}

但我的动画会在计时器的每个刻度上重新开始。

我该如何解决这个问题?

现在我收到了这些错误:

11-26 17:58:57.908: W/dalvikvm(15596): threadid=3: thread exiting with uncaught exception (group=0x2aaca228)
11-26 17:58:57.908: E/AndroidRuntime(15596): Uncaught handler: thread main exiting due to uncaught exception
11-26 17:58:57.908: E/AndroidRuntime(15596): java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
11-26 17:58:57.908: E/AndroidRuntime(15596):    at android.view.ViewTreeObserver.checkIsAlive(ViewTreeObserver.java:492)
11-26 17:58:57.908: E/AndroidRuntime(15596):    at android.view.ViewTreeObserver.removeGlobalOnLayoutListener(ViewTreeObserver.java:339)
11-26 17:58:57.908: E/AndroidRuntime(15596):    at com.activities.TimerActivity$1.onGlobalLayout(TimerActivity.java:64)
11-26 17:58:57.908: E/AndroidRuntime(15596):    at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:549)

1 个答案:

答案 0 :(得分:1)

每次布局屏幕时都会调用OnGlobalLayoutListener,其中包括每次Chronometer更改时(因为它更改宽度,可能是高度等等,一切都无效)。

onGlobalLayout()方法中使用removeOnGlobalLayoutListener();

ViewTreeObserver observer = timeView.getViewTreeObserver(); // Make this final
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    @SuppressWarnings("deprecation")
    public void onGlobalLayout() {
        // Your code ...

        if (android.os.Build.VERSION.SDK_INT < 16) // Use old, deprecated method
            timeView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        else // Use new method
            timeView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
}

注意:您必须使用removeGlobalOnLayoutListener()代替API&lt; 16. (方法名称在API级别16中更改。它们以相同的方式执行。)