如何在特定时间启动 Android AlphaAnimation?

发布于 2025-01-05 12:01:25 字数 1846 浏览 4 评论 0原文

我有这个图像按钮。我希望它在某个时间开始动画。 我查看了android文档,发现 setStartTime(long startTimeMillis)

这就是我想到的:

private ImageButton imgBtn;

// Other variables and stuff..

//And inside to onCreate void, I have set the button listener.
imgBtn.setOnClickListener(tappClickHandler);
    Button.OnClickListener imgClickHandler = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            new AsyncTaskExample().execute("");

        }
    };
    private class AsyncTaskExample extends AsyncTask<String, Integer, Integer> {
        protected void onPreExecute(){
            AlphaAnimation alDown = new AlphaAnimation(1.0f, 0.1f);
            alDown.setDuration(200);
            alDown.setFillAfter(true);
            imgBtn.startAnimation(alDown);
        }
        @Override
        protected Integer doInBackground(String... params) {
            Date test = new Date();
            return (test.getTime()/1000) + 5;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(Integer result) {
            Date clientTime = new Date();

            AlphaAnimation alUp = new AlphaAnimation(0.1f, 1.0f);
            alUp.setDuration(200);
            alUp.setStartTime(result);
            imgBtn.setAnimation(alUp);
            Log.d(LOG_TAG, "Time to start: " + imgBtn.getAnimation().getStartTime());
            Log.d(LOG_TAG, "Current device time: " + clientTime.getTime()/1000);    
        }

    }

日志打印:

02-13 20:40:42.634: D/tappWin(3504): Time to start: 1329162048

02-13 20:40:42.634: D/tappWin(3504): Current设备时间:1329162042

imgBtn 取得第一个动画,但不是第二个..

I have this ImageButton. I want it to start animating at a certain time.
I looked into android documentation, and found setStartTime(long startTimeMillis).

This is what I came up with:

private ImageButton imgBtn;

// Other variables and stuff..

//And inside to onCreate void, I have set the button listener.
imgBtn.setOnClickListener(tappClickHandler);
    Button.OnClickListener imgClickHandler = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            new AsyncTaskExample().execute("");

        }
    };
    private class AsyncTaskExample extends AsyncTask<String, Integer, Integer> {
        protected void onPreExecute(){
            AlphaAnimation alDown = new AlphaAnimation(1.0f, 0.1f);
            alDown.setDuration(200);
            alDown.setFillAfter(true);
            imgBtn.startAnimation(alDown);
        }
        @Override
        protected Integer doInBackground(String... params) {
            Date test = new Date();
            return (test.getTime()/1000) + 5;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(Integer result) {
            Date clientTime = new Date();

            AlphaAnimation alUp = new AlphaAnimation(0.1f, 1.0f);
            alUp.setDuration(200);
            alUp.setStartTime(result);
            imgBtn.setAnimation(alUp);
            Log.d(LOG_TAG, "Time to start: " + imgBtn.getAnimation().getStartTime());
            Log.d(LOG_TAG, "Current device time: " + clientTime.getTime()/1000);    
        }

    }

The log prints:

02-13 20:40:42.634: D/tappWin(3504): Time to start: 1329162048

02-13 20:40:42.634: D/tappWin(3504): Current device time: 1329162042

The imgBtn makes the first animation, but not the second..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2025-01-12 12:01:25

尝试跟踪动画时间肯定需要做太多的工作。当前代码无法正常工作的确切原因是,必须在 AnimationUtils.currentAnimationTimeMillis() 返回的时间值上下文中调用 setStartTime(),而不是系统时间。

然而,一种更简单的方法是使用 AnimationListener 对象在第一个动画完成时通知您以便启动第二个动画。换句话说:

Animation fadeOut = new AlphaAnimation(1.0f, 0.1f);
fadeOut.setDuration(500);
fadeOut.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) { }

    @Override
    public void onAnimationRepeat(Animation animation) { }

    @Override
    public void onAnimationEnd(Animation animation) {
        Animation fadeIn = new AlphaAnimation(0.1f, 1.0f);
        fadeIn.setDuration(500);
        imgBtn.startAnimation(fadeIn);
    }
});

imgBtn.startAnimation(fadeOut);

HTH

Definitely way too much work being done to try and track the animation times. The exact reason your current code isn't working well is because setStartTime() must be called in the context of the time values returned by AnimationUtils.currentAnimationTimeMillis() instead of the system time.

However, a much simpler approach is to use an AnimationListener object to notify you when the first animation is complete in order to start the second one. In other words:

Animation fadeOut = new AlphaAnimation(1.0f, 0.1f);
fadeOut.setDuration(500);
fadeOut.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) { }

    @Override
    public void onAnimationRepeat(Animation animation) { }

    @Override
    public void onAnimationEnd(Animation animation) {
        Animation fadeIn = new AlphaAnimation(0.1f, 1.0f);
        fadeIn.setDuration(500);
        imgBtn.startAnimation(fadeIn);
    }
});

imgBtn.startAnimation(fadeOut);

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文