1

我有一个很大的问题要问,因为我真的被困在这个问题上,它会被创建来在我的免费应用程序上获取广告,好的,首先我一直在关注这本书

开始 Android 游戏 2011

http://www.apress.com/9781430230427

现在这本书实现了一个非常漂亮和简单的游戏框架,我使用它(一个更简单的版本可以在这里找到

http://www.kilobolt.com/day-5-the-android-game-framework-part-i.html

现在这个框架不再使用任何类型的 XML 文件,它使用帧缓冲区将内容绘制到屏幕上。现在当应用程序第一次启动时,这是在 AndroidGame.java 中调用的第一个方法

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 480: 800;
    int frameBufferHeight = isPortrait ? 800: 480;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth
            / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight
            / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen();

    setContentView(renderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
}

我可以在哪里尝试实施 admob 横幅?这也是屏幕类的样子

public LogoScreen(Game game)
{
    super(game);
}

@Override
public void update(float deltaTime)
{
    Graphics g = game.getGraphics();
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();

    for(int i = 0; i < len; i ++)
    {
        try
        {
            TouchEvent event = (TouchEvent) touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_DOWN)
            {
                game.setScreen(new MainMenuScreen(game));
            }
        }
        catch(IndexOutOfBoundsException io)
        {

        }
    }

}

@Override
public void paint(float deltaTime) 
{
    Graphics g = game.getGraphics();
    g.drawImage(Assets.logoScreen, 0, 0);
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() 
{
    android.os.Process.killProcess(android.os.Process.myPid());
}

如果我想在 logoScreen 中显示 admob 广告,我该怎么做?我真的很困惑如何在我的应用程序中实现 admob,如果有人能对此有所了解或帮助我,那就太好了:)

谢谢

帆布

---更新--- 这是 FastRenderView 的代码

package com.CC.framework.implementation;

//Imports
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AndroidFastRenderView extends SurfaceView implements Runnable 
{
//Variables
AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;

public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) 
{
    super(game);
    this.game = game;
    this.framebuffer = framebuffer;
    this.holder = getHolder();

}

public void resume() 
{ 
    running = true;
    renderThread = new Thread(this);
    renderThread.start();
}      

public void run() 
{
    Rect dstRect = new Rect();
    long startTime = System.nanoTime();
    while(running) 
    {  
        if(!holder.getSurface().isValid())
        {
            continue;           
        }

        float deltaTime = (System.nanoTime() - startTime) / 10000000.000f;
        startTime = System.nanoTime();

        if (deltaTime > 3.15)
        {
            deltaTime = (float) 3.15;
        }


        game.getCurrentScreen().update(deltaTime);
        game.getCurrentScreen().paint(deltaTime);

        Canvas canvas = holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(framebuffer, null, dstRect, null);                           
        holder.unlockCanvasAndPost(canvas);

    }
}

public void pause() 
{                        
    running = false;                        
    while(true) 
    {
        try 
        {
            renderThread.join();
            break;
        } 
        catch (InterruptedException e) 
        {
            // retry
        }

    }
}     


}
4

2 回答 2

1

创建一个布局容器并将 AdView 和 renderView 放入其中:

RelativeLayout layout = new RelativeLayout(this);
AdView adView = new AdView(this, AdSize.BANNER, "a151bf25136cf46");
layout.addView(renderView);
layout.addView(adView);
setContentView(layout);
adView.loadAd(new AdRequest());

编辑 2020:

上面的代码已经有 7 年的历史了,从那时起 Google 移动广告 SDK 发生了很大变化。这是针对当前最新 SDK 版本的最小代码示例。

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.loadAd(new AdRequest.Builder().build());
setContentView(layout);
于 2013-06-18T15:49:44.510 回答
0

我昨晚试了一下,结果很酷,请在Oncreate()您的活动中查看我的代码:

 LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setGravity(Gravity.BOTTOM|Gravity.CENTER);

    // Create a banner ad
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");



    // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");



    /*For Test On Real Device*/
    AdRequest adRequest=  new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);



    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setId(R.id.linear);
    linearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER);


    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 9f);
    layoutParams1.gravity = Gravity.TOP;
    view.setLayoutParams(layoutParams1);

    int heightPixels = AdSize.FULL_BANNER.getHeightInPixels(this);

    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightPixels, 0f);
    layoutParams2.gravity = Gravity.BOTTOM;
    mAdView.setLayoutParams(layoutParams2);

    layout.addView(view);
    layout.addView(mAdView);


    // Start loading the ad.
    setContentView(layout);

这将向您显示底部的广告,GL ;)

于 2019-08-16T07:58:21.880 回答