Android – 用于onDraw的Predraw画布

我曾经在onDraw方法的
ImageView中进行绘图.

但是,我已经了解到在onDraw之外绘制画布更好,只需更新onDraw中的画布.

我知道这显然是错误的(因为它不起作用),但我将如何完成我正在尝试做的事情:

@Override 
public void onDraw(Canvas c) {
  c = this.newCanvas;
  super.onDraw(c);
}

最佳答案

    public class GameLoopThread extends Thread {
private GameView view;
private boolean running = false;

public GameLoopThread(GameView view) {
      this.view = view;
}

public void setRunning(boolean run) {
      running = run;
}

@Override
public void run() {

      while (running) {
             Canvas c = null;

             try {
                    c = view.getHolder().lockCanvas();
                    synchronized (view.getHolder()) {
                        if (c != null) {
                            view.onDraw(c);
                        }

                    }
             } finally {
                    if (c != null) {
                           view.getHolder().unlockCanvasAndPost(c);
                    }
             }

             try {

                           sleep(10);
             } catch (Exception e) {}
      }
}
}

然后让你的活动中的那个线程做这样的事情

    @Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(new GameView(GameActivity.this));
}

然后在GameViewClass做这样的事情

   public class GameView extends SurfaceView {

   private SurfaceHolder holder;
   private GameLoopThread gameLoopThread;


   public GameView(Context context) {
         super(context);
         gameLoopThread = new GameLoopThread(this);
         holder = getHolder();
         holder.addCallback(new SurfaceHolder.Callback() {

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                       boolean retry = true;
                       gameLoopThread.setRunning(false);
                       while (retry) {
                              try {
                                    gameLoopThread.join();
                                    retry = false;
                              } catch (InterruptedException e) {
                              }
                       }
                }

                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                       gameLoopThread.setRunning(true);
                       gameLoopThread.start();
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,
                              int width, int height) {
                }
         });

   }

   @Override
   protected void onDraw(Canvas canvas) {
         //Do Drawing
   }
}

这里重要的是线程是手动自动调用onDraw()方法,并锁定画布,绘制它,然后发布它.如果你不需要超快的刷新率,那么你最好做这样的事情:

    @Override 
    public void onDraw(Canvas c) {
        c = this.getHolder().lockCanvas();
        if (c != null) {
           //draw on canvas
        }
        if (c != null) {
            this.getHolder().unlockCanvasAndPost(c);
        }
    }

我只是不知道最后一点是否有效,从未测试过.
另外如果你想在绘图方法之外进行绘图,你可以在一个线程中运行你的更新(在你的画布上绘图),并且每次调用onDraw方法时都要检查Canvas是否准备好它帖子.例如,你的线程有一个布尔值,一旦画布被拉动它被设置为false,所以线程将绘制一个新的,但一旦完成绘图设置布尔值为true.在ondraw方法中检查boolean是否为true以及是否拉动画布.

点赞