我的目标是在 google map v2 上方绘制自由形状,因此我创建了一个应用程序,通过手指移动在自定义视图中绘制自由形状,并将此视图放在 layout.xml 中的地图片段上方,并使视图背景透明。现在我可以很好地绘制自由形状,但现在的问题是我无法控制地图或在其中进行缩放或拖动。
有什么方法可以让我同时控制它们,例如一个按钮来将控件从地图片段转换为绘图视图,反之亦然?或打开/关闭绘图视图?
这是我的代码
抽屉视图.java
public class DrawerView extends View {
public Paint paint = new Paint();
public Path path = new Path();
public Path circlePath = new Path();
private int lineCol;
public LayoutParams params;
public DrawerView(Context context, AttributeSet attrs){
super(context, attrs);
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.LineView, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
lineCol = a.getInteger(R.styleable.LineView_lineColor, 0);//0 is default
} finally {
a.recycle();
}
//paint object for drawing in onDraw
paint.setAntiAlias(true);
paint.setColor(lineCol);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(4f);
}
public void onButtonPress(){
// resets the screen
path.reset();
// Calls the onDraw() method
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
//canvas.drawPath(circlePath, circlePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
circlePath.reset();
circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
break;
case MotionEvent.ACTION_UP:
circlePath.reset();
break;
default:
return false;
}
postInvalidate();
return true;
}
}
MainActivity.java
public class MainActivity extends Activity
implements OnMapClickListener, OnMapLongClickListener, OnMarkerDragListener{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
Button plane;
Button tank;
Button ship;
DrawerView myView;
public Button btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plane = (Button)findViewById(R.id.btn_plane);
tank = (Button)findViewById(R.id.btn_tank);
ship = (Button)findViewById(R.id.btn_ship);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
myMap.setOnMarkerDragListener(this);
plane.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMap.addMarker(new MarkerOptions().position(
new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.plane)).draggable(true));
}
});
tank.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMap.addMarker(new MarkerOptions().position(
new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.tank)).draggable(true));
}
});
ship.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMap.addMarker(new MarkerOptions().position(
new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.destroyer)).draggable(true));
}
});
markerClicked = false;
myView = (DrawerView)findViewById(R.id.custView);
myView.setBackgroundColor(Color.TRANSPARENT);
btnReset = (Button) findViewById(R.id.clear);
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myView.path.reset();
myView.postInvalidate();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
@Override
public void onMapClick(LatLng point) {
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
markerClicked = false;
}
@Override
public void onMapLongClick(LatLng point) {
markerClicked = false;
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
}
@Override
public void onMarkerDragStart(Marker marker) {
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.dmeogooglemapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_tank"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="@drawable/tank" />
<Button
android:id="@+id/btn_plane"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:background="@drawable/plane" />
<Button
android:id="@+id/btn_ship"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="@drawable/destroyer" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.dmeogooglemapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
<com.example.dmeogooglemapv2.DrawerView
android:id="@+id/custView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
custom:lineColor="#ff0099" />
</RelativeLayout>
</LinearLayout>