我是 Android 新手。我想在手机中制作一个电池充电动画,例如,在屏幕的右上角,小图标在充电时上下移动并在当前电池百分比处停止.
到目前为止,在我的代码中,我已经能够让它移动,但它永远不会停止。
我想要的是动画在未充电时停止,或者在当前电池百分比时停止。
这是我在动画中的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.batteryinfo);
ImageView batteryImage = (ImageView) findViewById(R.id.BatteryImage);
batteryImage.setBackgroundResource(R.drawable.ic_battery_animation);
BatteryAnimation = (AnimationDrawable) batteryImage.getBackground();
batteryImage.post(new Starter());
textBatteryLevel = (TextView) findViewById(R.id.batterylevel_text);
registerBatteryLevelReceiver();
}
class Starter implements Runnable {
public void run() {
BatteryAnimation.start();
}
}
到目前为止,我可以获得电池状态、插头类型和电池健康状况。
最佳答案
Starter 类还应该实现停止逻辑。 像这样的东西:
class Starter implements Runnable {
boolean stopConditionMet = false;
public void run() {
BatteryAnimation.start();
try {
while (!stopConditionMet) { Thread.sleep(500); }
} catch (InterruptedException e) {}
BatteryAnimation.stop();
}
public void stop() { stopConditionMet=true; }
}
(您可以使用 wait()-notifyAll() 方案代替忙等待。上面的示例是为了简单起见)。
.. 并且您需要将 Starter 类的实例保留在您的 Activity 中。匿名声明它不允许您在需要时更改其值。
关于java - Android状态栏中的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9216616/