我正在创建一个实现动画的应用程序,当某个变量为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();
在app start
上启动帖子 /////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);
}
}});
}
提前感谢您的帮助。
答案 0 :(得分:0)
下面的代码可以帮助你。给图像增加名称(image1,image2,image3 ......)。
public class DemoAnimation extends Activity {
private boolean reverseOrder = false;
private int imageIndex = 0;
AnimationThread animThread;
ImageView imgdone;
public void onCreate(Bundle instance) {
super.onCreate(instance);
setContentView(R.layout.main);
imgdone=(ImageView)findViewById(R.id.image);
if (animThread == null) {
animThread = new AnimationThread();
animThread.start();
}
}
Handler handler = new Handler() {
public void handleMessage(Message message) {
try {
imgdone.setBackgroundResource(R.drawable.image
+ message.arg1);
} catch (Exception e) {
e.printStackTrace();
}
}
};
private class AnimationThread extends Thread {
boolean _startAnimation = true;
public void run() {
while (_startAnimation) {
Message message = new Message();
if (!reverseOrder) {
if (imageIndex >= 5)
reverseOrder = true;
message.arg1 = imageIndex;
if (imageIndex < 5)
imageIndex++;
else if (imageIndex == 5)
imageIndex--;
} else {
if (imageIndex <= 0)
reverseOrder = false;
message.arg1 = imageIndex;
if (imageIndex > 0)
imageIndex--;
else if (imageIndex == 0)
imageIndex++;
}
handler.sendMessage(message);
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}
//停止线程
if (animThread != null)
animThread._startAnimation = false;