我想用 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)