我的目標是繪製上述谷歌地圖v2的自由形狀,所以我創建一個應用程序來繪製由手指移動自由的形狀在定製視圖和layout.xml推杆地圖片段高於此視圖和使視圖背景變得透明。 現在我可以很好地繪製自由形狀,但現在的問題是我無法控制地圖或縮放或拖動它。禁用和啓用視圖機器人
是否有什麼辦法可以讓我控制他們兩個,例如按鈕隱蔽從地圖片段面圖的控制,反之亦然? 或打開/關閉繪圖視圖?
這是我的代碼
DrawerView.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>
你能告訴我怎麼用代碼?這會讓我再次控制地圖嗎? – Eman87
是的,這應該返回您在地圖 – Desert
的控制非常感謝你的幫助,它現在與我。 :) – Eman87