如何使用处理程序同步动画?

时间:2013-12-23 09:13:13

标签: java android

Polar_Anim

public class Polar_Anim {
            private static ImageView animImage;

            boolean pauseAnim = false;
            static int timeCounter = 0;
            int timeToRefresh = 180;
            static int animImageCount = 3;

            public Polar_Anim(ImageView animImage){
                pauseAnim = false;
                timeCounter = 0;
                Polar_Anim.animImage = animImage;
            }

         Handler mHandlerToAnimate = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    if(msg.what == 1){

                        switch (timeCounter) {
                        case 0:
                            animImage.setImageResource(R.drawable.polarbearmain1);
                            break;
                        case 1:
                            animImage.setImageResource(R.drawable.polarbearmain2);
                            break;
                        case 2:
                            animImage.setImageResource(R.drawable.polarbearmain3);
                            break;
                        default:
                            animImage.setImageResource(R.drawable.polarbearmain1);
                            break;
                        }

                        if(timeCounter <= (animImageCount-1)){
                            timeCounter++;
                        }



                    }
                };
            };

            private Runnable runnableToAnimate = new Runnable() {
                public void run() {
                    Message msg = new Message();
                    msg.what = 1;
                    mHandlerToAnimate.handleMessage(msg);
                    if(!pauseAnim){

                        mHandlerToAnimate.postDelayed(this, timeToRefresh);
                    }
                }

            };





        public void startPolarAnimation(){
            mHandlerToAnimate.post(runnableToAnimate);
        }

        public void stopPolarAnimation(){
            pauseAnim = true;
            if (mHandlerToAnimate != null) {
                mHandlerToAnimate.removeCallbacks(runnableToAnimate);
            }
        }

        public boolean isRunning(){
            return !pauseAnim;
        }

这是我使用我的处理程序的地方。

    polarimg.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        polar_anim=new Polar_Anim(polarimg);

                        if(event.getAction()==MotionEvent.ACTION_DOWN)  {

                            int id=v.getId();

                            if(id==R.id.imgview_polar)
                            {
                                    if(!polar_anim.isRunning())
                                    polar_anim.startPolarAnimation();
                                    else
                                        polar_anim.stopPolarAnimation();
                        }
                            mainPlayer(R.raw.bubblebrew);
                        }
                        return false;
                    }
                });

上面是我的动画代码。它工作正常,但问题是如果我连续播放1-2分钟它将不会执行动画。它只显示单个图像。     为什么会这样?

0 个答案:

没有答案