在画布上的android admob

时间:2013-07-16 20:17:52

标签: android android-canvas surfaceview

我想在我的游戏中添加一个admob, 我使用surfaceview绘制画布, 所以这是主要的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="a151e48ea219c98"
        android:visibility="visible" />
</RelativeLayout>

这是主要的活动

    MykSurface ourSurfaceView;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ourSurfaceView =  (MySurface) findViewById(R.id.surface);
}

这是我在表面上运行绘图画布的类

public class MySurface extends SurfaceView implements Runnable {
            .....
        @Override
        public void run() {
            // TODO Auto-generated method stub

            while (isRunning) {
                canvas.drawBitmap(bg, 0, 0, null);
        }
            .....
}

问题是我无法将theSurfaceView与布局上的surfaceview“连接”以用于绘制

1 个答案:

答案 0 :(得分:0)

你永远不会实例化MySurface。

修改activity_main.xml以显式实例化它:

<com.yourcompany.MySurface
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

或以编程方式将其添加到主活动的onCreate中。

另请注意,将SurfaceView的layout_width和height指定为match_parent将意味着AdView永远不会有任何空间。首先指定AdView并将SurfaceView标记为位于AdViuew下方或使用LinearLayout而不是RelativeLayout,并使用layout_weight = 1使SurfaceView扩展以填充未使用的空间。