【问题标题】:android admob on canvas画布上的 android admob
【发布时间】:2013-07-15 04:51:40
【问题描述】:

我正在尝试在我的游戏中添加一个 admob, 我正在使用表面视图来绘制我的画布, 所以这是主要布局

<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 与布局上的表面视图“连接”以将其用于绘图

【问题讨论】:

    标签: android android-canvas surfaceview


    【解决方案1】:

    您永远不会实例化 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" />
    

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

    还要注意,将 SurfaceView 的 layout_width 和 height 指定为 match_parent 将意味着 AdView 永远不会有任何空间。要么先指定 AdView 并将 SurfaceView 标记为在 AdVieww 之下,要么使用 LinearLayout 而不是 RelativeLayout 并为 SurfaceView 使用 layout_weight=1 以使其扩展以填充未使用的空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2012-08-04
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多