禁用并启用视图android

时间:2013-07-16 21:21:00

标签: android google-maps-android-api-2 android-custom-view

我的目标是在谷歌地图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>

2 个答案:

答案 0 :(得分:2)

是的,您可以通过在false中返回onTouchEvent来切换触摸事件的接收者,这应该将事件传播到下一个视图。

UPD:代码应该是这样的:

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isTouchable) return false;
// your DrawerView logic
}

其中isTouchable是一个布尔变量,应该是false,如果你想绘制,你不想在你的视图和true上绘制。

答案 1 :(得分:0)

您可以启用或禁用绘图叠加层上的点击:

findViewById(R.id.custView).setClickable(true/false);

虽然您的绘图叠加层无法点击,但您可以与地图进行互动。