Android游戲開發(fā)之旅(三)View類詳解
Posted on 2010-12-03 10:17 啥都寫點(diǎn) 閱讀(377) 評(píng)論(0) 編輯 收藏 所屬分類: Android在Android游戲 開發(fā) 之旅 二中我們講到了View 和SurfaceView的區(qū)別,今天Android123從View類開始著重的介紹 Android圖形顯示基類的相關(guān)方法和注意點(diǎn)。(文/Android開發(fā)網(wǎng))
自定義 View的常用方法:
onFinishInflate() 當(dāng)View中所有的子控件 均被映射成xml后觸發(fā)
onMeasure(int, int) 確定所有子元素的大小
onLayout(boolean, int, int, int, int) 當(dāng)View分配所有的子元素的大小和位置時(shí)觸發(fā)
onSizeChanged(int, int, int, int) 當(dāng)view的大小發(fā)生變化時(shí)觸發(fā)
onDraw(Canvas) view渲染內(nèi)容的細(xì)節(jié)
onKeyDown(int, KeyEvent) 有按鍵按下后觸發(fā)
onKeyUp(int, KeyEvent) 有按鍵按下后彈起時(shí)觸發(fā)
onTrackballEvent(MotionEvent) 軌跡球事件
onTouchEvent(MotionEvent) 觸屏事件
onFocusChanged(boolean, int, Rect) 當(dāng)View獲取 或失去焦點(diǎn)時(shí)觸發(fā)
onWindowFocusChanged(boolean) 當(dāng)窗口包含的view獲取或失去焦點(diǎn)時(shí)觸發(fā)
onAttachedToWindow() 當(dāng)view被附著到一個(gè)窗口時(shí)觸發(fā)
onDetachedFromWindow() 當(dāng)view離開附著的窗口時(shí)觸發(fā),Android123提示該方法和 onAttachedToWindow() 是相反的。
onWindowVisibilityChanged(int) 當(dāng)窗口中包含的可見的view發(fā)生變化時(shí)觸發(fā)
以上是View實(shí)現(xiàn)的一些基本接口的回調(diào)方法,一般我們需要處理畫布的顯示時(shí),重寫onDraw(Canvas)用的的是最多的:
view plaincopy to clipboardprint?
@Override
protected void onDraw(Canvas canvas) {
//這里我們直接使用canvas對(duì)象處理當(dāng)前的畫布,比如說使用Paint來選擇要填充的顏色
Paint paintBackground = new Paint();
paintBackground.setColor(getResources().getColor(R.color.xxx)); //從Res中找到名為xxx的color顏色定義
canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackground); //設(shè)置當(dāng)前畫布的背景顏色為paintBackground中定義的顏色,以0,0作為為起點(diǎn),以當(dāng)前畫布的寬度和高度為重點(diǎn)即整塊畫布來填充,具體的請(qǐng)查看Android123未來講到的Canvas和Paint,在Canvas中我們可以實(shí)現(xiàn)畫路徑,圖形,區(qū)域,線。而Paint作為繪畫方式的對(duì)象可以設(shè)置顏色,大小,甚至字體的類型等等。
}
@Override
protected void onDraw(Canvas canvas) {
//這里我們直接使用canvas對(duì)象處理當(dāng)前的畫布,比如說使用Paint來選擇要填充的顏色
Paint paintBackground = new Paint();
paintBackground.setColor(getResources().getColor(R.color.xxx)); //從Res中找到名為xxx的color顏色定義
canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackground); //設(shè)置當(dāng)前畫布的背景顏色為paintBackground中定義的顏色,以0,0作為為起點(diǎn),以當(dāng)前畫布的寬度和高度為重點(diǎn)即整塊畫布來填充,具體的請(qǐng)查看Android123未來講到的Canvas和Paint,在Canvas中我們可以實(shí)現(xiàn)畫路徑,圖形,區(qū)域,線。而Paint作為繪畫方式的對(duì)象可以設(shè)置顏色,大小,甚至字體的類型等等。
}
當(dāng)然還有就是處理窗口還原狀態(tài)問題(一般用于橫豎屏切換),除了在Activity中可以調(diào)用外,開發(fā)游戲時(shí)我們盡量在View中使用類似
view plaincopy to clipboardprint?
@Override
protected Parcelable onSaveInstanceState() {
Parcelable p = super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putInt("x", pX);
bundle.putInt("y", pY);
bundle.putParcelable("android123_state", p);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
dosomething(bundle.getInt("x"), bundle.getInt("y")); //獲取剛才存儲(chǔ)的x和y信息
super.onRestoreInstanceState(bundle.getParcelable("android123_state"));
return;
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable p = super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putInt("x", pX);
bundle.putInt("y", pY);
bundle.putParcelable("android123_state", p);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
dosomething(bundle.getInt("x"), bundle.getInt("y")); //獲取剛才存儲(chǔ)的x和y信息
super.onRestoreInstanceState(bundle.getParcelable("android123_state"));
return;
}
本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/JavaTiger427/archive/2010/11/25/6034540.aspx
-- 學(xué)海無涯