Implementing Admob banner when setContentView() is used for the Surfaceview

北城余情 提交于 2019-12-02 18:15:02

问题


I am struggling to implement an admob banner into my app because the setContentView() method is used for the surfaceView called gameView so creating the adView in xml cannot be applied to this framework as setContentView is already being used. And I don't know how to do this programmatically. Does anyone have a solution to this?

My main Activity:

public class GameMainActivity extends BaseGameActivity {
....
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
    prefs = getPreferences(Activity.MODE_PRIVATE); // New line!
    highScore = retrieveHighScore();
    highScoreUnits = retrieveHighScoreUnits();
    highScoreTens = retrieveHighScoreTens();
    highScoreHundreds = retrieveHighScoreHundreds();
    muteButton = retrieveMuteButton();
    assets = getAssets();
    sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT);
    setContentView(sGame);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}

and my custom surfaceView code

public class GameView extends SurfaceView implements Runnable {

private Bitmap gameImage;
private Rect gameImageSrc;
private Rect gameImageDst;
private Canvas gameCanvas;
private Painter graphics;

private Thread gameThread;
private volatile boolean running = false;
private volatile State currentState;

private InputHandler inputHandler;


public GameView(Context context, int gameWidth, int gameHeight) {
    super(context);
    gameImage = Bitmap.createBitmap(gameWidth, gameHeight,
            Bitmap.Config.RGB_565);
    gameImageSrc = new Rect(0, 0, gameImage.getWidth(),
            gameImage.getHeight());
    gameImageDst = new Rect();
    gameCanvas = new Canvas(gameImage);
    graphics = new Painter(gameCanvas);
    SurfaceHolder holder = getHolder();
    holder.addCallback(new Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            initInput();
            if (currentState == null) {
                setCurrentState(new LoadState());
            }
            initGame();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                                   int width, int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            pauseGame();
        }

    });
}

回答1:


Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned (for example at the bottom center of the screen like this):

public class GameMainActivity extends BaseGameActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        prefs = getPreferences(Activity.MODE_PRIVATE); // New line!
        highScore = retrieveHighScore();
        highScoreUnits = retrieveHighScoreUnits();
        highScoreTens = retrieveHighScoreTens();
        highScoreHundreds = retrieveHighScoreHundreds();
        muteButton = retrieveMuteButton();
        assets = getAssets();
        // Create an ad.
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        // set background color of adview to force it to show
        adView.setBackgroundColor(Color.TRANSPARENT);
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        // Create an ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        // Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Create and set your game's view
        sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT);


        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layout.addView(sGame);
        layout.addView(adView, adParams);
        setContentView(layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    }
}


来源:https://stackoverflow.com/questions/32573930/implementing-admob-banner-when-setcontentview-is-used-for-the-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!