Я создаю приложение, которое реализует анимацию, когда определенная переменная равна true, когда я запускаю действие, а переменная - true, анимация работает, но если я изменяю переменную, а затем изменяю ее обратно на true Переменная анимация не будет работать. У кого-нибудь есть идеи, почему?
Объявить переменные
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
chargeStatus = (ImageView)findViewById(R.id.chargeStatus);
chargeStatus.setBackgroundResource(R.drawable.chargeon);
// Get the background, which has been compiled to an AnimationDrawable object.
chargeAnimation = (AnimationDrawable) chargeStatus.getBackground();
Запускает поток при запуске приложения
/////When Application Starts
@Override
public void onStart()
{
super.onStart();
Thread cThread = new Thread(new serverThread()); //Starts the thread that initiates communications
cThread.start();
Тема, которая устанавливает значение
public class serverThread implements Runnable
{
//public boolean connected = false; //connection
public void run() //runs the thread
{
try
{
functions.connectToServer(serverIp); //connects to server
connected = true;
while (connected)
{
try
{
connectStatus.setImageResource(R.drawable.blue_car);
functions.getStreams(); //establish stream connections
functions. updateStatus(); //updates the status of the server
changeButtonImage(); //sets initial state for charge button
try
{
Thread.sleep(2500);
}
catch ( InterruptedException interruptedException )
{
functions.displayMessage( "\nThread exception" );
} // end catch
}
catch (IOException e)
{
Log.e("Client","Trouble Getting Streams",e);
}
}//end while
}//end try
catch (Exception e)
{
Log.e("Client", "Error Connecting", e);
connected = false;
connectStatus.setImageResource(R.drawable.red_car);
}
}; //end run
}; //end serverThread
Изменить функцию кнопки
public void changeButtonImage(){
runOnUiThread(new Runnable(){
public void run() {
if (functions.chargeStatNumber != 0)
{
// Start the charge animation
chargeAnimation.start();
//set the Charge on off button to stop
chargeOnOff.setImageResource(R.drawable.charge_off);
}
else
{
// stop the charge animation if running
chargeAnimation.stop();
//set charge on off button to on
chargeOnOff.setImageResource(R.drawable.charge_on);
//set the charging symbol to off
chargeStatus.setImageResource(R.drawable.not_charging);
}
}});
}
Заранее спасибо за помощь.