通过libGDX / RoboVM控制AdMob Banner在iOS中的定位

时间:2014-10-19 18:40:04

标签: ios eclipse libgdx admob robovm

我创建了我的libGDX iOS项目,并且我试图将我的AdMob广告定位到屏幕的底部中心,但不知道如何完成此操作。我通过RoboVM使用绑定,并且不知道任何RoboVM方法来控制我的广告。我从here复制了教程。有没有人有任何提示或教程来帮助我完成这项工作?现在广告似乎让我失去了整个横幅广告的1/4更靠近屏幕右侧。以下是我的代码:

public class IOSLauncher extends IOSApplication.Delegate implements IActivityRequestHandler{
private static final Logger log = new Logger(IOSLauncher.class.getName(), Application.LOG_DEBUG);
private static final boolean USE_TEST_DEVICES = true;
private GADBannerView adview;
private boolean adsInitialized = false;
private IOSApplication iosApplication;

@Override
protected IOSApplication createApplication() {
    IOSApplicationConfiguration config = new IOSApplicationConfiguration();
    config.orientationLandscape = true;
    config.orientationPortrait = false;

    iosApplication = new IOSApplication(new TestProject(this), config);
    return iosApplication;
}

public static void main(String[] argv) {
    NSAutoreleasePool pool = new NSAutoreleasePool();
    UIApplication.main(argv, null, IOSLauncher.class);
    pool.close();
}

@Override
public void hide() {
    initializeAds();

    final CGSize screenSize = UIScreen.getMainScreen().getBounds().size();
    double screenWidth = screenSize.width();

    final CGSize adSize = adview.getBounds().size();
    double adWidth = adSize.width();
    double adHeight = adSize.height();

    log.debug(String.format("Hidding ad. size[%s, %s]", adWidth, adHeight));

    float bannerWidth = (float) screenWidth;
    float bannerHeight = (float) (bannerWidth / adWidth * adHeight);

    adview.setFrame(new CGRect(0, -bannerHeight, bannerWidth, bannerHeight));
}

@Override
public void show() {
    initializeAds();

    final CGSize screenSize = UIScreen.getMainScreen().getBounds().size();
    double screenWidth = screenSize.width();

    final CGSize adSize = adview.getBounds().size();
    double adWidth = adSize.width();
    double adHeight = adSize.height();

    log.debug(String.format("Showing ad. size[%s, %s]", adWidth, adHeight));

    float bannerWidth = (float) screenWidth;
    float bannerHeight = (float) (bannerWidth / adWidth * adHeight);

    adview.setFrame(new CGRect((screenWidth / 2) - adWidth / 2, 0, bannerWidth, bannerHeight));
}

public void initializeAds() {
    if (!adsInitialized) {
        log.debug("Initalizing ads...");

        adsInitialized = true;

        adview = new GADBannerView(GADAdSize.banner());
        adview.setAdUnitID(Constants.AdUnitID); //put your secret key here
        adview.setRootViewController(iosApplication.getUIViewController());


        iosApplication.getUIViewController().getView().addSubview(adview);

        final GADRequest request = GADRequest.create();

        adview.setDelegate(new GADBannerViewDelegateAdapter() {
            @Override
            public void didReceiveAd(GADBannerView view) {
                super.didReceiveAd(view);
                log.debug("didReceiveAd");
            }

            @Override
            public void didFailToReceiveAd(GADBannerView view,
                    GADRequestError error) {
                super.didFailToReceiveAd(view, error);
                log.debug("didFailToReceiveAd:" + error);
            }
        });

        adview.loadRequest(request);

        log.debug("Initalizing ads complete.");
    }
}

@Override
public void showAds(boolean show) {
    initializeAds();

    final CGSize screenSize = UIScreen.getMainScreen().getBounds().size();
    double screenWidth = screenSize.width();

    final CGSize adSize = adview.getBounds().size();
    double adWidth = adSize.width();
    double adHeight = adSize.height();

    log.debug(String.format("Hidding ad. size[%s, %s]", adWidth, adHeight));

    float bannerWidth = (float) screenWidth;
    float bannerHeight = (float) (bannerWidth / adWidth * adHeight);

    if(show) {
        adview.setFrame(new CGRect((screenWidth / 2) - adWidth / 2, 0, bannerWidth, bannerHeight));
    } else {
        adview.setFrame(new CGRect(0, -bannerHeight, bannerWidth, bannerHeight));
    }

1 个答案:

答案 0 :(得分:1)

您使用

设置广告排名
adview.setFrame(CGRect);

如果您检查 CGRect 的参数,就像;

CGRect(double x, double y, double width, double height)

0,0坐标(x,y)位于左上角。所以,你的代码;

// center of screen
double adX = (screenWidth / 2) - (adWidth / 2);
// bottom of screen
double adY = screenHeight - bannerHeight;

adview.setFrame(new CGRect(adX, adY, bannerWidth, bannerHeight));

而另一件重要的事情,你不应该在两种方法中操纵定位!您的 showAds 方法应该是;

public void showAds(boolean show) {
    if (show) {
        show();
    } else {
        hide();
    }
}