【问题标题】:Stop an animation and start again停止动画并重新开始
【发布时间】:2013-08-07 21:09:36
【问题描述】:

当我单击动画 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。

【问题讨论】:

  • 要使用“等待”,您需要在 synchronizedblock 中
  • 阅读等待方法的文档。
  • 现在我没有得到异常但我没有达到我的目标。

标签: android exception animation imageview wait


【解决方案1】:
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();
            }
        });

【讨论】:

  • 这对我有什么帮助?我无法使用 stop() 停止 ObjectAnimator。
猜你喜欢
  • 2015-03-21
  • 2011-11-30
  • 2013-05-19
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多