編輯:關於Android編程
/** * 主界面 * @author gavin * */ public class MainActivity extends FragmentActivity implements OnClickListener { private Button btn1; private Button btn2; private FragmentManager fm; View playerView; private Fragment currentFragment; private U3dFragment u3dFragment = new U3dFragment(); private MenuOneFragment menuOneFragment = new MenuOneFragment(); private MenuTwoFragment menuTwoFragment = new MenuTwoFragment(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Constants.mUnityPlayer = new UnityPlayer(this); int glesMode = Constants.mUnityPlayer.getSettings().getInt("gles_mode", 1); boolean trueColor8888 = false; Constants.mUnityPlayer.init(glesMode, trueColor8888); fm = getSupportFragmentManager(); setContentView(R.layout.activity_main); initView(); currentFragment = u3dFragment; changeFragment(currentFragment); } /** * 初始化控件 */ private void initView(){ btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btn1: Toast.makeText(MainActivity.this, "btn1", Toast.LENGTH_SHORT).show(); if(currentFragment instanceof U3dFragment || currentFragment instanceof MenuTwoFragment){ currentFragment = menuOneFragment; changeFragment(currentFragment); }else{ currentFragment = u3dFragment; changeFragment(currentFragment); } break; case R.id.btn2: Toast.makeText(MainActivity.this, "btn2", Toast.LENGTH_SHORT).show(); if(currentFragment instanceof U3dFragment || currentFragment instanceof MenuOneFragment){ currentFragment = menuTwoFragment; changeFragment(currentFragment); }else{ currentFragment = u3dFragment; changeFragment(currentFragment); } break; } } /** * 方法用來改變界面 */ private void changeFragment(Fragment fragment){ FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.content, fragment); ft.commit(); } protected void onDestroy () { Constants.mUnityPlayer.quit(); super.onDestroy(); } // onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume. protected void onPause() { super.onPause(); Constants.mUnityPlayer.pause(); } protected void onResume() { super.onResume(); Constants.mUnityPlayer.resume(); } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Constants.mUnityPlayer.configurationChanged(newConfig); } public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Constants.mUnityPlayer.windowFocusChanged(hasFocus); } public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return Constants.mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event); return super.dispatchKeyEvent(event); } }上面是Activity的寫法。
/** * 展示u3d的界面 * @author gavin * */ public class U3dFragment extends Fragment { private Activity context; View playerView; @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); context = activity; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub playerView = Constants.mUnityPlayer.getView(); LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); playerView.setLayoutParams(lp); if(playerView.getParent() != null){ ((ViewGroup)playerView.getParent()).removeAllViews(); } return playerView; } @Override public void onDestroy() { // TODO Auto-generated method stub // Constants.mUnityPlayer.quit(); super.onDestroy(); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); Constants.mUnityPlayer.pause(); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); Constants.mUnityPlayer.resume(); } }上面是展示U3d視圖的Fragment的寫法。 要注意的有以下幾點: 1.UnityPlayer這個對象被定義為了靜態變量,在MainActivity中進行初始化,在Fragment中進行使用,用來獲取Unity3d的view。 2.在U3dFragment中沒有在onDestroy中,調用quit()方法。 3.在U3dFragment中的onCreateView()方法中,進行了下面的處理。
((ViewGroup)playerView.getParent()).removeAllViews();
對於上面三個問題,如果不處理會相應的引發下面三個問題。 1.展示U3d視圖的Fragment無法顯示Unity3d的動畫效果。 2.切換頁面的時候會報下面的錯誤: 10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeVideoFrameCallback:VILII
很多人寫文章,喜歡把什麼行業現狀啊,研究現狀啊什麼的寫了一大通,感覺好像在寫畢業論文似的,我這不廢話,先直接上幾個圖,感受一下。第一張圖是在把代碼注入到地圖裡面,啟動首頁
鑒於谷歌最新推出的Android Studio備受開發者的推崇,所以也跟著體驗一下。一、介紹Android Studio Android Studio 是一個A
Launcher3壁紙的規格默認是:壁紙高度=屏幕高度,壁紙寬帶=屏幕寬度*2倍 Android4.4的壁紙信息存放在/data/system/users/0/目錄下,W
二維碼掃描 Android Zxing圖片拉伸解決 Zxing是google提供的二維碼掃描工程 默認是橫屏的 轉換成豎屏後圖片出現拉伸 這裡提供解決辦法: Zxi