gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:13:50 24 4
gpt4 key购买 nike

我有这个 ImageButton。我希望它在某个时间开始动画。我查看了 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): 开始时间:1329162048

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

imgBtn 制作第一个动画,但不制作第二个..

最佳答案

尝试和跟踪动画时间确实需要完成太多工作。您当前代码运行不佳的确切原因是因为 setStartTime() 必须在 AnimationUtils.currentAnimationTimeMillis() 返回的时间值的上下文中调用,而不是系统时间。

但是,更简单的方法是使用 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

关于android - 如何在特定时间启动 Android AlphaAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com