Android 360全景開發(PanoramaGL)
阿新 • • 發佈:2019-02-09
<pre name="code" class="java">最近公司要求做室內的360全景開發,在網上搜了些資料,基本上都是給的官網連結,自己研究了下,其實把這個做好真挺難搞,下面主要根據官網的PanoramaGL來作介紹: 1,下載一個PanoramaGL的jar包,http://code.google.com/p/panoramagl-android/downloads/detail?name=PanoramaGL_0.2-beta.jar,然後匯入到專案中。 2,繼承PLView,獲取<span style="font-family: Arial, Helvetica, sans-serif;">當前根內容檢視,將其新增到360檢視中,然後在載入全景影象</span> public class MainActivity extends PLView { /* 下拉控制元件 */ private Spinner mPanoramaTypeSpinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListener(new PLViewListener() { @Override public void onDidClickHotspot(PLIView view, PLIHotspot hotspot, CGPoint screenPoint, PLPosition scene3DPoint) { Toast.makeText( view.getActivity().getApplicationContext(), String.format("You select the hotspot with ID %d", hotspot.getIdentifier()), Toast.LENGTH_SHORT) .show(); } }); } /** * @param contentView表示當前根內容檢視 * @return activity的根內容檢視 */ @Override protected View onContentViewCreated(View contentView) { // 載入佈局 ViewGroup mainView = (ViewGroup) this.getLayoutInflater().inflate( R.layout.activity_main, null); // 新增360檢視 mainView.addView(contentView, 0); // 下拉列表控制 mPanoramaTypeSpinner = (Spinner) mainView .findViewById(R.id.spinner_panorama_type); ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.panorama_types, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mPanoramaTypeSpinner.setAdapter(adapter); mPanoramaTypeSpinner .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { loadPanoramaFromJSON(position); } @Override public void onNothingSelected(AdapterView<?> parentView) { } }); return super.onContentViewCreated(mainView); } /** * 載入全景影象 * * @param index表示的位置 * 0 = 立方體, 1 =球體 , 2 = 球體, 3 = 圓柱體 */ @SuppressWarnings("unused") private void loadPanorama(int index) { try { Context context = this.getApplicationContext(); PLIPanorama panorama = null; // 鎖定全景檢視 this.setLocked(true); // 全景全景檢視 switch (index) { // 立方體全景 (supports up 1024x1024 image per face) case 0: PLCubicPanorama cubicPanorama = new PLCubicPanorama(); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_f1), false), PLCubeFaceOrientation.PLCubeFaceOrientationFront); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_b1), false), PLCubeFaceOrientation.PLCubeFaceOrientationBack); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_l1), false), PLCubeFaceOrientation.PLCubeFaceOrientationLeft); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_r1), false), PLCubeFaceOrientation.PLCubeFaceOrientationRight); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_u1), false), PLCubeFaceOrientation.PLCubeFaceOrientationUp); cubicPanorama.setImage( new PLImage(PLUtils.getBitmap(context, R.raw.panorama_d1), false), PLCubeFaceOrientation.PLCubeFaceOrientationDown); panorama = cubicPanorama; break; // 球形全景 panorama (supports up 2048x1024 image) case 1: panorama = new PLSpherical2Panorama(); ((PLSpherical2Panorama) panorama).setImage(new PLImage(PLUtils .getBitmap(context, R.raw.panorama2), false)); break; // 球形全景 (supports up 1024x512 image) case 2: panorama = new PLSphericalPanorama(); ((PLSphericalPanorama) panorama).setImage(new PLImage(PLUtils .getBitmap(context, R.raw.panorama), false)); break; // 圓柱形全景 (supports up 1024x1024 image) case 3: PLCylindricalPanorama cylindricalPanorama = new PLCylindricalPanorama(); cylindricalPanorama.setHeight(3.0f); cylindricalPanorama.getCamera().setPitchRange(0.0f, 0.0f); cylindricalPanorama.setImage(new PLImage(PLUtils.getBitmap( context, R.raw.panorama), false)); panorama = cylindricalPanorama; break; default: break; } if (panorama != null) { // 設定攝像機的旋轉角度 panorama.getCamera().lookAt(0.0f, 170.0f); // 新增一個熱點 panorama.addHotspot(new PLHotspot(1, new PLImage(PLUtils .getBitmap(context, R.raw.hotspot), false), 0.0f, 170.0f, 0.05f, 0.05f)); // 重置檢視 this.reset(); // 載入全景圖 this.startTransition(new PLTransitionBlend(2.0f), panorama); // 或者 // this.setPanorama(panorama); } // 開啟全景檢視 this.setLocked(false); } catch (Throwable e) { Toast.makeText(this.getApplicationContext(), "Error: " + e, Toast.LENGTH_SHORT).show(); } } /** * 負載全景影象使用JSON協議 * * @param index表示的位置 * 0 = 立方體, 1 =球柱 , 2 = 球體, 3 = 圓柱體 */ private void loadPanoramaFromJSON(int index) { try { PLILoader loader = null; switch (index) { case 0: loader = new PLJSONLoader("res://raw/json_cubic"); break; case 1: loader = new PLJSONLoader("res://raw/json_spherical2"); break; case 2: loader = new PLJSONLoader("res://raw/json_spherical"); break; case 3: loader = new PLJSONLoader("res://raw/json_cylindrical"); break; default: break; } if (loader != null) this.load(loader, true, new PLTransitionBlend(2.0f)); } catch (Throwable e) { Toast.makeText(this.getApplicationContext(), "Error: " + e, Toast.LENGTH_SHORT).show(); } } 其實載入主要用得的還是json協議,Android客戶端只是負責載入360全景影象,具體的大家可以下一個例子來看下,具體的可以留言問我,當然我可能也有許多不懂的地方,我也是在研究,大家互相學習進步。