Issue
I'm trying to animate an object coming down from an ImageView but the ImageView's X-axis doesn't update while moving.
Here is the Thread:
public class EnemyMoveThread extends Thread // Enemy animation
{
@Override
public void run()
{
**enemyX = enemy.getX();** // this is the problematic part
while (! disqualified())
{
try {
enemy.startAnimation(ltrAnim); // move ImageView left to right
Thread.sleep(600);
}
catch (InterruptedException e)
{ e.printStackTrace(); }
try {
enemy.startAnimation(rtlAnim); // move ImageView left to right
Thread.sleep(600);
}
catch (InterruptedException e)
{ e.printStackTrace(); }
}
}
}
Solution
That's because the Thread can't update gui elements, as it isn't the main thread. However the Activity provides a method called runOnUiThread(Runnable runnable), that can update GUI elements. In your code you would need to get the Activity of your app, using a custom constructor and then call the GUI update methods using
activity.runOnUiThread(() -> {
// your GUI code here
});
Answered By - Sebastian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.