0

我有一个在屏幕上旋转的图像。现在的问题是,我想在 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();
    }

}
4

1 回答 1

1

繁忙的循环绝对是坏主意。永远不要那样做。您应该为此使用 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 );
于 2013-08-07T11:44:28.707 回答