2012年8月3日 星期五

【Android遊戲開發之七】(遊戲開發中需要的樣式)再次剖析遊戲開發中對SurfaceView中添加元件方案!


各位童鞋請你們注意:surfaceview中確實有 onDraw這個方法,但是surfaceview不會自己去調用!!!
而我代碼中的ondraw 也好 draw 也好,都是我自己定義的一個方法。。。放在執行緒中不斷調用的,一定要注意!! 
上一篇我們已經可以在同一介面中既顯示我們的surfaceview和button、textview等組件,那麼基本算是成功了,但是身為遊戲開發人員,如果不是想故意要這種類似電影形式的展現方式(我們的surfaceview在中間 - -.很想播放電影似的。。),是絕對不允許遊戲的分量只是占了中間那麼一部分,肯定需要全屏顯示,別的元件只是一個配角的角色。那麼下面先上一張截圖看下為什麼修改與調整。


看到我們畫出來的字體了吧,很悲劇被覆蓋了!只要有button就會有一塊長條,即使我們修改button中佈局的顏色也只是把長條的顏色變成白色,當然好看是好看了,但是仍舊遮擋我們的字體!這可不是我們想要的結果。我們想要的效果應該是下圖這樣的:


娃哈哈,這效果就對啦,我們的view占滿全屏,而組件本身才會對我們的view中的內容有遮擋,不會多出一些無用的長條遮擋.... 
當時雖然想的方法就是佈局xml的問題,我一開始想在我們xml中定義的surfaceview中直接添加按鈕,但是view不能添加view!所以沒辦法,就想到是否是佈局的問題。經過多次嘗試才終於成功做到。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="1" >
  11. <com.himi.MySurfaceView android:id="@+id/view3d"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"/>
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true"
  18. android:text="Himi Button_1"
  19. android:id="@+id/button1"/>
  20. <Button android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentBottom="true"
  23. android:layout_toRightOf="@id/button1"
  24. android:text="Himi Button_2"
  25. android:id="@+id/button2"/>
  26. <TextView
  27. android:id="@+id/textview"
  28. android:layout_width="fill_parent"
  29. android:layout_height="fill_parent"
  30. android:text="This is Himi"
  31. android:textSize="32sp"
  32. android:textColor="#00FF00"
  33. android:gravity="center_horizontal"/>
  34. </RelativeLayout>
  35. </LinearLayout>
  xml 修改的不大,主要將之前的線性佈局改成了相對佈局。雖然改動不大,但是也真的費了不少時間去調整、這樣一來大家就可以在自己的遊戲Surfaceview中隨意添加元件啦,娃哈哈~~~
源碼在上一篇已經給出下載位址,這裡也只是對xml的修改大家需要可以去下載上一篇的源碼,將xml調整即可

沒有留言:

張貼留言