1

我想用 Google Map v2 快照填充 ImageView。

简短的摘要:

我还需要扩充 Maps XML 实例吗?

我需要使用地图片段吗?

我想以编程方式创建地图实例并拍摄快照吗?

我的想法是以编程方式创建一个 Maps 实例,适当调整大小(比如 200x200),然后拍摄快照。但是,我的快照例程总是因"Bitmap must be > 0 in height and width" 而崩溃。

这是错误的方法吗?

XML 会是这样的:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical" >
        <ImageView android:id="@+id/location_card_map"
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:contentDescription="Location"
            android:clickable="true"/>
    </LinearLayout>

地图类:

    private ManagerMaps(Context context)
    {
        mContext = context;
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        initializeMaps();
    }
...
            private void initializeMaps()
            {
                LinearLayout layout = new LinearLayout(mContext);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                layout.setLayoutParams(new  LinearLayout.LayoutParams(200,200));//(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                GoogleMapOptions options = new GoogleMapOptions();
                options.camera(new CameraPosition(new LatLng(0, 0), 1, 0, 0));

                // Create the MapView programmatically
                mMapView = new MapView(mContext, options);
                layout.addView(mMapView);
                    mMapView.setLayoutParams(new LayoutParams(200,200));        
                mMapView.onCreate(null);
                mMapView.onResume();
        //      setContentView(layout);        

                // Gets to GoogleMap from the MapView and does initialization stuff
                mMap = mMapView.getMap();
                if(mMap == null)
                {
                    Log.d("AppDebug", "MAP IS NULL ********");
                    return;
                }
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                mMap.setMyLocationEnabled(true);

                // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
                try {
                    MapsInitializer.initialize(mContext);
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }

            }

            public void captureMapScreen() 
            {
                SnapshotReadyCallback callback = new SnapshotReadyCallback() {

                    @Override
                    public void onSnapshotReady(Bitmap snapshot) {
                        try {
                            FileOutputStream out = new FileOutputStream(
                                    Environment.getExternalStorageDirectory()
                                            + "/MapScreenShot"
                                            + System.currentTimeMillis() + ".png");

                            snapshot.compress(Bitmap.CompressFormat.PNG, 90, out);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                mMap.snapshot(callback);  // crashes in bitmap thread here with height/width zero
            }

调用方法:

                ManagerMaps maps = ManagerMaps.getInstance(getActivity());
                maps.captureMapScreen();

编辑:

崩溃发生在:mMap.snapshot(callback);

日志猫

10-22 01:36:44.300: E/AndroidRuntime(29292): FATAL EXCEPTION: Thread-3366
10-22 01:36:44.300: E/AndroidRuntime(29292): java.lang.IllegalArgumentException: width and height must be > 0
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:695)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:674)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:641)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at maps.ag.ca.a(Unknown Source)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at maps.ag.av.run(Unknown Source)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at java.lang.Thread.run(Thread.java:841)
4

1 回答 1

0

这个问题是由 Android 中可绘制资源的自动缩放和非常小的可绘制对象的不幸组合引起的。

例如,如果您的应用程序仅提供 drawable-xhpdi 资源,则需要缩小它们的大小以适应较低密度的屏幕。如果您自己不提供这些资源,这将由 Android 自动完成。

显示密度的比例设置如下:

xhdpi: 2.0 hdpi: 1.5 mdpi: 1.0 ldpi: 0.75

有时不幸的是,当将这些资源作为 xhdpi 提供并缩小它们时,像素大小可能会被截断为 0。对此没有适当的保护措施,Android 将无法创建具有该尺寸的位图并显示崩溃IllegalArgumentException: width and height must be > 0

但我不确定你的情况。因此,请使用 LogCat 更新您的问题。

更新它lyk:

public void captureMapScreen() {
        SnapshotReadyCallback callback = new SnapshotReadyCallback() {

            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                try {
                    mView.setDrawingCacheEnabled(true);
                    Bitmap backBitmap = mView.getDrawingCache();
                    Bitmap bmOverlay = Bitmap.createBitmap(
                            backBitmap.getWidth(), backBitmap.getHeight(),
                            backBitmap.getConfig());
                    Canvas canvas = new Canvas(bmOverlay);
                    canvas.drawBitmap(snapshot, new Matrix(), null);
                    canvas.drawBitmap(backBitmap, 0, 0, null);
                    FileOutputStream out = new FileOutputStream(
                            Environment.getExternalStorageDirectory()
                                    + "/MapScreenShot"
                                    + System.currentTimeMillis() + ".png");

                    bmOverlay.compress(Bitmap.CompressFormat.PNG, 90, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        mMap.snapshot(callback);

    }

用于 snapshot.compress(Bitmap.CompressFormat.PNG, 90, out);地图截图并避免代码

mView.setDrawingCacheEnabled(true);
Bitmap backBitmap = mView.getDrawingCache();
Bitmap bmOverlay = Bitmap.createBitmap(
backBitmap.getWidth(), backBitmap.getHeight(),
backBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(snapshot, new Matrix(), null);
canvas.drawBitmap(backBitmap, 0, 0, null);
于 2013-10-22T05:26:35.913 回答