android - 停止动画并重新开始

标签 android exception animation imageview wait

当我单击动画 ImageView 时,我想在 ObjectAnimation 运行时停止它。然后,我想在该 ImageView 上播放 FrameAnimation。之后,第一个动画再次开始。

这是我的 OnClickListener:

OnClickListener click = new OnClickListener() {
            @Override
                public void onClick(View arg0) {


                try {
                    animator.wait(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                setFrameAnimation(view);


            }
        };

        view.setOnClickListener(click);

Animator就是ObjectAnimator动画。
View 是我的ImageView

我的 setFrameAnimation 方法:

AnimationDrawable frameAnimation = (AnimationDrawable)view.Background();
frameAnimation.start();

此代码无效。调用 wait() 时出现 IllegalMonitorStateException。

最佳答案

Use this 


int duration = 150;
        img = (ImageView)findViewById(R.id.imageView1);

        BitmapDrawable frame1 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y1);
        BitmapDrawable frame2 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y2);
        BitmapDrawable frame3 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y3);
        BitmapDrawable frame4 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y4);
        BitmapDrawable frame5 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y5);


        animation = new AnimationDrawable();

        animation.addFrame(frame1, duration);
        animation.addFrame(frame2, duration);
        animation.addFrame(frame3, duration);
        animation.addFrame(frame4, duration);
        animation.addFrame(frame5, duration);
//        animation.addFrame(frame6, duration);
//        animation.addFrame(frame7, duration);
//        animation.addFrame(frame8, duration);
//        animation.addFrame(frame9, duration);
//        animation.addFrame(frame10, duration);


  img.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
             animation.setOneShot(false);
                img.setBackgroundDrawable(animation);
                animation.start();
        }
    });



        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);

        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animation.start();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animation.stop();
            }
        });

关于android - 停止动画并重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18104167/

相关文章:

android - 是否有必要从 Firebase 实时数据库中删除每个添加的监听器?

android - 我可以在 Android 上获得英语以外语言的语音识别吗?

c++ - Shared_ptr 和 unique_ptr 有异常(exception)

javascript - HttpCompileException 未被用户代码处理(当我在脚本中添加 C# 代码时)

java - 如何使用上次关闭的设置启动应用程序?

java - 从 parse.com 接收文本文件并放置在 textview 中

java - 如何以用户友好的方式处理java异常

animation - 动画后保持 SVG 属性为最终值

css - 如何创建 Material Design 径向 react 动画

javascript - 如何使 3D 地球动画始终从面向相机的球体的同一侧开始