在Android平台,大部分廣告的嵌入方法都是類似的。
這次我選擇哇棒廣告平台為例,說一下使用AndEngine引擎如何嵌入廣告。
首先AndroidManifest.xml檔案需要添加相關的許可權和內容,這些在廣告SDK內建的文章中都有描述,這裡就不寫了。
拷貝例子中res/values/attrs.xml檔案到項目對應的位置。
編輯項目的布局檔案,我的內容如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:adsdk="http://schemas.android.com/apk/libs/cn.wey.android" android:id="@+id/main_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.wooboo.adlib_android.WoobooAdView android:id="@+id/wooboobanner" android:layout_width="fill_parent" android:layout_height="wrap_content" adsdk:backgroundColor="#FF000000" adsdk:refreshInterval="40" adsdk:textColor="#FFFFFFFF" /> <org.andengine.opengl.view.RenderSurfaceView android:id="@+id/renderSurfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>
既然使用布局檔案,那麼遊戲的入口類就要整合自SimpleLayoutGameActivity
以我前面放的華容道遊戲為例,我的Huarong.java檔案內容如下:
public class Huarong extends SimpleLayoutGameActivity {public EngineOptions onCreateEngineOptions() {final Camera camera = new Camera(0, 0, Constants.CAMERA_WIDTH,Constants.CAMERA_HEIGHT);final EngineOptions engineOptions = new EngineOptions(true,ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(Constants.CAMERA_WIDTH, Constants.CAMERA_HEIGHT),camera);// enable sound & musicengineOptions.getAudioOptions().setNeedsMusic(true);engineOptions.getAudioOptions().setNeedsSound(true);return engineOptions;}@Overrideprotected void onCreateResources() {// andengine's engineConstants.mEngine = this.mEngine;// application contextConstants.mContext = this;// load game settingsSettings.load();// load pictures & soundAssets.load();}@Overrideprotected Scene onCreateScene() {return new MainMenuScene();}@Overrideprotected int getLayoutID() {return R.layout.activity_huarong; //返回布局檔案的資源id}@Overrideprotected int getRenderSurfaceViewID() {return R.id.renderSurfaceView; //返回布局檔案中RenderSurfaceView控制項的id}}
搞定,運行項目就可以看到廣告效果了。
注意,必須要用真機才可以看到效果。