首先,感谢一下这位哥们的文章 http://blog.csdn.net/weiming8517/article/details/8793968
官方例子:http://code.google.com/p/panoramagl-android/downloads/list
因为公司最近有这个需求,所以就研究了下这玩意。
其实很简单的,就一个类,仔细看看就会了.......
先上图:
1.首先看看结构
2.然后是Manactivity类,我在之前那位哥们基础上加了几段代码,实现了 “旋转手机,自动浏览360°全景”
package com.example.panoramagldemo;
import javax.microedition.khronos.opengles.GL10;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import com.panoramagl.PLCylindricalPanorama;
import com.panoramagl.PLIPanorama;
import com.panoramagl.PLImage;
import com.panoramagl.PLSphericalPanorama;
import com.panoramagl.PLView;
import com.panoramagl.utils.PLUtils;
public class MainActivity extends PLView
{
/**
* 控制全景图 是否可以自动旋转
* true 可以选择 false 不可以旋转
*/
private boolean sign=true;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//调用加载全景图方法
this.loadPanorama(0);
}
/**
* This event is fired when OpenGL renderer was created
* @param gl Current OpenGL context
*/
@Override
protected void onGLContextCreated(GL10 gl)
{
super.onGLContextCreated(gl);
//Add layout
View mainView = this.getLayoutInflater().inflate(R.layout.activity_main, null);
this.addContentView(mainView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//重要 官方这里没有给出使用示例 控制垂直方向观看角度 默认为getCamera().setPitchRange(-90,90);
//可设定getCamera().setPitchRange(-30,30);等有一定仰俯视角度的观看效果 这里设置为上下不可滑动
// getCamera().setPitchRange(0,0);
//取消放大缩小
//getCamera().setFovEnabled(false);
/*//Zoom controls 缩放控制
ZoomControls zoomControls = (ZoomControls)mainView.findViewById(R.id.zoomControls);
zoomControls.setOnZoomInClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
getCamera().zoomIn(true);
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
getCamera().zoomOut(true);
}
});*/
}
/**load methods*/
/**
* Load panorama image manually
* @param index Spinner position where 0 = spherical, 1 = spherical2, 2 = cubic, 3 = cylindrical
*/
private void loadPanorama(int index)
{
GL10 gl = this.getCurrentGL();
PLIPanorama panorama = null;
//Lock panoramic view
this.setBlocked(true);
//Spherical panorama example (supports up 1024x512 texture) 球体全景例子 1024*512贴图
if(index == 0)
{
//gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
panorama = new PLSphericalPanorama();
((PLSphericalPanorama)panorama).setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.drawable.a), false));
}
//Spherical2 panorama example (only support 2048x1024 texture) 球体全景例子 2048*1024贴图
/*
else if(index == 1)
{
panorama = new PLSpherical2Panorama();
((PLSpherical2Panorama)panorama).setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_sphere2), false));
}
//Cubic panorama example (supports up 1024x1024 texture per face) 立方体全景例子 需每个面的贴图都是1024*1024
else if(index == 2)
{
PLCubicPanorama cubicPanorama = new PLCubicPanorama();
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_f), false), PLCubeFaceOrientation.PLCubeFaceOrientationFront);
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_b), false), PLCubeFaceOrientation.PLCubeFaceOrientationBack);
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_l), false), PLCubeFaceOrientation.PLCubeFaceOrientationLeft);
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_r), false), PLCubeFaceOrientation.PLCubeFaceOrientationRight);
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_u), false), PLCubeFaceOrientation.PLCubeFaceOrientationUp);
cubicPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.pano_d), false), PLCubeFaceOrientation.PLCubeFaceOrientationDown);
panorama = cubicPanorama;
}*/
//Cylindrical panorama example (supports up 1024x1024 texture) 柱形全景例子 需提供1024*1024贴图
else if(index == 3)
{
PLCylindricalPanorama cylindricalPanorama = new PLCylindricalPanorama();
cylindricalPanorama.setHeightCalculated(false);
cylindricalPanorama.getCamera().setPitchRange(0.0f, 0.0f);
cylindricalPanorama.setImage(gl, PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.a), false));
panorama = cylindricalPanorama;
}
//Add a hotspot 添加热点
// panorama.addHotspot(new PLHotspot((kHotspotIdMin + Math.abs(random.nextInt()) % ((kHotspotIdMax + 1) - kHotspotIdMin)), PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.hotspot), false), 0.0f, 0.0f, 0.08f, 0.08f));
//Load panorama
this.reset();
this.setPanorama(panorama);
//为True的时候可以自动旋转
if(sign){
this.startSensorialRotation();
// this.activateOrientation();
this.activateAccelerometer();
// this.activateMagnetometer();
// this.activateGyroscope();
this.setAccelerometerEnabled(true);
this.setAccelerometerInterval(0.033f);
this.setAccelerometerSensitivity(10.0f);
this.setAccelerometerLeftRightEnabled(true);
this.setAccelerometerUpDownEnabled(false);
}else{
this.setAccelerometerEnabled(false);
}
//Unlock panoramic view
this.setBlocked(false);
}
}
再次感谢那位哥......
Demo下载地址点击打开链接