Android: AnimationDrawable不能与Runnable一起工作


public class MainActivity extends Activity{
   public static Runnable StartRun;
   private ImageView Player;
   protected void onCreate(){
   ...
   Player = (ImageView) findViewById(R.id.imgStart);
   StartRun = new Runnable(){
        @Override
        public void run() {
            Player.setBackgroundResource(R.drawable.loadanimation); //XML animationlist
            AnimationDrawable frameanimation = (AnimationDrawable)
                                            Player.getBackground();
            frameanimation.start(); 
        }
    };
  }
}


public class RadioPlayer extends Service{
    Handler handle;
     public void onCreate(){
         handle = new Handler();
     }
public int onStartCommand(Intent intent, int flags, int startId) {
    handle.post(MainActivity.StartRun);
    if (intent.getBooleanExtra(ACTION_PLAY, false)) {
        play();
    }
    return Service.START_STICKY;
}
 }

我已经测试了AnimationDrawable部分的代码直接在onCreate(),所以它的工作。我已经使用Logcat来确认starrun, run()正在被调用,&我还试图从方法内部定义imageview,而不是像现在这样使用全局。

仍然没有雪茄。有人能帮忙吗?

更新:似乎有一个潜在的问题导致这个错误。我已经成功地在可运行线程之外执行了animationDrawable代码,即直接在活动上(证明动画确实有效,但对我自己来说是不切实际的),我试图使用localbroadcastReceiver,以及处理程序等这些非直接到活动的方法都不成功,我已经完全没有办法了。

由于时间不够,我只好用一个简单的progressdialog来代替帧动画代码。在某种程度上,它达到了目的,然而,这是一个将就的解决方案,需要在几个月后再次解决。如果有人有同样的问题,并能对这个主题有任何启发,请评论/发帖

我认为你应该在onWindowFocusChanged方法上开始你的动画,因为它是知道活动是否对用户可见的最好的地方。在你的代码可运行的只是为了显示动画,我认为。像这样的代码应该可以工作:

public void onWindowFocusChanged(boolean hasFocus) {    
super.onWindowFocusChanged(hasFocus);         
if (hasFocus)
{
   Player.setBackgroundResource(R.drawable.loadanimation); //XML animationlist
   AnimationDrawable frameanimation = (AnimationDrawable) Player.getBackground();
   frameanimation.start();
  }
 }

最新更新