您的位置:首页 > 移动开发 > Android开发

Android 游戏引擎libgdx 如何添加Admob、多盟类广告条

2012-08-25 13:21 411 查看
1、initialize方法与initializeForView方法

这是AndroidApplication中的两个方法,我们来看下源代码

public void initialize (ApplicationListener listener, boolean useGL2IfAvailable) {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useGL20 = useGL2IfAvailable;
initialize(listener, config);
}


public View initializeForView (ApplicationListener listener, boolean useGL2IfAvailable) {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useGL20 = useGL2IfAvailable;
return initializeForView(listener, config);
}


除了一个返回View,一个不返回外,似乎没有区别。

我们继续深入

public void initialize (ApplicationListener listener, AndroidApplicationConfiguration config) {
graphics = new AndroidGraphics(this, config, config.resolutionStrategy == null ? new FillResolutionStrategy()
: config.resolutionStrategy);
 4         input = new AndroidInput(this, graphics.view, config);
5         audio = new AndroidAudio(this);
6         files = new AndroidFiles(this.getAssets(), this.getFilesDir().getAbsolutePath());
7         this.listener = listener;
8         this.handler = new Handler();
9
10         Gdx.app = this;
11         Gdx.input = this.getInput();
12         Gdx.audio = this.getAudio();
13         Gdx.files = this.getFiles();
14         Gdx.graphics = this.getGraphics();

16         try {
17             requestWindowFeature(Window.FEATURE_NO_TITLE);
18         } catch (Exception ex) {
19             log("AndroidApplication", "Content already displayed, cannot request FEATURE_NO_TITLE", ex);
20         }
21         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
22         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
23         setContentView(graphics.getView(), createLayoutParams());
         createWakeLock(config);
     }


public View initializeForView (ApplicationListener listener, AndroidApplicationConfiguration config) {
graphics = new AndroidGraphics(this, config, config.resolutionStrategy == null ? new FillResolutionStrategy()
: config.resolutionStrategy);
 4         input = new AndroidInput(this, graphics.view, config);
5         audio = new AndroidAudio(this);
6         files = new AndroidFiles(this.getAssets(), this.getFilesDir().getAbsolutePath());
7         this.listener = listener;
8         this.handler = new Handler();
9
10         Gdx.app = this;
11         Gdx.input = this.getInput();
12         Gdx.audio = this.getAudio();
13         Gdx.files = this.getFiles();
14         Gdx.graphics = this.getGraphics();

createWakeLock(config);
         return graphics.getView();
}


绿色部分完全一样,区别在于红色部分。initialize已经帮我们完成了setContentView工作。

我们现在需要在setContentView之前加入广告视图。

2、添加广告视图

这里以多盟广告为例

// 广告视图,这里以多盟为例,关于多盟请看 http://www.domob.cn/ public DomobAdView mAdview320x50 = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize(new DemoGame(), false);

// 创建布局
RelativeLayout layout = new RelativeLayout(this);
// 设置窗口无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 去除强制屏幕装饰(如状态条)弹出设置
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// 创建libgdx视图
View gameView = initializeForView(new DemoGame(), false);
// 添加libgdx视图
layout.addView(gameView);

// 创建多盟视图
mAdview320x50 = new DomobAdView(this, "56OJyM1ouMGoaSnvCK",
DomobAdView.INLINE_SIZE_320X50);
// 设置布局属性
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// 添加多盟视图
layout.addView(mAdview320x50, adParams);
// 添加布局
setContentView(layout);
}


3、最后效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: