裁剪/遮蔽在Android上的MapView [英] Clipping/Masking a MapView on Android

查看:115
本文介绍了裁剪/遮蔽在Android上的MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表现出作为MapView类Android上的一个圆圈,就像是:

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);
}

不幸的MapView(第二版: com.google.android.gms.maps.MapView )成分似乎忽略这一点,而不是仅仅采取这一圈的边界/ RECT绘制地图:(

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)

谢谢!

推荐答案

Android支持,因为API级别11.硬件加速的问题是,不是当硬件加速开启所有的绘图操作都支持。和 Canvas.clipPath 就是其中之一。不支持的操作的完整列表,可以发现这里

Android supports a hardware acceleration since API level 11. The problem is that not all drawing operations are supported when hardware acceleration is turned on. And Canvas.clipPath is one of them. Complete list of unsupported operations can be found here

您可以尝试实现你想要的东西,而无需使用 clipPath 或只是禁用硬件加速。

You can try to implement what you want without using clipPath or just disable hardware acceleration.

要禁用应用程序级硬件加速确保您有 targetSdkVersion 11或更高版本,并使用在 hardwareAccelerated 标签 AndroidManifest

To disable hardware acceleration on application level make sure you have targetSdkVersion 11 or higher and use hardwareAccelerated tag in the AndroidManifest:

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

您只能使用以下方法特定视图禁用硬件加速。

You can disable hardware acceleration only for the particular view using the following method.

@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 注释和检查设备的Andr​​oid版本为蜂窝或更高,否则的Eclipse 可能会产生错误。

Note that you have to use TargetApi annotation and check if device android version is Honeycomb or higher, otherwise Eclipse may produce an error.

如果从你的问题code不工作尝试用它来取代它:

If code from your question isn't working try to replace it with this:

@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);
}

这篇关于裁剪/遮蔽在Android上的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆