0

So heres the deal. I am making a game in andEngine and I use CropResoltionpolicy (how it works: http://android.kul.is/2013/10/andengine-tutorial-dealing-with-screen-sizes.html). I have engine camera set as 480x800 in horizontal orientation. but the devices resolution is 1920x1080. In my game top and bottom parts of the screen are overflowing devices screen (see the previous link, how red rectangles are overflowing on left and right of the screen). So when I add banner that starts directly on top (aligned to the top of the main game activity layout) it is partially hiddden. I can easly move banner a bit lower just by using '.setY()' but that does not work properly on other devices with different screens. So what I'm looking for is a method to set the banner always on top or bottom of the devices screen, not game view/layout because they are not the same.

My code that I use to show ads:

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

    setContentView(R.layout.activity_main);

    final DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);


    this.adView = new AdView(this);
    this.adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
    this.adView.setAdUnitId("myAdId");
    com.google.android.gms.ads.AdRequest adrequest = new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR).build();
    this.adView.setY(displayMetrics.heightPixels - this.adView.getHeight()); // here I set position of my banner, but it does not work properly

    this.adView.loadAd(adrequest);

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    layout.addView(this.mRenderSurfaceView);

    layout.addView(this.adView, params);

}

My main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_gravity="center"
android:gravity="center"

tools:context="com.hsdev.salesrep.MainActivity" >

</RelativeLayout>
4

1 回答 1

0

如果您想得足够多,看起来答案很容易:

final DisplayMetrics displayMetrics = new DisplayMetrics();
this.adView.setY(displayMetrics.heightPixels - (this.adView.getHeight() * (displayMetrics.heightPixels / 480) ) - 50);

其中 480 是 andEngine 中相机的高度,50 是横幅的高度。

于 2015-03-30T11:26:34.940 回答