You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中实现简易的Timer动画?

Simplest Implementation of a TimerAnimator Similar to ValueAnimator

Hey there! I totally get wanting a lightweight alternative to Android's ValueAnimator for a simple timer-style animation. Let's put together the most straightforward version that mimics the core behavior you showed in your example.

Core Idea

We'll use a Handler and Runnable to repeatedly update a value over a set duration—just like ValueAnimator does, but stripped down to only the essentials. The key is to calculate the progress over time and map it to your desired value range.

Full TimerAnimator Implementation

import android.os.Handler;
import android.os.Looper;

public class TimerAnimator {
    private final int startValue;
    private final int endValue;
    private final long duration;
    private AnimatorUpdateListener updateListener;
    private final Handler handler = new Handler(Looper.getMainLooper());
    private final Runnable updateRunnable = new Runnable() {
        private long startTime;
        private boolean isRunning = false;

        @Override
        public void run() {
            if (!isRunning) {
                startTime = System.currentTimeMillis();
                isRunning = true;
            }

            long elapsedTime = System.currentTimeMillis() - startTime;
            float progress = Math.min((float) elapsedTime / duration, 1.0f);
            int currentValue = startValue + Math.round((endValue - startValue) * progress);

            // Notify listener with current value
            if (updateListener != null) {
                updateListener.onAnimationUpdate(currentValue);
            }

            // Keep running until we reach the end
            if (progress < 1.0f) {
                handler.postDelayed(this, 16); // ~60fps, adjust as needed
            } else {
                isRunning = false;
            }
        }
    };

    // Constructor matching ValueAnimator.ofInt(start, end)
    public TimerAnimator(int startValue, int endValue, long duration) {
        this.startValue = startValue;
        this.endValue = endValue;
        this.duration = duration;
    }

    // Listener interface for updates
    public interface AnimatorUpdateListener {
        void onAnimationUpdate(int currentValue);
    }

    public void addUpdateListener(AnimatorUpdateListener listener) {
        this.updateListener = listener;
    }

    // Start the animation
    public void start() {
        handler.post(updateRunnable);
    }

    // Cleanup to prevent memory leaks
    public void cancel() {
        handler.removeCallbacks(updateRunnable);
    }
}

How to Use It (Just Like Your ValueAnimator Example)

// Create the animator (from 1 to 10 over 20 seconds)
TimerAnimator timeAnimator = new TimerAnimator(1, 10, 20000);

// Add update listener to update your TextView
timeAnimator.addUpdateListener(new TimerAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(int currentValue) {
        tttt.setText(String.valueOf(currentValue));
    }
});

// Start the animation
timeAnimator.start();

// Don't forget to cancel it when you're done (e.g., in onDestroy)
// timeAnimator.cancel();

Key Notes

  • Lightweight: No dependency on the Android animation framework—just basic Handler logic.
  • Smooth Updates: The 16ms delay keeps it close to 60fps, but you can adjust this if you need slower/faster updates.
  • Memory Safety: Always call cancel() when you no longer need the animator (like in onDestroy() of your Activity/Fragment) to avoid lingering Handler callbacks.
  • Value Calculation: We use Math.round() to ensure we get integer values, matching your ofInt use case. For float values, you'd just remove the rounding.

This implementation hits all the core functionality you showed in your ValueAnimator example, but in a much simpler, self-contained package.

内容的提问来源于stack exchange,提问作者user7994394

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:30:53