ImageView 很慢

ImageView very slow

我的问题是我的 ScrollView 非常缓慢和滞后。

我每次滚动时都会收到这些警告:

Skipped 146 frames!  The application may be doing too much work on its main thread.

这是我的 XML 文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/c1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/course1" />

    <TextView
        android:id="@+id/c1text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/c1"
        android:gravity="center"
        android:text="BlaBla1"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView [...] />

    <TextView [...] />

    [more ImageViews and TextViews]
</RelativeLayout>

有什么优化图片加载的方案?

对此最明显的解决方案是按照 Android 的预期使用 ListView

使用 ListView 的原因是它回收其显示,从而保持最小的内存占用。结合 ViewHolder pattern and even with Picasso or Glide 库,它可以轻松地进行预加载和缓存,并为您提供更好的性能。

不幸的是,唯一的选择是在后台手动预加载图像,但这会消耗内存并可能导致您的应用程序关闭。

可能,您的图片的分辨率超过了屏幕所需的分辨率。如果可能,提前调整图像大小。