从网络加载图像时的占位符动画

Placeholder animation while loading image from the network

我正在处理一堆 ImageView,其中大部分都是动态更新的。我检查图像是否存在于本地存储驱动器上,如果不存在,我下载图像。当这一切发生时,我创建了一个 CardView 并将其显示在屏幕上。我想知道的是如何在我的图像下载时将我的 ImageView 设置为具有占位符动画(圆圈加载动画)?我曾尝试使用 .gif,但据我所知,android "from the box" 不支持动画。还有哪些选择?

最简单的方法是使用单独的视图:ProgressBar。每个图像单元格都是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ImageView>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </ProgressBar>
</RelativeLayout>

ProgressBar 的名称不太恰当,因为可以将其样式设置为看起来像其他形状而不是条形。这种特殊的风格使它看起来像一个圆形的加载微调器。只需确保在加载图像后将微调器可见性设置为 View.INVISIBLE。