简体   繁体   English

AnimationDrawable只工作一次

[英]AnimationDrawable works only once

I really need help to get this animation fixed. 我确实需要帮助来修复此动画。 I'm tsetting an animation dynamic using addFrame(). 我正在使用addFrame()设置动画动态。 Each generated animation consists of 5 frames. 每个生成的动画包含5帧。 Alltogether 5 different animations are possible. 共有5种不同的动画。 The animation is started by a handler - every 5 seconds. 动画由处理程序启动-每5秒钟启动一次。

    setAnimation(); //inital setting of animation

    stimulusOverlay.setBackground(animation);

    final Handler recallStimulusHandler = new Handler();
    Runnable runAnimation = new Runnable() {
        @Override
        public void run() {
            stimulusOverlay.setVisibility(View.VISIBLE);
            animation.start();
            setStimulusInvisibleHandler();
            recallStimulusHandler.postDelayed(this, 5000);
        }
    };
    stimulusOverlay.post(runAnimation);

    private void setStimulusInvisibleHandler(){
        final Handler setInvisibleHandler = new Handler();
        final Runnable setStimulusInvisible = new Runnable() {
            @Override
            public void run() {
                stimulusOverlay.setVisibility(View.INVISIBLE);
                 setAnimation();
            }
        };
        setInvisibleHandler.postDelayed(setStimulusInvisible, 2000);
    }

I need to use the Handler. 我需要使用处理程序。 Otherwise the frames wouldn't be displayed for the given time. 否则,在给定时间内不会显示帧。 After 2 seconds (animation needs about 1,5 sec), I change the animation - for the next loop - with the function setAnimation(). 2秒后(动画需要大约1.5秒),我使用setAnimation()函数更改了动画-下一个循环。

    private void setAnimation() {

        Resources res = this.getResources();
        String stimulusString = getStimulusFromPref(countAnimation); // never empty
        int stimulusId = res.getIdentifier(stimulusString, "drawable", this.getPackageName());
        int stimulusLength = stimulusString.length();
        String maskingString = "mask_" + stimulusLength;
        int maskingId = res.getIdentifier(maskingString, "drawable", this.getPackageName());

        AnimationDrawable nextAnimation = new AnimationDrawable();
        nextAnimation.setOneShot(true);
        nextAnimation.addFrame(res.getDrawable(R.drawable.transparent), 300);
        nextAnimation.addFrame(res.getDrawable(maskingId), 300);
        nextAnimation.addFrame(res.getDrawable(stimulusId), 300);
        nextAnimation.addFrame(res.getDrawable(maskingId), 300);
        nextAnimation.addFrame(res.getDrawable(R.drawable.transparent), 300);
        animation = nextAnimation;

        countAnimation = (countAnimation + 1)%5;
    }

The Code is running fine for the first time. 该代码首次运行正常。 Afterwards nothing shows up anymore, without throwing an exeption or warning. 之后,什么也没有出现,没有抛出任何警告或警告。

Using logs: 使用日志:

The code is running through for each 5 sec 代码每5秒运行一次

the dynamic image generation works fine (for each possible combination) 动态图像生成效果很好(适用于每种可能的组合)

I also thought about something like recursive calls at the end of setAnimation() , but I think calling the handler every 5 sec makes recursion invalid. 我还考虑了setAnimation()末尾的递归调用之类的事情,但我认为每5秒调用一次处理程序会使递归无效。 getResources() always returns the same value, so I think everything works out here. getResources()始终返回相同的值,所以我认为这里一切正常。 I need to overwrite animation with a new animation because there is no possibility to reset the animation like removeFrames or something like that. 我需要用新的动画覆盖animation ,因为无法重置诸如removeFrames之类的动画。 There is also some further code making the ImageView visible/invisible, but thats not the problem either. 还有一些其他代码使ImageView可见/不可见,但这也不是问题。 I'm adding the ImageView to my windowManager like this: (don't think that's the problem, just for the sake of completeness) 我将ImageView像这样添加到我的windowManager中:(不要为了完整性而认为这是问题)

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    stimulusOverlay = new ImageView(this);
    stimulusOverlay.setVisibility(View.VISIBLE);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

    windowManager.addView(stimulusOverlay, params);

Maybe you have an idea of what's going wrong here. 也许您对这里出了什么问题有个想法。 Just ask for further questions or something. 只是问其他问题。

Clearly you are calling the handler only once.After first execution handler is not called again and it stops its execution.Instead call stimulusOverlay again in loop like this. 显然,您仅调用处理程序一次。在第一次执行处理程序之后,不再再次调用它并停止执行。相反,在这样的循环中再次调用stimulusOverlay。

final Handler recallStimulusHandler = new Handler();
Runnable runAnimation = new Runnable() {
    @Override
    public void run() {
        stimulusOverlay.setVisibility(View.VISIBLE);
        animation.start();
        setStimulusInvisibleHandler();
        recallStimulusHandler.postDelayed(this, 5000);
        //call this handler again
        stimulusOverlay.post(runAnimation);
    }
};
stimulusOverlay.post(runAnimation);

Also remove setInvisibleHandler.postDelayed(setStimulusInvisible, 2000) inside setStimulusInvisibleHandler() method. 还要删除setInvisibleHandler.postDelayed(setStimulusInvisible, 2000) ()方法内的setInvisibleHandler.postDelayed(setStimulusInvisible, 2000)

You should remove setOneShot(true); 您应该删除setOneShot(true); line 线

setOneShot (boolean oneShot): Sets whether the animation should play once or repeat. setOneShot (布尔oneShot):设置动画播放一次还是重复播放。

details are available here 详情请点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM