我正在使用StreetViewPanorama
在Android应用中显示全景图像和街景。当我尝试使用当前渲染的图像对该屏幕进行截图时,它给了我空白屏幕
我引用了this链接
给出给定gps点的静止图像。 但是我想拍摄我正在渲染的屏幕截图。 下面是代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lin"
>
<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ok" />
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/g_map_street"
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</LinearLayout>
MainActivity.java
public class StreetViewMapActivity extends AppCompatActivity implements OnStreetViewPanoramaReadyCallback {
private StreetViewPanorama streetView;
Button btnOk;
LinearLayout lin=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_street_view_map);
lin=(LinearLayout)findViewById(R.id.lin);
btnOk=(Button)findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View rootView = getWindow().getDecorView().findViewById(R.id.g_map_street);
Bitmap bm=getScreenShot(rootView);
store(bm,"hello.jpg");
}
});
StreetViewPanoramaFragment streetViewFragment =
(StreetViewPanoramaFragment) getFragmentManager()
.findFragmentById(R.id.g_map_street);
streetViewFragment.getStreetViewPanoramaAsync(this);
}
public Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
public void store(Bitmap bm, String fileName){
String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() ;
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
streetView = streetViewPanorama;
streetViewPanorama.setPosition(new LatLng(-33.87365, 151.20689));
}
}
请帮助。