AndEngine嵌入廣告

來源:互聯網
上載者:User

在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}}

搞定,運行項目就可以看到廣告效果了。

注意,必須要用真機才可以看到效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.