我有一个游戏类
我这样做的地方
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
puzzle = getPuzzle(diff);
calculateUsedTiles();
puzzleView = new PuzzleView(this);
Log.d(TAG,"new Puxxle");
setContentView(puzzleView);
Log.d(TAG,"setContent");
puzzleView.requestFocus();
}
我有我的PuzzleView类
public PuzzleView(Context context) {
super(context);
Log.d(TAG, "puzzleview");
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
Log.d(TAG, "w and h" + w + " " + h);
width = w / 9f;
height = h / 9f;
Log.d(TAG, "selY and x" + selX + " " + selY);
getRect(selX, selY, selRect);
Log.d(TAG, "selY and x" + selX + " " + selY);
Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
super.onSizeChanged(w, h, oldw, oldh);
}
private void getRect(int x, int y, Rect rect) {
rect.set((int) (x * width), (int) (y * height),
(int) (x * width + width), (int) (y * height + height));
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG,"drawing");
Paint background = new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
}
现在我的理解是,在Game类中,我们执行setContentView
时会调用所有onsizeChanged和onDraw,但是在调用setContent之后会发生这种情况。
这是我的日志序列
D/Game ( 4175): onCreate
D/PuzzleView( 4175): puzzleview
D/Game ( 4175): new Puxxle
D/Game ( 4175): setContent
D/PuzzleView( 4175): w and h320 430
D/PuzzleView( 4175): selY and x0 0
D/PuzzleView( 4175): selY and x0 0
D/PuzzleView( 4175): onSizeChanged: width 35.555557, height 47.77778
D/PuzzleView( 4175): drawing
答案 0 :(得分:0)
当一个Activity获得焦点时,将要求它绘制其布局。 Android框架将处理绘制过程,但Activity必须提供根节点 其布局层次结构。
setContentView()
只设置根节点,但视图的绘制发生在onCreate()
之后的其他位置。