我是 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();
}
}
到目前为止,我可以获得电池状态、插头类型和电池的运行状况。
启动类还应该实现停止逻辑。
就像是:
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 中。匿名声明不允许您在需要时更改其值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)