【发布时间】:2012-09-15 18:52:57
【问题描述】:
我希望这里的人可能知道导致这种行为的原因:
在我的整个应用程序中,在看似随机的地方和随机条件下,我都在观察这个奇怪的 UI 问题。图像有时被加载为黑色(具有正确的边界)或不正确的图像源(再次,具有正确的边界)。这会影响ImageViews 并影响android:background 带有颜色资源引用的标签。
我的应用程序依赖于 6 个库项目,它通过服务运行本机代码,应用程序中的活动使用 GlSurfaceViews(尽管并非所有显示问题的活动都包含 OpenGL 组件)。通过使用大量内存,我认为问题可能来自这些地方或它们的组合。
您可以在以下屏幕截图中看到此行为:
-
这实际上是一个 6 像素左右宽的列分隔符图像,它被错误地绘制到我的 ImageView 中(ImageView 本身的大小似乎正确)。
-
当退出应用程序然后再次(重复)返回时,它反而出现(并保持)如下:
-
在强制清除和清除应用数据后,它返回到正确的格式:
您还可以看到,它旁边的放大镜图像在每个图像中都显示良好。这些丢失/不正确的图像和背景的问题似乎在整个应用程序生命周期中随机发生,我一直无法找到重现它的方法。
这些图像的布局没有什么特别之处,在渲染生命周期中我没有做任何有趣的事情(我没有覆盖onDraw() 或onMeasure() 等)。这些图像的来源不是动态设置的,而是通过 XML 设置的。
从上面的示例可以看出,这不是构建问题,因为它发生在应用生命周期之间而不是安装之间。它也出现在不同的设备上,例如三星 8.9、Acer Iconia Tab、Motarola XOOM,
在我看来,引用表存在某种错误,它可能是由我的本机代码推动的吗?还是我在应用程序的某些阶段使用过多内存造成的?
这是上述示例的 XML 源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/browseProgressWrapper"
android:layout_width="match_parent"
android:layout_height="@dimen/actionbar_compat_height"
android:orientation="horizontal">
<RelativeLayout android:layout_width="@dimen/search_bar_width"
android:layout_height="match_parent">
<EditText android:id="@+id/browseFilter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:imeOptions="actionSearch"
android:background="@drawable/edit_text_blue"
android:maxLength="30"/>
<ImageView android:id="@+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_input_delete"
android:layout_marginRight="5dp"/>
</RelativeLayout>
<ImageView android:id="@+id/browseFilterButton"
android:src="@drawable/ic_menu_search"
android:scaleType="center"
android:layout_width="@dimen/actionbar_compat_height"
android:layout_height="@dimen/actionbar_compat_height"
android:layout_gravity="center_vertical"
android:minWidth="@dimen/actionbar_compat_height"/>
</LinearLayout>
我碰巧得到了另一个类似事件的代码/布局的更完整描述:
我有一个“设置”Activity,它会在保存新设置详细信息后重新启动我的应用程序。它通过停止Service、调用新的Activity(Splash Activity)并完成自身来实现:
mConfiguration.save();
mConfiguration = new Configuration(Configuration.getInstance());
getActivity().stopService(new Intent(getActivity(), NativeService.class));
getActivity().finish();
startActivity(new Intent(getActivity(), SplashActivity.class));
大多数情况下(在大多数设备上)这都能正常工作,Splash Activity 包含正确加载的图像。有时,尽管在某些设备上,Splash Activity 加载了不正确的资源(我的测试人员称之为“颠倒的耐克刻度”)或只是一个空白框(如下所示)。有谁知道为什么?
这是 Splash 页面的布局,您可以看到它非常简单,没有任何意外:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ContentBackgroundColor"
android:orientation="vertical" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/manager_android_400" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
理论检验和证明:
我推测这可能是处理器/内存问题,在启动画面退出并移动到下一个活动之前布局没有被完全绘制,所以我输入了这段代码:
image = (ImageView) findViewById(R.id.image);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
moveToStartScreen.start();
}
});
希望上面的代码可以确保图像在移动到开始页面之前确实已加载,但似乎没有可观察到的效果。
另一种理论
我还想知道这是否可能是由 R.id / R.colour / R.drawable 资源引起的,在程序执行中如何被破坏?有谁知道为什么会发生。
我的本机代码会在一些 Android 未正确分配的内存地址上泛滥吗?
以前有没有人注意到这一点 - 或者可能知道为什么会发生这种行为?
【问题讨论】:
-
我在使用的库项目中出现了错误的可绘制对象的问题,由于你报告了同样的事情,我在谷歌上搜索它并最终得到stackoverflow.com/questions/7724145/…。有什么帮助吗?
-
您能否详细解释一下您的本机代码如何与您的应用程序交互?对我来说,在某些东西上涂鸦的本地代码似乎是最合理的。
-
@MattGibson Native Code 接口通过 JNI 方法触发 UI 组件可以注册接收的回调/侦听器方法。除非您考虑将本机代码直接写入 OpenGL 上下文,否则不会在 JNI 之外直接发送数据。
-
@Graeme 我很想知道您的问题是否与我遇到的问题有关。经过无数小时的调试,我找到了我的旁路。 (见stackoverflow.com/questions/16526275/…)。如果您有机会检查我的旁路,请告诉我它是如何进行的。
标签: android image layout tearing