方法一:OpenGL ES
1.在build.gradle文件中添加依赖
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
高版本AS中settings.gradle.kts:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
2.添加库依赖
compile 'com.github.CN-ZPH:weibo360panorama:v1.0.1'
高版本AS中:
implementation("com.github.CN-ZPH:weibo360panorama:v1.0.1")
3.添加布局文件
<com.zph.glpanorama.GLPanorama
android:id="@+id/mGLPanorama"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.zph.glpanorama.GLPanorama>
4.传入你的全景图
public class Main2Activity extends AppCompatActivity {
private GLPanorama mGLPanorama;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//初始化全景控件
mGLPanorama= (GLPanorama) findViewById(R.id.mGLPanorama);
mGLPanorama.setGLPanorama(R.drawable.bbb);
}
}
示例如下:
方法二:Google VR
1.添加依赖
目前GitHub上最新版本号为1.8.0,我这里也用最新的了。
最低支持到 minSdkVersion 19 也就是Android 4.4.0
在 build.gradle 文件中添加库依赖:
dependencies {
compile 'com.google.vr:sdk-panowidget:1.80.0'
}
2.创建布局
<com.google.vr.sdk.widgets.pano.VrPanoramaView
android:id="@+id/mVrPanoramaView"
android:layout_width="match_parent"
android:layout_height="250dip"/>
3.AndroidManifest中添加权限
<!-- These permissions are used by Google VR SDK to get the best Google VR headset profiles. !-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//因为全景图较大,所以在application下申请更多空间,但是作为一个有节操的码农建议你不要这么干。
<application android:largeHeap="true" </application>
4.Activity中初始化组件
//初始化VR图片
private void initVrPaNormalView() {
mVrPanoramaView = (VrPanoramaView) findViewById(R.id.mVrPanoramaView);
VrPanoramaView.Options paNormalOptions = new VrPanoramaView.Options();
paNormalOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;
// mVrPanoramaView.setFullscreenButtonEnabled (false); //隐藏全屏模式按钮
mVrPanoramaView.setInfoButtonEnabled(false); //设置隐藏最左边信息的按钮
mVrPanoramaView.setStereoModeButtonEnabled(false); //设置隐藏立体模型的按钮
mVrPanoramaView.setEventListener(new ActivityEventListener()); //设置监听
//加载本地的图片源
mVrPanoramaView.loadImageFromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.andes), paNormalOptions);
//设置网络图片源
// panoWidgetView.loadImageFromByteArray();
}
private class ActivityEventListener extends VrPanoramaEventListener {
@Override
public void onLoadSuccess() {//图片加载成功
}
@Override
public void onLoadError(String errorMessage) {//图片加载失败
}
@Override
public void onClick() {//当我们点击了VrPanoramaView 时候触发 super.onClick();
}
@Override
public void onDisplayModeChanged(int newDisplayMode) {
super.onDisplayModeChanged(newDisplayMode);
}
}