/** * Callback fired on regular interval. * @param millisUntilFinished The amount of time until finished. */ publicabstractvoidonTick(long millisUntilFinished);
/** * Callback fired when the time is up. */ publicabstractvoidonFinish();
privatestaticfinalint MSG = 1;
// handles counting down private Handler mHandler = new Handler() {
@Override publicvoidhandleMessage(Message msg){
synchronized (CountDownTimer.this) { if (mCancelled) { return; }
if (millisLeft <= 0) { onFinish(); } else { long lastTickStart = SystemClock.elapsedRealtime(); onTick(millisLeft);
// take into account user's onTick taking time to execute long lastTickDuration = SystemClock.elapsedRealtime() - lastTickStart; long delay;
if (millisLeft < mCountdownInterval) { // just delay until done delay = millisLeft - lastTickDuration;
// special case: user's onTick took more than interval to // complete, trigger onFinish without delay if (delay < 0) delay = 0; } else { delay = mCountdownInterval - lastTickDuration;
// special case: user's onTick took more than interval to // complete, skip to next interval while (delay < 0) delay += mCountdownInterval; }
Introduced in Android 3.0 (API level 11), the property animation system lets you animate properties of any object, including ones that are not rendered to the screen. The system is extensible and lets you animate properties of custom types as well.
View Animation is the older system and can only be used for Views. It is relatively easy to setup and offers enough capabilities to meet many application’s needs.