实现地图地球Rajawali成片段Android

时间:2016-03-17 15:47:44

标签: android fragment rajawali

我在片段android上合并世界地图时遇到了问题 由于以这种方式执行活动:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|
| 1 | S |   |   | S | S | S |   | S |   |   |
| 2 | S |   |   |   |   |   |   | S |   |   |
| 3 |   | S |   |   |   |   |   |   |   |   |
| 4 |   | S |   | S |   |   |   |   |   |   |
| 5 | S |   |   | S |   |   | S |   | S |   |
| 6 | S |   |   | S |   |   | S |   | S |   |
| 7 | S |   |   | S |   |   | S |   | S |   |
| 8 | S |   |   |   |   |   | S |   |   | S |
| 9 |   |   |   |   |   |   | S |   |   | S |
| 10|   |   |   | S | S | S |   |   |   |   |

但是当我想在片段中做到这一点时,我会给出错误行

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final RajawaliSurfaceView surface = new RajawaliSurfaceView(this);
    surface.setFrameRate(60.0);
    surface.setRenderMode(IRajawaliSurface.RENDERMODE_CONTINUOUSLY);

    // Add mSurface to your root view
    addContentView(surface, new  ActionB

ar.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT));

    renderer = new Renderer(this);
    surface.setSurfaceRenderer(renderer);
}

1 个答案:

答案 0 :(得分:4)

嗯,我认为你误解了片段和活动之间的区别,这个错误信息:

  

找不到符号方法addContentView()

指出这种方法不存在!

这是如何将Rajawali3D地图加载到Android片段的一个非常基本的示例。

enter image description here

创建Activity,添加Fragment事务,将片段加载到map_fragment容器中,在act_frag_map布局中定义:

public class Map3DFragActivity extends AppCompatActivity {

    public RajawaliSurfaceView rajawaliTexture;
    Renderer renderer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_frag_map);

        setFragment();

    }

    protected void setFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment mapFragment = new FragmentMap();
        FragmentTransaction ft =
                fragmentManager.beginTransaction();
        ft.add(R.id.map_fragment, mapFragment).commit();
    }

}

这是包含片段的布局,在属性android:name中添加了我们的类片段的名称:

<强> act_frag_map

<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"
    tools:context=".FragmentExampleActivity" >

    <fragment
        android:id="@+id/map_fragment"
        android:name="com.test.FragmentMap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
         />
</RelativeLayout>

这是创建地图的片段!

FragmentMap

import org.rajawali3d.surface.RajawaliSurfaceView;

public class FragmentMap extends Fragment {

    public RajawaliSurfaceView rajawaliTexture;
    Renderer renderer;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_frag_map,
                container, false);

        rajawaliTexture = (RajawaliSurfaceView) view.findViewById(R.id.rajawali_surface);
        renderer = new Renderer(getActivity());
        rajawaliTexture.setSurfaceRenderer(renderer);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // make sure this line exists

    }
}

<强> activity_frag_map

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:surfaceview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <org.rajawali3d.surface.RajawaliSurfaceView
        android:id="@+id/rajawali_surface"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        surfaceview:frameRate="60.0"
        surfaceview:renderMode="RENDER_WHEN_DIRTY"/>

</FrameLayout>