我有這ImageButton。我希望它在特定時間開始動畫。 我看了一下android文檔,發現setStartTime(long startTimeMillis)。如何在特定時間啓動Android AlphaAnimation?
這是我想出了:
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使第一動畫,而不是第二..
當我使用[startAnimation()](http://developer.android.com/reference/android/view/View.html#startAnimation(android.view。 animation.Animation))它工作正常。 – kristianfroelund 2012-02-13 19:51:50