我正在研究需要在谷歌地图上添加绘图功能的项目。 我的问题是在我试图将图像保存在外部存储器中绘制谷歌地图后。它保存在内存中,但无法获得谷歌地图的背景。
这是保存按钮后创建的图像,我有当前位置按钮,编辑按钮,谷歌文本但不是地图:
这是我的xml文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/map_search"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:hint="search....."
android:textColor="@android:color/black"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="geoLocate"
android:text="GO" />
<ImageButton
android:id="@+id/exit_edit_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/exit_button"
android:background="@null"
android:layout_marginTop="8dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
/>
<ImageButton
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/save_32"
android:background="@null"
android:layout_marginTop="8dp"
android:layout_marginRight="10dp"
/>
</LinearLayout>
<FrameLayout
android:id="@+id/signature"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/mapFragment"/>
<ImageButton
android:id="@+id/edit_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/edit_button"
android:background="@null"
android:layout_marginLeft="320dp"
android:layout_marginTop="430dp"/>
</FrameLayout>
</LinearLayout>
这是我的地图活动代码,我称之为sava方法。
private void drawOnMap() {
mcontent = (FrameLayout) findViewById(signature);
save_button = (ImageButton) findViewById(R.id.save_button);
exit_edit_mode = (ImageButton) findViewById(R.id.exit_edit_mode);
mSignature = new signature(this, null);
mcontent.addView(mSignature);
save_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSignature.save();
}
});
exit_edit_mode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSignature.clear();
mcontent.removeView(mSignature);
mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
}
});
}
这里我定义了从地图活动中调用的保存方法
public void save() {
Bitmap returnedBitmap = Bitmap.createBitmap(mcontent.getWidth(),
mcontent.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = mcontent.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.MAGENTA);
mcontent.draw(canvas);
ByteArrayOutputStream baso=null;
File file=null;
try{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
// compress and write output stream
baso=new ByteArrayOutputStream();
returnedBitmap.compress(Bitmap.CompressFormat.JPEG,40,baso);
file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+file.separator+"pictures/"+timeStamp+".JPEG");
file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
fos.write(baso.toByteArray());
fos.close();
Toast.makeText(signature.this.getContext(), "Map Saved to memory", Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
这就是我在谷歌地图上画画的方式:
这是我的签名课,它帮助我在谷歌地图上做免费的手绘。
public class signature extends View {
static final float STROKE_WIDTH = 10f;
static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
Paint paint = new Paint();
Path path = new Path();
float lastTouchX;
float lastTouchY;
final RectF dirtyRect = new RectF();
public signature(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
public void clear() {
path.reset();
invalidate();
}
public void save() {
Bitmap returnedBitmap = Bitmap.createBitmap(mcontent.getWidth(),
mcontent.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = mcontent.getBackground();
System.out.println("#######################"+bgDrawable);
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
mcontent.draw(canvas);
ByteArrayOutputStream baso=null;
File file=null;
try{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
// compress and write output stream
System.out.println("@@@@@@@@@@@@@@@@"+timeStamp);
baso=new ByteArrayOutputStream();
returnedBitmap.setHasAlpha(true);
returnedBitmap.compress(Bitmap.CompressFormat.JPEG,40,baso);
file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+file.separator+"pictures/"+timeStamp+".JPEG");
file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
fos.write(baso.toByteArray());
fos.close();
Toast.makeText(signature.this.getContext(), "Map Saved to memory", Toast.LENGTH_LONG).show();
/*Intent intent = new Intent();
intent.putExtra("byteArray", baso.toByteArray());
setResult(1, intent);
finish();*/
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
//save.setEnabled(true);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX, eventY);
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
path.lineTo(historicalX, historicalY);
}
path.lineTo(eventX, eventY);
break;
}
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
private void resetDirtyRect(float eventX, float eventY) {
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
如果有人让我摆脱它,我将非常感激。
谢谢。
更新
当我尝试这种方法时,它只保存地图而不是我在谷歌地图上绘制的部分。当我在bitmap下面使用canvas时压缩它只保存地图但是当我在bitmap上面使用canvas时压缩它再次出现黑屏,如上图所示。
public void CaptureMapScreen() {
GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
// TODO Auto-generated method stub
bitmap = snapshot;
try {
FileOutputStream out = new FileOutputStream("/mnt/sdcard/"
+ "MyMapScreen" + System.currentTimeMillis()
+ ".png");
// above "/mnt ..... png" => is a storage path (where image will be stored) + name of image you can customize as per your Requirement
Canvas convas = new Canvas(bitmap);
mcontent.draw(convas);
// mcontent.getBackground();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Toast.makeText(getContext(),"map saved",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
};
mGoogleMap.snapshot(callback);
}