0

ゲームに 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);
        }
            .....
}

問題は、ourSurfaceView をレイアウト上の surfaceview に「接続」して描画に使用できないことです。

4

1 に答える 1

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 を AdViewew の下にあるものとしてマークするか、RelativeLayout の代わりに LinearLayout を使用し、SurfaceView に layout_weight=1 を使用して未使用のスペースを埋めるように拡張します。

于 2013-07-17T22:09:33.760 に答える