gpt4 book ai didi

android - 向 Gallery 添加一个简单的 ScrollView 会导致内存泄漏

转载 作者:IT王子 更新时间:2023-10-28 23:37:35 26 4
gpt4 key购买 nike

我在使用 Gallery 组件时遇到了只能归类为 ScrollView 元素的内存泄漏。

简短的背景。我有一个现有的应用程序,它是一个照片幻灯片应用程序。它使用 Gallery 组件,但适配器中的每个元素都以全屏显示。(完整源代码可在 this link 获得)

适配器 View 元素由一个 ImageView 和两个用于标题和描述的 TextView 组成。由于照片的分辨率相当高,该应用程序使用了大量内存,但画廊通常能够很好地回收它们。

但是,当我现在为描述 TextView 实现 ScrollView 时,我几乎立即遇到了内存问题。这是我做的唯一改变

<ScrollView
android:id="@+id/description_scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:id="@+id/slideshow_description"
android:textSize="@dimen/description_font_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_below="@id/slideshow_title"
android:singleLine="false"
android:maxLines="4"/>
</ScrollView>

我做了一个堆转储,可以清楚地看到这是内存问题的根源所在的 Scrollview。

以下是堆转储分析的两个屏幕截图。请注意,ScrollView 保留了对 mParent 的引用,其中包括我使用的大照片 Heap analysis - leak candidate Heap analysis - drilldown to a single ScrollView

如果我使用 TextView 的滚动(android:scrollbars = "vertical"and .setMovementMethod(new ScrollingMovementMethod());

PSS 尝试关闭持久绘图缓存,但没有不同 dreaandroid:persistentDrawingCache="none"

最佳答案

您是否尝试过在容器 View 滚出屏幕时移除 ScrollView ?我不确定这是否适合您,但值得一试吗?或者,尝试在 ScrollView 离开屏幕时调用 setScrollContainer(false)。这似乎从 mScrollContainers 集中删除了 View 。

另外,this question ,由 Dianne Hackborn(android 工程师)回答,明确声明不要在画廊内使用可 ScrollView 。也许这个问题是为什么?

关于android - 向 Gallery 添加一个简单的 ScrollView 会导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com