Android Animation/GUI Behaviour
I have here a button which calls a very long lasting calculation. To indicate this to the user, i want to display a loading symbol. The calculation is called in this method in a extra thread (LoadingAnimationThread);
@Override
public void onClick(View v) {
BookView bookView = (BookView) lexs.startBook();
LoadingAnimationThread thread = null;
if (type == BOOK) {
thread = new LoadingAnimationThread(BOOK, book, bookView);
} else if (type == CHAPTER) {
thread = new LoadingAnimationThread(CHAPTER, chapter, bookView);
} else if (type == PARAGRAPH) {
thread = new LoadingAnimationThread(PARAGRAPH, paragraph, bookView);
}
LoadingAnimation.startAnimation();
thread.run();
}
The run method of the thread is the following:
@Override
public void run() {
if (type == BOOK) {
((BookView) bookView).setBookToDisplay(book);
} else if (type == CHAPTER) {
((BookView) bookView开发者_如何学Go).setBookToDisplay(chapter.getBook());
((BookView) bookView).setExpanededChapter(chapter, true);
} else if (type == PARAGRAPH) {
((BookView) bookView).setBookToDisplay(paragraph.getParentChapter().getBook());
((BookView) bookView).setExpanededChapter(paragraph.getParentChapter(), false);
((BookView) bookView).setExpandedParagraph(paragraph);
}
LoadingAnimation.stopAnimation();
}
This calls in the run method of the Thread are the methods which need a lot of calculation. With LoadingAnimation.startAnimation() and LoadingAnimation.stopAnimation() is start respectively stop the animation:
public static void startAnimation() {
lexs.runOnUiThread(new Runnable() {
@Override
public void run() {
loadingImage.setImageResource(R.drawable.loading_background);
animation.start();
Log.v("Animation", "Loading Animation started");
}
});
}
public static void stopAnimation() {
lexs.runOnUiThread(new Runnable() {
@Override
public void run() {
loadingImage.setImageResource(R.drawable.loading0);
animation.stop();
Log.v("Animation", "Loading Animation stopped");
}
});
}
I see the Log lines in Logcat, but the animation isn't visible on the GUI. Why not??? I know the animation works properly, because on other places it works correctly. Sometimes Android doesn't update the UI immediately, that's why i used runOnUIThread(). But the doc for this method says:
If the current thread is the UI thread, then the action is executed immediately.
So i think the current thread isn't the UI thread, but how can i find the UI thread? Or how can i force the Runnable to run immediately??
You should only make a call to runOnUiThread from a background thread. I think you have the logic backwards. You should set it up so startAnimation and stopAnimation don't make any calls to runOnUiThread, just use the code as is. Then startAnimation should be called from the main UI thread and stopAnimation can be called by the background thread with runOnUiThread.
Here's some sample code:
public void onClick(View v) {
// this is the main thread
// ......
LoadingAnimation.startAnimation(); // Don't need to call this with runOnUiThread since we're on teh main thread
thread.run();
}
@Override
public void run() {
runOnUiThread(new Runnable() {
// Need to put this in runOnUiThread since it's changing the UI from the background
if (type == BOOK) {
((BookView) bookView).setBookToDisplay(book);
} else if (type == CHAPTER) {
((BookView) bookView).setBookToDisplay(chapter.getBook());
((BookView) bookView).setExpanededChapter(chapter, true);
} else if (type == PARAGRAPH) {
((BookView) bookView).setBookToDisplay(paragraph.getParentChapter().getBook());
((BookView) bookView).setExpanededChapter(paragraph.getParentChapter(), false);
((BookView) bookView).setExpandedParagraph(paragraph);
}
LoadingAnimation.stopAnimation();
});
}
public static void startAnimation() {
loadingImage.setImageResource(R.drawable.loading_background);
animation.start();
Log.v("Animation", "Loading Animation started");
}
public static void stopAnimation() {
loadingImage.setImageResource(R.drawable.loading0);
animation.stop();
Log.v("Animation", "Loading Animation stopped");
}
精彩评论