如何在android中生成图像次数

时间:2013-08-07 11:37:42

标签: android

我有一个在屏幕上旋转的图像。现在的问题是,我希望在10秒后在屏幕上反复生成此图像。我尝试了很多使用for循环。但它不起作用。我想在屏幕上一次又一次地生成相同的旋转图像。请帮忙。提前致谢。这是代码。

@Override
public void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    int draw_x = Math.round(System.currentTimeMillis()
            % (this.getWidth() * 2));
    int draw_y = Math.round(System.currentTimeMillis()
            % (this.getHeight() * 2));

    for (int i = 0; i < 10; i++) {

        if (draw_x > this.getWidth())
            draw_x = (this.getWidth() * (2)) - draw_x;
        if (draw_y > this.getHeight())
            draw_y = (this.getHeight() * (2)) - draw_y;
        if (draw_x > this.getWidth())
            draw_x = (this.getWidth() * (2)) - draw_x;
        if (draw_y > this.getHeight())
            draw_y = (this.getHeight() * (2)) - draw_y;

        canvas.drawBitmap(eBall, draw_x, draw_y, null);
        image.add(eBall);


         float time = System.currentTimeMillis(); 
                     while ((time+50000)>System.currentTimeMillis());
         i++;
        Invalidate();
    }

}

1 个答案:

答案 0 :(得分:1)

忙碌循环绝对 BAD IDEA。永远不要那样做。您应该使用Android动画框架并旋转视图或使用Handler()并发布延迟Runnable

Handler h = new Handler();

....

protected Runnable mTick = new Runnable() {
   @Override
   public void run() {
       methodThatRotatesTheImage();

       h.postDelayed( this, delayInMillis );
   }
}

并开始这样做你只需:

h.post( mTick );

并停止:

h.removeCallbacks( mTick );