如何在特定时间启动 Android AlphaAnimation?
我有这个图像按钮。我希望它在某个时间开始动画。 我查看了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试跟踪动画时间肯定需要做太多的工作。当前代码无法正常工作的确切原因是,必须在
AnimationUtils.currentAnimationTimeMillis()
返回的时间值上下文中调用setStartTime()
,而不是系统时间。然而,一种更简单的方法是使用
AnimationListener
对象在第一个动画完成时通知您以便启动第二个动画。换句话说: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 byAnimationUtils.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:HTH