0

我想做一个动画,我有 10 个球的图像,我想做:

  1. Image1可见,所有其他图像隐藏
  2. Image2可见所有其他隐藏的图像
  3. ...
  4. Image10可见所有其他隐藏的图像

我应该在一个动画中做到这一点(如何?)还是应该创建一个 java 方法 showImage(String pathimage) 来显示位于 pathimage 的图像并隐藏其他的?我可以使用任何关于时间延迟的想法吗

handler.postDelayed(new Runnable() { 
    public void run() { }
}

编辑,这是我的代码 问题是 run() 总是在似乎是无限循环的情况下被调用。但我看不出我的错误在哪里

游戏活动

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.....
        ImageView image2 = (ImageView) findViewById(R.id.imageView3);
        image2.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                myAsyncRunnable mar = new myAsyncRunnable(GameActivity.this);
                mar.execute(null);
                PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this);
                photoTask.execute(null);
                Log.w("GAMEACTIVITY","PHOTOTASK");
                return false;
            }

        });
}

我的异步任务

public class myAsyncRunnable extends AsyncTask<Void, Boolean, Void> {

    GameActivity gameactivity;

    @Override
    protected Void doInBackground(Void... params) {
        pull();
        return null;
    }

public myAsyncRunnable(GameActivity gameactivity) {
    super();
    this.gameactivity = gameactivity;
}


    public void pull() {

        final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
        final int drawables[] = new int[] {R.drawable.pull2,R.drawable.pull3,R.drawable.pull4,R.drawable.pull5,R.drawable.pull6,R.drawable.pull7};
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                for (int i=0;i<drawables.length;i++) {
                    image3.setImageResource(drawables[i]);
                    gameactivity.handler.postDelayed(this, 500);
                    Log.w("asyncrunnable","run"+i);
                }

            }

        };
        gameactivity.handler.post(runnable);
    }
}
4

1 回答 1

1

你不应该创建 10 个 ImageViews 来做这样的动画。

只有一个 ImageView 就足够了!使用处理程序以setImage每 x 毫秒左右的任何形式更新图像。

初始化数组中的图像和整数中的当前图像:

int drawables[] = new int[] {R.drawable.image1, R.drawable.image2, ...};
int mCurrent = 0;

然后初始化您的更新程序:

 Runnable runnable = new Runnable() {

    @Override
    public void run() {
                 if(mCurrent>=drawables.length)mCurrent=0;
                 imageView.setImageResource(drawables[mCurrent]);
                 mHandler.postDelayed(this, 3000); //every 3 seconds
    }
};

最后,当您想开始时,只需发布​​可运行文件:

mHandler.post(runnable);

你的代码是错误的:

试试这个拉功能(虽然我并不真正认可整个代码风格,但没有时间修复它)

public void pull() {

    final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
    final int drawables[] = new int[] {R.drawable.pull2,R.drawable.pull3,R.drawable.pull4,R.drawable.pull5,R.drawable.pull6,R.drawable.pull7};
            for (int i=0;i<drawables.length;i++) {
                final int j = i;
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
                image3.setImageResource(drawables[j]);

        }

    };
                gameactivity.handler.postDelayed(this, 500 * i);
                Log.w("asyncrunnable","run"+i);
            }
}

}

于 2013-02-04T13:39:01.727 回答