admob广告横幅重叠我的游戏屏幕android

时间:2015-10-11 23:20:59

标签: java android google-play admob google-play-services

我成功整合了admob广告,但我有一个问题就是在游戏画面下放置横幅广告而不重叠我的游戏视图我当前的问题就像图片Pic 1我希望它像这样图片 Pic2

注意:我正在使用java admob代码这是我的代码

package com.xxxx.xxxxx;
import org.cocos2d.actions.CCActionManager;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.ideastudio.incroyableAventuresAlaadin.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

public class xxxxxx extends Activity{
/** Called when the activity is first created. */

public static xxxxx app;

public static CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false; 

private static final String AD_UNIT_ID = "ca-app-pub-xxxxxxxx";
private static final String AD_INTERSTITIAL_UNIT_ID = "ca-app-pub-xxxxx";

/** The interstitial ad. */
private InterstitialAd interstitialAd;
private AdView adView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    if( !isCreated ){
        isCreated = true;
    } else {
        return;
    }

    app = this;

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


    mGLSurfaceView = new CCGLSurfaceView(this);


    // Create the adView
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the adView to it
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM ,RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    adView.setLayoutParams(params);

    layout.addView(mGLSurfaceView);
    layout.addView(adView);

    setContentView(layout);


    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

    //-----------------------------------------------------Interstitial Add
    // Create an Interstitial ad.
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
     // Load the interstitial ad.
    AdRequest interstitialAdRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(interstitialAdRequest);

    /////////////////////////////////////////////////////////////////////////////
    Common.game_initialize();
    getScaledCoordinate();
    CCDirector.sharedDirector().attachInView(mGLSurfaceView); 

    // attach the OpenGL view to a window
    Common.sound_engine = SoundEngine.sharedEngine();
    loadSound();

    CCScene scene = CCScene.node();
    scene.addChild(new HelloWorldLayer(), 1);
    CCDirector.sharedDirector().runWithScene(scene);
}

@Override
public void onStart() {
    super.onStart();
}

@Override    
public void onPause() {
    if (adView != null) {
          adView.pause();
        }


    super.onPause();

    MediaGlobal._shared().pauseMusic();

    if(GameLayer.sharedGameLayer() != null){
        GameLayer.sharedGameLayer().onPause(null);
    }
    CCDirector.sharedDirector().pause();
}

@Override
public void onResume() {
    super.onResume();

    if (adView != null) {
        adView.resume();
      }

    MediaGlobal._shared().resumeMusic();
}

@Override
public void onDestroy() {

    if (adView != null) {
          adView.destroy();
        }


    isCreated = false;

    MediaGlobal._shared().stopMusic();
    Common.sound_engine.realesAllEffects();

    super.onDestroy();
    CCDirector.sharedDirector().end();       

    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCSpriteFrameCache.sharedSpriteFrameCache().removeAllSpriteFrames();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        exitGameDialog();
        return false;
    }
    return super.onKeyDown(keyCode, event);
}

public void exitGameDialog() {
    Builder builder = new AlertDialog.Builder(FlyingPanda.this)
            .setIcon(R.drawable.icon)
            .setTitle("Quitter le jeu?")
            .setMessage("Est-vous sûr?")
            .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .setPositiveButton("Oui",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            CCActionManager.sharedManager()
                                    .removeAllActions();
                            CCDirector.sharedDirector().end();
                            finish();
                        }
                    });
    builder.create().show();
}

private void loadSound() {
    SoundEngine.purgeSharedEngine();

    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bomb);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bounce);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.death);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.fly);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gamebg);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gameover);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.jumppad);
}

private void getScaledCoordinate() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    Common.SCREEN_WIDTH = displayMetrics.widthPixels;
    Common.SCREEN_HEIGHT = displayMetrics.heightPixels;
    Common.kXForIPhone = Common.SCREEN_WIDTH / 480.0f;
    Common.kYForIPhone = Common.SCREEN_HEIGHT / 320.0f;
}


// Admob Setting
////////////////////////////////////////////////////////////////////////////////

public void setHideAdView(final boolean bHide) {
    runOnUiThread(new Runnable() {
        public void run() {
            if(bHide) {
                adView.setVisibility(View.INVISIBLE);
            }
            else {
                adView.setVisibility(View.VISIBLE);
            }
        }
    });


}

public void showInterstitialAds()
{
    runOnUiThread(new Runnable() {
        public void run() {

            // Load the interstitial ad.
            AdRequest interstitialAdRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(interstitialAdRequest);

            interstitialAd.show();
        }
    });
}

  }

1 个答案:

答案 0 :(得分:0)

您需要为广告视图添加规则,将其设置为"以下" gl表面视图。

int id = 173; //random number
mGLSurfaceView.setId(id);
params.addRule(RelativeLayout.BELOW, id);

如果使用API​​级别17+,您可以使用:

View.generateViewId()

或者在xml中保留该号码。