Android的AdMob的无XML [英] Android AdMob without XML

查看:128
本文介绍了Android的AdMob的无XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的问题要问,因为我真的停留在这一点,它会创建让我的免费应用的广告,好第一关我一直在这本书以下

开始的Andr​​oid游戏2011

  HTTP://www.a$p$pss.com/9781430230427
 

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

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

现在这个框架并没有使用什么那么,它使用的framebuffer画的东西在屏幕上的任何类型的XML文件。现在,当该应用程序第一次启动,这是第一个方法调用这是在AndroidGame.java

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

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

    布尔isPortrait = getResources()getConfiguration()==方向Configuration.ORIENTATION_PORTRAIT。;
    INT frameBufferWidth = isPortrait? 480:800;
    INT frameBufferHeight = isPortrait? 800:480;
    位图帧缓冲= Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight,Config.RGB_565);

    浮动的scaleX =(浮点)frameBufferWidth
            / getWindowManager()getDefaultDisplay()的getWidth()。
    浮动的scaleY =(浮点)frameBufferHeight
            / getWindowManager()getDefaultDisplay()的getHeight()。

    的RenderView =新AndroidFastRenderView(这一点,帧缓冲);
    显卡=新AndroidGraphics(getAssets(),帧缓冲);
    FILEIO =新AndroidFileIO(本);
    音频=新AndroidAudio(本);
    输入=新AndroidInput(这一点,的RenderView,将scaleX,的scaleY);
    屏幕= getInitScreen();

    的setContentView(的RenderView);

    电源管理器电源管理器=(电源管理器)getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCKMyGame);
}
 

我在哪里在这里可以尝试和实施AdMob的大旗?另外这是一个屏幕类的样子

 公共LOGOSCREEN(比赛游戏)
{
    超(游戏);
}

@覆盖
公共无效更新(浮动deltaTime)
{
    图形G = game.getGraphics();
    名单<的TouchEvent> touchEvents = game.getInput()getTouchEvents()。

    INT的len = touchEvents.size();

    的for(int i = 0; I< LEN;我++)
    {
        尝试
        {
            的TouchEvent事件=(的TouchEvent)touchEvents.get(我);
            如果(Event.type访问== TouchEvent.TOUCH_DOWN)
            {
                game.setScreen(新MainMenuScreen(游戏));
            }
        }
        赶上(IndexOutOfBoundsException异常IO)
        {

        }
    }

}

@覆盖
公共无效漆(浮动deltaTime)
{
    图形G = game.getGraphics();
    g.drawImage(Assets.logoScreen,0,0);
}

@覆盖
公共无效暂停(){
    // TODO自动生成方法存根

}

@覆盖
公共无效简历(){
    // TODO自动生成方法存根

}

@覆盖
公共无效的Dispose(){
    // TODO自动生成方法存根

}

@覆盖
公共无效点击返回按钮()
{
    android.os.Process.killProcess(android.os.Process.myPid());
}
 

如果我想显示在LOGOSCREEN的AdMob的广告我能做什么,将工作?我如何,我可以实现的AdMob到我的应用程序,如果任何人能照到一些轻在这个或帮助我,将是巨大真的很困惑:)

感谢您

画布

---更新--- 这里是code为FastRenderView

 包com.CC.framework.implementation;

//进口
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Rect;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;

公共类AndroidFastRenderView扩展了SurfaceView实现Runnable
{
//变量
AndroidGame游戏;
位图帧缓冲;
螺纹renderThread = NULL;
SurfaceHolder持有人;
挥发性布尔运行= FALSE;

公共AndroidFastRenderView(AndroidGame游戏,位图的帧缓冲)
{
    超(游戏);
    this.game =游戏;
    this.framebuffer =帧缓冲;
    this.holder = getHolder();

}

公共无效简历()
{
    运行=真;
    renderThread =新主题(本);
    renderThread.start();
}

公共无效的run()
{
    矩形dstRect =新的矩形();
    长的startTime = System.nanoTime();
    而(运行)
    {
        如果(!holder.getSurface()。isValid()的)
        {
            继续;
        }

        浮动deltaTime =(System.nanoTime() -  startTime时)/ 10000000.000f;
        的startTime = System.nanoTime();

        如果(deltaTime> 3.15)
        {
            deltaTime =(浮点)3.15;
        }


        。game.getCurrentScreen()更新(deltaTime);
        。game.getCurrentScreen()漆(deltaTime);

        帆布帆布= holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(帧缓冲,空,dstRect,NULL);
        holder.unlockCanvasAndPost(画布);

    }
}

公共无效暂停()
{
    运行= FALSE;
    而(真)
    {
        尝试
        {
            renderThread.join();
            打破;
        }
        赶上(InterruptedException的E)
        {
            //重试
        }

    }
}


}
 

解决方案

创建一个布局容器,并把AD浏览报和的RenderView它:

  RelativeLayout的布局=新RelativeLayout的(这一点);
AD浏览报AD浏览报=新的AD浏览报(这一点,AdSize.BANNER,a151bf25136cf46);
layout.addView(的RenderView);
layout.addView(AD浏览报);
的setContentView(布局);
adView.loadAd(新AdRequest());
 

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
        }

    }
}     


}

解决方案

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

这篇关于Android的AdMob的无XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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