在onPause和onResume之后,动画未在处理程序中执行

时间:2013-12-30 06:10:36

标签: android handlers

我使用帧动画创建了一个动画,我的每个活动都是从GRID VIEW调用的,我在HANDLERS中使用帧动画运行我的动画,问题是在onPause()[按下后退按钮后]以及之后的时候相同的活动被打开,动画没有被执行,(帧被延迟或卡住)任何人都可以帮助知道我应该做什么。

    //This My application object from which i m calling handlers
    imageViewWalrus.setVisibility(View.INVISIBLE);
    imageViewWalrus.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            // TODO Auto-generated method stub
            animWalrus = new WalrusAnimation(imageViewWalrus);
            if (event.getAction()==MotionEvent.ACTION_DOWN) {
                int id=arg0.getId();
                if (id==R.id.imageViewWalrus) {
                    if (!animWalrus.isRunning()) 
                    {
                        //calling handlers
                        animWalrus.startWalrusAnimation();
                    }
                }
                //Music
                mainPlayer(R.raw.walrus);

            } 
            return false;
        }
    });

这是我的Handlers类......

import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class WalrusAnimation {

    private static ImageView animImage;

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

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

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

            if(msg.what == 1){

                switch (timeCounter) {
                case 0:
                    //animation images
                    animImage.setImageResource(R.drawable.walrus1);
                    break;
                case 1:
                    animImage.setImageResource(R.drawable.walrus2);
                    break;
                case 2:
                    animImage.setImageResource(R.drawable.walrus3);
                    break;
                case 3:
                    animImage.setImageResource(R.drawable.walrus1);
                    break;
                case 4:
                    animImage.setImageResource(R.drawable.walrus2);
                    break;
                case 5:
                    animImage.setImageResource(R.drawable.walrus3);
                    break;
                case 6:
                    animImage.setImageResource(R.drawable.walrus1);
                    break;
                case 7:
                    animImage.setImageResource(R.drawable.walrus2);
                    break;
                case 8:
                    animImage.setImageResource(R.drawable.walrus3);
                    break;
                default:
                    animImage.setImageResource(R.drawable.walrus1);
                    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 startWalrusAnimation(){
        mHandlerToAnimate.post(runnableToAnimate);
    }

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

    public boolean isRunning(){
        return !pauseAnim;
    }
}

0 个答案:

没有答案