为什么我的动画在返回包含它的活动时会冻结?

发布于 2025-01-01 13:03:08 字数 2770 浏览 2 评论 0原文

我对 Android 还是个新手,所以如果这是一个明显的错误,请原谅我。在此活动中,我使用 ViewPager 水平滚动三个布局,其中包含一个 ImageButton,该布局具有取决于其当前状态的动画背景。当按下按钮时,它会启动一个新的活动。但是,当我点击后退按钮返回到包含新活动中的动画的活动时,有时动画会冻结或播放得比应有的速度快。我编写了一个启动动画的方法,并在 onWindowFocusChanged()onRestart() 中使用。我正在使用 Android 2.1 (API 7)。

这是我的代码:

public class CopyOfWorld extends Activity{

    MediaPlayer muzak;
    Boolean mSwitch = false;
    ImageButton holmes;
    AnimationDrawable holmesAnimation;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.world);

        Preferencer pp = (Preferencer)getApplicationContext();

        ViewPagerAdapter adapter = new ViewPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);


        if(pp.getMuzak()){
            mSwitch = true;
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
    }

    public void clicker(View v){
     Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
        startActivityForResult(myIntent, 0);
   }

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        beginRender();

    }

    @Override
    protected void onPause(){
        super.onPause();
        if(mSwitch){
        muzak.release();
        }
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Preferencer pp = (Preferencer)getApplicationContext();
        if(pp.getMuzak()){
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
        beginRender();
    }


    public void beginRender(){
        ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
        StateListDrawable background = (StateListDrawable) holmes.getBackground();
        Drawable current = background.getCurrent();
        if(current instanceof AnimationDrawable){
            holmesAnimation = (AnimationDrawable) current;
            holmesAnimation.start();
        }
    }
}

我尝试在 ()onResume 下调用方法 beginRender(),但应用程序只是崩溃了。

有人能指出我正确的方向吗?

编辑:

我一直在各处调整代码,不幸的是没有效果。但我确实注意到动画行为的模式。当我按下 ImageButton 或按住它,使其从默认动画变为按下或聚焦动画,然后将手指从按钮上移开,这样它就不会启动新活动 的行为与我在本文开头所描述的非常相似(即它应该返回到默认动画,但相反以两倍的速率播放、卡住或根本不播放。)

,它有时 包含这些ImageButton 将其背景定义为动画,并且没有源 (src)。但是,当我将 background 更改为透明并将 src 更改为动画时,应用程序崩溃了。

有什么线索吗?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).

This is my code:

public class CopyOfWorld extends Activity{

    MediaPlayer muzak;
    Boolean mSwitch = false;
    ImageButton holmes;
    AnimationDrawable holmesAnimation;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.world);

        Preferencer pp = (Preferencer)getApplicationContext();

        ViewPagerAdapter adapter = new ViewPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);


        if(pp.getMuzak()){
            mSwitch = true;
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
    }

    public void clicker(View v){
     Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
        startActivityForResult(myIntent, 0);
   }

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        beginRender();

    }

    @Override
    protected void onPause(){
        super.onPause();
        if(mSwitch){
        muzak.release();
        }
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Preferencer pp = (Preferencer)getApplicationContext();
        if(pp.getMuzak()){
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
        beginRender();
    }


    public void beginRender(){
        ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
        StateListDrawable background = (StateListDrawable) holmes.getBackground();
        Drawable current = background.getCurrent();
        if(current instanceof AnimationDrawable){
            holmesAnimation = (AnimationDrawable) current;
            holmesAnimation.start();
        }
    }
}

I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.

Could anyone point me in the right direction?

EDIT:

I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)

Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.

Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文