禁用mapView的硬件加速会导致不断重绘 [英] Disabling hardware acceleration for mapView leads to constant redraws

查看:94
本文介绍了禁用mapView的硬件加速会导致不断重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:使用xml中的android:hardwareAccelerated ="false"禁用硬件加速会将我的Theme.Sherlock.Light.DarkActionBar主题的背景颜色更改为更白的白色". 这曾经是主要问题.我更改标题以强调第二个问题.

Short version: Disabling hardware acceleration with android:hardwareAccelerated="false" in xml changes the background color of my Theme.Sherlock.Light.DarkActionBar theme to a whiter "white". this used to be the main question. I changed the title to emphasize the second problem.

仅对mapView禁用硬件加速,会导致不断重绘.

Disabling hardware acceleration for the mapView only, causes constant redraws.

长版:

默认情况下,API 14级及更高版本启用AFAIK硬件加速.(参考)

AFAIK hardware acceleration is enabled by default on API level 14 and up. (reference)

由于我正在构建和测试API级别16,因此通常会启用硬件加速,这就是我以前所看到的.主题是浅色,但不是完全纯白色,而是浅灰色(默认).

Since I'm building and testing for API level 16, my hardware acceleration was usually on so that's what I was used to seeing. The theme is light but not quite pure white, it's a light grey (default).

我在地图上绘制了一些圆形叠加层,当我放大放大时,mapview变得非常缓慢,并且在logcat中出现了太大而无法渲染为纹理"的形状错误.我发现关闭硬件加速可以解决此问题.

I had some circle overlays being drawn on a map and when I zoomed in close, the mapview was becoming very laggy and I was getting a shape "too large to be rendered into a texture" error in logcat. I discovered that turning hardware acceleration off fixes the problem.

当我为android清单中的应用程序(或单个活动)关闭硬件加速时,布局的背景颜色会更改.它从浅灰色变为非常浅的灰色,几乎是纯白色.这是正常行为吗?

When I turn hardware acceleration off for the application (or an individual activity) in the android manifest, the background color of my layouts changes. It goes from a light grey to a very very light grey, almost pure white. Is this normal behavior?

我尝试使用以下命令仅针对mapview关闭硬件加速:

I tried turning off hardware acceleration just for the mapview with:

if(android.os.Build.VERSION.SDK_INT>=11) {
mapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); }

这非常有用,可以消除纹理太大的错误,因为mapview并非由硬件加速的,并且还可以使我的其他应用程序硬件保持加速.这将是理想的解决方案.但是,这引起了另一个问题.它使我使用此代码的叠加层的onDraw方法不断被调用.即onDraw本身会被一遍又一遍地调用,而不会在mapview上调用invalidate().知道为什么会这样吗?

This works great to get rid of the texture too large errors because the mapview is not hardware accelerated AND it also keeps the rest of my app hardware accelerated. This would be the ideal solution. However, this causes another problem. It makes the onDraw method of the overlays where I use this code get called constantly. i.e, onDraw is called over and over and over by itself without calling invalidate() on the mapview. Any idea why this would be the case?

更新:

下面是一些简单的代码,当仅针对mapView禁用硬件加速(我想要的)时,该常量将通过重绘常量来重新创建问题:

Below is some simple code that will recreate the issue with the constant redraws when hardware acceleration is disabled only for the mapView (what I want):

MapActivity:

public class SettingsActivity extends MapActivity {

private MapView mapView;
private static List<Overlay> overlayList;
private static AccuracyCircleOverlay accuracyCircleOverlay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manage_maps);

    mapView = (MapView) findViewById(R.id.map);

    overlayList = mapView.getOverlays();
    accuracyCircleOverlay = new AccuracyCircleOverlay(this);
    overlayList.add(accuracyCircleOverlay);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

覆盖:

public class AccuracyCircleOverlay extends Overlay {

private Context context;

public AccuracyCircleOverlay(Context context) {
    this.context = context;
}

@TargetApi(11)
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);

    if (android.os.Build.VERSION.SDK_INT >= 11) {
        mapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }                 
            Log.d("accuracy OVERLAY", "being redrawn");
}
}

推荐答案

我终于意识到问题出在哪里.不应从叠加层的draw方法内部将mapView的图层类型设置为软件!这样做似乎会触发另一次绘制,从而导致循环.我将以下代码放入保存了mapView的Activity的onCreate中,该代码可以正常工作.它告诉mapView在软件模式下渲染而不会引起不断的重绘!

I finally realized what the problem was. Setting the layer type of the mapView to software should not be done from inside the draw method of the overlay! Doing that seems to trigger another draw, hence causing a loop. I put the following code in the onCreate of my Activity that holds the mapView and it worked. It tells the mapView to render in software mode without causing constant redraws!

活动中:

if (android.os.Build.VERSION.SDK_INT >= 11) {
    mapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}  

或者使用XML:

<com.google.android.maps.MapView
        ...
        android:layerType="software" />

这篇关于禁用mapView的硬件加速会导致不断重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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