实施Admob的横幅时的setContentView()被用于Surfaceview [英] Implementing Admob banner when setContentView() is used for the Surfaceview

查看:312
本文介绍了实施Admob的横幅时的setContentView()被用于Surfaceview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力实现一个AdMob的横幅到我的应用程序,因为的setContentView()方法用于surfaceView称为gameView因此创建XML中的AD浏览报不能应用于此框架的setContentView已被使用。我不知道如何以编程方式做到这一点。有没有人有一个解决的办法?

我的主要活动:

 公共类GameMainActivity扩展BaseGameActivity {
....
 @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    例如=这一点;
    preFS = GET preferences(Activity.MODE_PRIVATE); //新行!
    高分= retrieveHighScore();
    highScoreUnits = retrieveHighScoreUnits();
    highScoreTens = retrieveHighScoreTens();
    highScoreHundreds = retrieveHighScoreHundreds();
    muteButton = retrieveMuteButton();
    资产= getAssets();
    sGame =新GameView(这一点,GAME_WIDTH,GAME_HEIGHT);
    的setContentView(sGame);
    。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}
 

和我的自定义surfaceView code

 公共类GameView扩展了SurfaceView实现Runnable {

私人位图gameImage;
私人矩形gameImageSrc;
私人矩形gameImageDst;
私人画布的GameCanvas;
私人画家显卡;

私人螺纹gameThread;
私人挥发性布尔运行= FALSE;
私人的不稳定状态,currentState的;

私人InputHandler inputHandler;


公共GameView(上下文的背景下,INT gameWidth,诠释gameHeight){
    超(上下文);
    gameImage = Bitmap.createBitmap(gameWidth,gameHeight,
            Bitmap.Config.RGB_565);
    gameImageSrc =新的Rect(0,0,gameImage.getWidth(),
            gameImage.getHeight());
    gameImageDst =新的矩形();
    在GameCanvas =新的Canvas(gameImage);
    显卡=新的画家(GameCanvas的);
    SurfaceHolder支架= getHolder();
    holder.addCallback(新的回调(){

        @覆盖
        公共无效surfaceCreated(SurfaceHolder持有者){
            initInput();
            如果(currentState的== NULL){
                setCurrentState(新LoadState的());
            }
            initGame();
        }

        @覆盖
        公共无效surfaceChanged(SurfaceHolder持有人,INT格式,
                                   INT宽度,高度INT){
            // TODO自动生成方法存根
        }

        @覆盖
        公共无效surfaceDestroyed(SurfaceHolder持有者){
            pauseGame();
        }

    });
}
 

解决方案

使用一个RelativeLayout的或的FrameLayout为你的父母的布局,然后只需在屏幕的底部中心定义布局参数AD浏览报被定位(例如像这样):

 公共类GameMainActivity扩展BaseGameActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        例如=这一点;
        preFS = GET preferences(Activity.MODE_PRIVATE); //新行!
        高分= retrieveHighScore();
        highScoreUnits = retrieveHighScoreUnits();
        highScoreTens = retrieveHighScoreTens();
        highScoreHundreds = retrieveHighScoreHundreds();
        muteButton = retrieveMuteButton();
        资产= getAssets();
        //创建一个广告。
        AD浏览报AD浏览报=新的AD浏览报(本);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        // AD浏览报的设置背景颜色,迫使它显示
        adView.setBackgroundColor(Color.TRANSPARENT);
        // AD浏览报添加到视图层次结构。该视图将没有大小
        //直到广告被加载。
        RelativeLayout的布局=新RelativeLayout的(这一点);
        layout.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        //创建一个广告请求。
        AdRequest adRequest =新AdRequest.Builder()建立()。

        //开始在后台加载的广告。
        adView.loadAd(adRequest);

        //请求全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //创建和设置您的游戏的看法
        sGame =新GameView(这一点,GAME_WIDTH,GAME_HEIGHT);


        RelativeLayout.LayoutParams adParams =新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(AD浏览报,adParams);
        的setContentView(布局);
        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}
 

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();
        }

    });
}

解决方案

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); 
    }
}

这篇关于实施Admob的横幅时的setContentView()被用于Surfaceview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆