是否可以从活动屏幕上截取渲染的街景全景视图的屏幕截图?

时间:2018-10-01 12:01:06

标签: android android-fragments screenshot google-street-view

我正在使用StreetViewPanorama在Android应用中显示全景图像和街景。当我尝试使用当前渲染的图像对该屏幕进行截图时,它给了我空白屏幕

以下是屏幕截图图像 enter image description here

我引用了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));





    }
}

请帮助。

0 个答案:

没有答案