在Android中实现内容占位符作为进度指标

时间:2018-03-13 13:59:48

标签: android progress-indicator

我的问题与this question非常相似。我想获得使用这种风格的进度指标:

https://github.com/michalsnik/ember-content-placeholders

即用文本和图像替换内容为动画like here的灰色矩形。

过去我见过这么多,例如Facebook的。我很确定YouTube应用曾经拥有这种风格。

问题

显而易见的第一个问题是如何在Android上实现这一点,特别是如果有一种惯用的,标准的或简单的方法来实现这一点,第二个问题与此密切相关,因为这是我的

问题

如何将TextView转换为此类加载矩形。我可以用这个替换我的整个视图结构,但我也不知道从哪里开始动画。

2 个答案:

答案 0 :(得分:1)

看看这个图书馆 - ShimmerLayout

  

ShimmerLayout可用于为您的Android应用程序添加微光效果(如Facebook或LinkedIn使用的效果)。

另外,如果您对如何实现动画感到好奇,请查看库sources

答案 1 :(得分:0)

您需要创建骨架布局并将其在整个屏幕上充气。然后,您可以使用其他库来添加闪烁效果。

drawable / skeleton.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <solid android:color="@color/skeleton"/>
<corners android:radius="4dp"/>
</shape>

layout / skeleton_row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="@dimen/row_layout_height">

  <View
    android:id="@+id/icon"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_margin="15dp"
    android:layout_gravity="center_vertical"
    android:background="@drawable/skeleton"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

  <View
    android:id="@+id/topText"
    android:layout_width="200dp"
    app:layout_constraintTop_toTopOf="@id/icon"
    app:layout_constraintStart_toEndOf="@id/icon"
    android:layout_height="15dp"
    android:layout_marginLeft="16dp"
    android:background="@drawable/skeleton"/>

  <View
    android:id="@+id/bottomText"
    android:layout_width="250dp"
    android:layout_height="15dp"
    app:layout_constraintTop_toBottomOf="@id/topText"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toEndOf="@id/icon"
    android:layout_marginLeft="16dp"
    android:background="@drawable/skeleton"/>

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/skeleton"
    app:layout_constraintTop_toBottomOf="@id/icon"
    android:layout_marginTop="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

</android.support.constraint.ConstraintLayout>

查看我编写的本教程,了解如何使用它:https://medium.com/@sha17187/upgrade-progress-loading-with-a-skeleton-and-shimmer-effect-in-android-863ea4ff5b0b