Android AdMob without XML

2019-02-28 23:11发布

问题:

I have a massive question to ask as I am really stuck on this and it would be create to get ads on my free application, ok first off I have been following this book

Beginning Android games 2011

http://www.apress.com/9781430230427

Now this book implements a very nice and simple game framework which I use (a simpler version can be found here

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

Now this framework doesn't use any type of XML file what so ever, it uses a framebuffer to draw things onto the screen. now when the application is first started, this is the first method called which is in the 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");
}

Where in here could i try and implement the admob banner? Also this is what a screen class looks like

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

If I wanted to display an admob advert in the logoScreen what could i do that would work? I am really confused on how I can implement admob into my application, if any one can shine some light on this or help me that would be great :)

Thank you

Canvas

---Update--- Here is the code for the 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
        }

    }
}     


}

回答1:

Create a layout container and put the AdView and the renderView in it:

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