在 ViewFlipper 中运行动画以在按钮单击多次时翻转图像

发布于 2024-12-17 06:05:09 字数 5439 浏览 0 评论 0原文

我正在 Android 中开发 LCR 骰子游戏,但遇到问题,当我尝试再次单击按钮来运行图像翻转动画时,我正在使用 Asynctask 来运行动画 这是我的项目的示例代码:

public class FlippingImage extends AsyncTask<Void, Long, Long>
    {

        @Override
        protected Long doInBackground(Void... params) {

            // TODO Auto-generated method stub
            int timer=0;

            mp=MediaPlayer.create(LCRDiceActivity.this,R.raw.rollingdices);
            mp.start();
            while (timer<5000) 
            {
                mp.start();

        Log.i("LCRgame", "start flip");

        mFlipper.startFlipping();
        mFlipper.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
        mFlipper2.startFlipping();
        mFlipper2.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper2.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
        mFlipper3.startFlipping();
        mFlipper3.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper3.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));    
        SystemClock.sleep(1000);
        timer=timer+1000;

            }  

        return null;

        }

        @Override
        protected void onProgressUpdate(Long... values) {
            // TODO Auto-generated method stub

            super.onProgressUpdate(values);

            LCRDiceActivity.this.ChipsCountPlayer1.setText(ChipsCount1);
        }

        @Override
        protected void onPostExecute(Long result) {
            // TODO Auto-generated method stub
            //mFlipper.stopFlipping();
             Random myRondom=new Random();
                int myNumber=myRondom.nextInt(5)+1;
                switch (myNumber) {
                case 1:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_L_1)));

                     ChipsCount1=ChipsCount1-1;

                    break;
                case 2:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_C_1)));
                    ChipsCount1=ChipsCount1-1;

                    break;
                case 3:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_R_1)));

                    ChipsCount1=ChipsCount1-1;
                    break;
                case 4:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                case 5:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                case 6:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                default:
                    break;

                }   

}

这是buttonClickListener:

private OnClickListener StartClickListener=new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        // flipImage=new FlippingImage();

        // flipImage.execute( );

            Runnable myRunner = new Runnable(){
                 public void run() {
                    new FlippingImage().execute();
            }};
            myHandler=new Handler();
            myHandler.post(myRunner);


        }


    };

我收到此错误:

11-21 14:16:25.861: ERROR/AndroidRuntime(329): FATAL EXCEPTION: AsyncTask #2
11-21 14:16:25.861: ERROR/AndroidRuntime(329): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.lang.Thread.run(Thread.java:1019)
11-21 14:16:25.861: ERROR/AndroidRuntime(329): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)

如何解决这个问题请帮助我?

I'm working on LCR dice game in Android and I have a problem,when I try to click again on a button to run image flip animation,I'm using Asynctask to run animation
this is a sample code of my project:

public class FlippingImage extends AsyncTask<Void, Long, Long>
    {

        @Override
        protected Long doInBackground(Void... params) {

            // TODO Auto-generated method stub
            int timer=0;

            mp=MediaPlayer.create(LCRDiceActivity.this,R.raw.rollingdices);
            mp.start();
            while (timer<5000) 
            {
                mp.start();

        Log.i("LCRgame", "start flip");

        mFlipper.startFlipping();
        mFlipper.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
        mFlipper2.startFlipping();
        mFlipper2.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper2.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
        mFlipper3.startFlipping();
        mFlipper3.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
        mFlipper3.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));    
        SystemClock.sleep(1000);
        timer=timer+1000;

            }  

        return null;

        }

        @Override
        protected void onProgressUpdate(Long... values) {
            // TODO Auto-generated method stub

            super.onProgressUpdate(values);

            LCRDiceActivity.this.ChipsCountPlayer1.setText(ChipsCount1);
        }

        @Override
        protected void onPostExecute(Long result) {
            // TODO Auto-generated method stub
            //mFlipper.stopFlipping();
             Random myRondom=new Random();
                int myNumber=myRondom.nextInt(5)+1;
                switch (myNumber) {
                case 1:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_L_1)));

                     ChipsCount1=ChipsCount1-1;

                    break;
                case 2:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_C_1)));
                    ChipsCount1=ChipsCount1-1;

                    break;
                case 3:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_R_1)));

                    ChipsCount1=ChipsCount1-1;
                    break;
                case 4:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                case 5:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                case 6:
                    mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));

                    break;
                default:
                    break;

                }   

}

And this is buttonClickListener :

private OnClickListener StartClickListener=new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        // flipImage=new FlippingImage();

        // flipImage.execute( );

            Runnable myRunner = new Runnable(){
                 public void run() {
                    new FlippingImage().execute();
            }};
            myHandler=new Handler();
            myHandler.post(myRunner);


        }


    };

And I got this error:

11-21 14:16:25.861: ERROR/AndroidRuntime(329): FATAL EXCEPTION: AsyncTask #2
11-21 14:16:25.861: ERROR/AndroidRuntime(329): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.lang.Thread.run(Thread.java:1019)
11-21 14:16:25.861: ERROR/AndroidRuntime(329): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)

How to solve this problem please help me ?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-12-24 06:05:09

首先,错误是说你不能触摸 ViewFlipper 除非你位于创建 ViewFlipper 的同一个线程中(我假设是 UI 线程)

似乎你可能必须使用 runOnUiThread( Runnable ) 来扰乱 ViewFlipper 上的动画视图翻转器

First, the error is saying you cannot touch the ViewFlipper unless you were in the same thread that created the ViewFlipper (which is the UI thread I am assuming)

Seems like you might have to use runOnUiThread( Runnable ) to mess with the animations on the view flipper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文