简体   繁体   中英

How to bring SDLSurfaceView to front when placed inside a layout?

I'm using a FrameLayout to hold two views, one on top of the other. One is an AdView and the other is a SDLSurfaceView: https://github.com/kivy/python-for-android/blob/master/src/src/org/renpy/android/SDLSurfaceView.java

Basically I want the AdView to be placed in front of the SDLSurfaceView but only when an actual Ad is being displayed. The ad loads after about 30 seconds after starting the app but if I click on where the ad should be when no ad is displayed, it still opens up a link. How do I keep the SDLSurfaceView in front of the AdView until the AdView actually loads?

Here is a sample of what I have so far:

mView = new SDLSurfaceView(
            this,
            mPath.getAbsolutePath());
Hardware.view = mView;

//CREATE WIDGETS LAYOUT PARAMETERS
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.WRAP_CONTENT,
    FrameLayout.LayoutParams.WRAP_CONTENT,
    Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);

//CREATE ADVIEW
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(AdSize.BANNER);

//ADD LAYOUT PARAMETER TO ADVIEW
adView.setLayoutParams(lp);

//SET AD LISTENER
adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adView.bringToFront();
            adView.invalidate();
            mView.invalidate();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            adView.getParent().bringChildToFront(mView);
            mView.invalidate();
            adView.invalidate();
        }
    });

//CREATE LAYOUT
FrameLayout layout = new FrameLayout(this);
//SET CONTENT TO LAYOUT
setContentView(layout);
//ADD VIEWS TO LAYOUT
layout.addView(adView);
layout.addView(mView);
//BUILD AD REQUEST
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build();
adView.loadAd(adRequest);

I am using kivy to create an adroid app and python-for-android to package the apk. The sample is part of my PythonActivity.java used by the python-for-android packager.

You can show the ad when it's loaded and remove from the layout when it isn't loaded or displayed. In this case, you'd have to place the adview on top of the SDLSurfaceView and set its visibility modes:

I would do it this way:

mView = new SDLSurfaceView(
            this,
            mPath.getAbsolutePath());
Hardware.view = mView;

//CREATE WIDGETS LAYOUT PARAMETERS
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.WRAP_CONTENT,
    FrameLayout.LayoutParams.WRAP_CONTENT,
    Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);

//CREATE ADVIEW
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(AdSize.BANNER);
adView.setVisibility(View.GONE);

//ADD LAYOUT PARAMETER TO ADVIEW
adView.setLayoutParams(lp);

//SET AD LISTENER
adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adView.setVisibility(View.VISIBLE);

        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            adView.setVisibility(View.GONE);

        }
    });

//CREATE LAYOUT
FrameLayout layout = new FrameLayout(this);
//SET CONTENT TO LAYOUT
setContentView(layout);
//ADD VIEWS TO LAYOUT
layout.addView(mView);
layout.addView(adView);
//BUILD AD REQUEST
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build();
adView.loadAd(adRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM