直播技术之使用cocos2dx显示礼物
1.需要使用到cocos2dx显示礼物则需要继承Cocos2dxActivity
首次先Cocos2dxActivity->onCreate()->init()
public void init() {
// FrameLayout
ViewGroup.LayoutParams framelayout_params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mFrameLayout = new FrameLayout(this);
mFrameLayout.setLayoutParams(framelayout_params);
// Cocos2dxEditText layout
//ViewGroup.LayoutParams edittext_layout_params =
// new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT);
//Cocos2dxEditText edittext = new Cocos2dxEditText(this);
//edittext.setLayoutParams(edittext_layout_params);
// ...add to FrameLayout
//mFrameLayout.addView(edittext);
// Cocos2dxGLSurfaceView
this.mGLSurfaceView = this.onCreateView();
// ...add to FrameLayout
mFrameLayout.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
setContentView(mFrameLayout);
}
其实Cocos2dxActivity已经给我我们提示,需要将我们自定义的布局按照怎样的顺序添加到ContentView中(10-18行);
abstract public View createCustomView();
mFrameLayout.addView(createCustomView(), new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
所以只用在createCustomView()中实现自己的布局就可以。
2.使用自定义GLSurfaceView与GLSurfaceView.Renderer播放视频
render = new SurfaceView.Renderer(context);
render.setGlSurfaceView(this);
setEGLContextClientVersion(2); // 设置opengl es2 版本
this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderer(render); // 设置渲染
setRenderMode(RENDERMODE_WHEN_DIRTY); // 需要时渲
setZOrderOnTop(false);//重点,小心坑;在Cocos2dxActivity->onCreateView()->glSurfaceView.setZOrderOnTop(true);所以这里设置强制为false,是自定义GLSurfaceView在动画的下面
//Cocos2dxActivity->onCreateView()
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
//this line is need on some device if we specify an alpha bits
if(this.mGLContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
glSurfaceView.setZOrderOnTop(true);//
Cocos2dxEGLConfigChooser chooser = new Cocos2dxEGLConfigChooser(this.mGLContextAttrs);
glSurfaceView.setEGLConfigChooser(chooser);
return glSurfaceView;
}