2013-10-23 80 views
0

我正在使用cocos2d-x在android中開發遊戲。這是我的要求。cocos2d-x如何在同一屏幕中添加Videoview和Cocos2dxGlsurfaceview?

  1. 30%的屏幕將被VideoView覆蓋。
  2. 70%的屏幕將覆蓋Cocos2dxGlsurfaceview。

我已經創建了這個xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" > 

    <VideoView 
     android:id="@+id/videoPlayer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="7" /> 

    <FrameLayout 
     android:id="@+id/frmGame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="3" 
     android:background="@android:color/transparent" > 
    </FrameLayout> 

</LinearLayout> 

這是Cocos2dxActivity.java其中我編輯init()方法來添加VideoView。

package org.cocos2dx.lib; 

import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Message; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.FrameLayout; 
import android.widget.VideoView; 

public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener { 
    // =========================================================== 
    // Constants 
    // =========================================================== 

    private static final String TAG = Cocos2dxActivity.class.getSimpleName(); 

    // =========================================================== 
    // Fields 
    // =========================================================== 

    private Cocos2dxGLSurfaceView mGLSurfaceView; 
    private Cocos2dxHandler mHandler; 
    private static Context sContext = null; 
    private FrameLayout frmGame; 
    private VideoView videoPlayer; 

    public static Context getContext() { 
     return sContext; 
    } 

    // =========================================================== 
    // Constructors 
    // =========================================================== 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     sContext = this; 
     this.mHandler = new Cocos2dxHandler(this); 

     this.init(); 

     Cocos2dxHelper.init(this, this); 
    } 

    @Override 
    public void runOnGLThread(final Runnable pRunnable) { 
     this.mGLSurfaceView.queueEvent(pRunnable); 
    } 

    // =========================================================== 
    // Methods 
    // =========================================================== 
    public void init() { 

     View v = LayoutInflater.from(this).inflate(R.layout.activity_cocos2dx_video1, null); 

     frmGame = (FrameLayout) v.findViewById(R.id.frmGame); 
     videoPlayer = (VideoView) v.findViewById(R.id.videoPlayer); 


     // FrameLayout 
     ViewGroup.LayoutParams framelayout_params = 
      new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
             ViewGroup.LayoutParams.FILL_PARENT); 
     FrameLayout framelayout = new FrameLayout(this); 
     framelayout.setLayoutParams(framelayout_params); 

     // Cocos2dxEditText layout 
     ViewGroup.LayoutParams edittext_layout_params = 
      new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
             ViewGroup.LayoutParams.WRAP_CONTENT); 
     Cocos2dxEditText edittext = new Cocos2dxEditText(this); 
     edittext.setLayoutParams(edittext_layout_params); 

     // ...add to FrameLayout 
     framelayout.addView(edittext); 

     // Cocos2dxGLSurfaceView 
     this.mGLSurfaceView = this.onCreateView(); 

     // ...add to FrameLayout 
     framelayout.addView(this.mGLSurfaceView); 

     // Switch to supported OpenGL (ARGB888) mode on emulator 
     if (isAndroidEmulator()) 
      this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); 

     this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer()); 
     this.mGLSurfaceView.setCocos2dxEditText(edittext); 

     // Set framelayout as the content view 
     frmGame.addView(framelayout,framelayout_params); 
     setContentView(frmGame); 
    } 

    public Cocos2dxGLSurfaceView onCreateView() { 
     return new Cocos2dxGLSurfaceView(this); 
    } 

    private final static boolean isAndroidEmulator() { 
     String model = Build.MODEL; 
     Log.d(TAG, "model=" + model); 
     String product = Build.PRODUCT; 
     Log.d(TAG, "product=" + product); 
     boolean isEmulator = false; 
     if (product != null) { 
     isEmulator = product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_"); 
     } 
     Log.d(TAG, "isEmulator=" + isEmulator); 
     return isEmulator; 
    } 

    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 
} 

但是,當我試圖把VideoView與Cocos2dxGlsurfaceview應用程序在init()方法崩潰。

錯誤:孩子已經有了一個parent.you必須先調用removeview()。

請幫我看看我做錯了什麼。

回答

1
View v = LayoutInflater.from(this).inflate(R.layout.activity_cocos2dx_video1, null); 

此代碼是關鍵點。

從這個document,我們可以知道返回的View是根視圖,也就是說,這個vfrmGame的父視圖。

所以,解決你的問題,你可以用

setContentView(v); 
+0

感謝的人取代

setContentView(frmGame); 

追趕我的愚蠢的錯誤。這被稱爲沮喪的高度。 –

+0

不客氣:) – pktangyue

+0

@ pktangyue還有一個問題我想知道我的CCLayer的高度和寬度以及CCLyear的來源是否對此有任何想法? –

相關問題