android与fragment交互,fragment显示unity3d视图。
/** * 主界面 * @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的写法。
((ViewGroup)playerView.getParent()).removeAllViews();
对于上面三个问题,如果不处理会相应的引发下面三个问题。
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSoftInputClosed:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetTouchDeltaY:VF
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputString:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputCanceled:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetExtras:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetDefaultDisplay:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResume:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResize:VIIII
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequestedAA:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequested32bitDisplayBuffer:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRender:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRecreateGfxState:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativePause:Z
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeKeysPressed:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInjectEvent:ZL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInitWWW:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFocusChanged:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFile:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDone:V
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeActivityIndicatorStyle:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.initJni:VL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.UnitySendMessage:VLLL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocationStatus:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocation:VFFFFDF
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeForwardEventsToDalvik:VZ
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDeviceOrientation:VI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodUnblockStreaming:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcess:IL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodInitJni:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodGetInfo:II
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodBlockStreaming:I
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcessMicData:ILI
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyInvoke:LILL
10-16 13:26:44.927: D/dalvikvm(8170): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyFinalize:VI
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。