如何将android上的progressbar用作hp工具栏

如何将android上的progressbar用作hp工具栏,android,android-progressbar,Android,Android Progressbar,我正在做一个小项目,是关于一个游戏的 我设法在游戏中使用进度条作为我的hp栏。 但是,它可以在没有任何动画的情况下工作。 这里的“损坏”是一个整数,它是对hp棒的损坏。 hpBar是对应于curHp的hp bar for( int i=0 ; i<damage ; i++){ new Handler().postDelayed(new Runnable(){ @Override public void run(){ --

我正在做一个小项目,是关于一个游戏的 我设法在游戏中使用进度条作为我的hp栏。 但是,它可以在没有任何动画的情况下工作。 这里的“损坏”是一个整数,它是对hp棒的损坏。 hpBar是对应于curHp的hp bar

for( int i=0 ; i<damage ; i++){
      new Handler().postDelayed(new Runnable(){ 
        @Override
        public void run(){
            --curHp;
            hpBar.setProgress(curHp);
        }
    },40);


}

for(inti=0;i您应该提前创建Handler(),并将其保留在范围内

此外,您还可以休眠线程:

for( int i=0 ; i<damage ; i++){
  new Handler().postDelayed(new Runnable(){ 
    @Override
    public void run(){
       try {
          Thread.sleep(40);
       } catch (InterruptedException e) { }
       --curHp;
        hpBar.setProgress(curHp);
    }
},40);
}
for(int i=0;i