gpt4 book ai didi

java - 动画Drawable + Android + 启动和停止动画

转载 作者:行者123 更新时间:2023-12-01 05:25:17 25 4
gpt4 key购买 nike

我正在创建一个实现动画的应用程序,当某个变量为true时,当我启动 Activity 并且该变量为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);
}
}});
}

预先感谢您的帮助。

最佳答案

下面的代码可能对您有帮助。增量命名图像(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;

关于java - 动画Drawable + Android + 启动和停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com