【问题标题】:Animation in status bar in AndroidAndroid状态栏中的动画
【发布时间】:2012-02-09 18:30:15
【问题描述】:

我是 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();
        }

    }

到目前为止,我可以获得电池状态、插头类型和电池的健康状况。

【问题讨论】:

    标签: java android android-animation power-management


    【解决方案1】:

    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 中。匿名声明它不会让您在需要时更改它的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2015-07-06
      相关资源
      最近更新 更多