1

I am trying to show a MapView as a circle on Android just like that:

protected void onDraw(Canvas canvas) {
    Path path = new Path();
    path.addCircle(400,200,100,Direction.CW);
    canvas.clipPath(path);
    super.onDraw(canvas);
}

Unfortunately the MapView (v2: com.google.android.gms.maps.MapView) component seems to ignore that and instead just taking the bounds/rect of that circle to draw the map :(

I've been searching the web for a while now, not finding a nice solution for that.

Is this even possible? (The Blundell solution is not working for me because I want the background to be visible around the map)

Thanks!

4

1 回答 1

8

Android 从 API 级别 11 开始支持硬件加速。问题是当硬件加速打开时,并非所有绘图操作都支持。并且Canvas.clipPath是其中之一。可以在此处找到不支持的操作的完整列表

clipPath您可以尝试在不使用或仅禁用硬件加速的情况下实现您想要的功能。

要在应用程序级别禁用硬件加速,请确保您拥有targetSdkVersion11 或更高版本并hardwareAccelerated在以下位置使用标记AndroidManifest

<application android:hardwareAccelerated="false" ...>

您可以使用以下方法仅为特定视图禁用硬件加速。

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void enableHardwareAcceleration(View view, boolean enabled) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (enabled) {
            view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }
}

请注意,您必须使用TargetApi注释并检查设备的android版本是否为Honeycomb或更高,否则Eclipse可能会产生错误。

如果您的问题中的代码不起作用,请尝试将其替换为:

@Override
protected void dispatchDraw(Canvas canvas) {
    Path path = new Path();
    int count = canvas.save();

    path.addCircle(400,200,100,Direction.CW);

    canvas.clipPath(path);
    super.dispatchDraw(canvas);
    canvas.restoreToCount(count);
}
于 2013-01-12T15:50:35.143 回答