动画,延迟

时间:2013-02-04 13:33:56

标签: android animation delay

我想制作动画,我有10张球的图像,我想这样做:

  1. Image1可见,隐藏所有其他图片
  2. Image2可见所有其他隐藏的图片
  3. ...
  4. Image10可见所有其他隐藏的图片
  5. 我应该在一个动画中这样做(怎么做?)或者我应该创建一个java方法showImage(String pathimage)来显示位于pathimage的图像并隐藏其他图像?我可以使用

    来了解时间延迟
    handler.postDelayed(new Runnable() { 
        public void run() { }
    }
    

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

    GAMEACTIVITY

        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);
        }
    }
    

1 个答案:

答案 0 :(得分:1)

您不应创建10个ImageView来执行此类动画。

只有一个ImageView应该足够了!使用处理程序每​​隔x毫秒左右使用setImage的任何风格更新Image。

初始化数组中的图像,并将当前图像初始化为整数:

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
    }
};

最后,当你想要开始时,只需发布​​runnable:

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);
            }
}

}