1

首先,这是我的回收站视图的项目布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto_medium"
        android:maxLines="3"
        android:text="dddddddddddddddddddddddddddddddddddddd"
        android:textColor="@color/tab_text_color"
        android:textSize="@dimen/_14ssp"
        app:layout_constraintEnd_toEndOf="@+id/cardView17"
        app:layout_constraintStart_toStartOf="@+id/cardView17"
        app:layout_constraintTop_toBottomOf="@+id/cardView17" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="4dp"
        app:cardCornerRadius="@dimen/_6sdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="@dimen/_120sdp"
                android:layout_height="@dimen/_180sdp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/edit_bg" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

这就是我添加微光效果的方式

val shimmer = Shimmer.AlphaHighlightBuilder()// The attributes for a ShimmerDrawable is set by this builder
            .setDuration(2000) // how long the shimmering animation takes to do one full sweep
            .setBaseAlpha(0.9f) //the alpha of the underlying children
            .setHighlightAlpha(0.93f) // the shimmer alpha amount
            .setWidthRatio(1.5F)
            .setDirection(Shimmer.Direction.LEFT_TO_RIGHT)
            .setAutoStart(true)
            .build()

        val shimmerDrawable = ShimmerDrawable().apply {
            setShimmer(shimmer)
        }

        Glide
            .with(context)
            .load(data.thumbnail)
            .placeholder(shimmerDrawable)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .centerCrop()
            .into(binding.thumbnail)

这就是我得到的结果

在此处输入图像描述

是的,微光效果工作正常,下载图像时也会加载图像,但在微光效果期间,我得到了这些奇怪的后边缘,我试图在上述布局中更改某些视图的背景颜色,但没有任何效果,如果我删除微光效果和占位符,那么就没有边缘,显然也没有微光

4

1 回答 1

1

为了避免这些黑边,您可以将整个 CardView 包裹在com.facebook.shimmer.ShimmerFrameLayout如下所示的内部:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto_medium"
        android:maxLines="3"
        android:text="dddddddddddddddddddddddddddddddddddddd"
        android:textColor="@color/tab_text_color"
        android:textSize="@dimen/_14ssp"
        app:layout_constraintEnd_toEndOf="@+id/shimmerFrameLayout"
        app:layout_constraintStart_toStartOf="@+id/shimmerFrameLayout"
        app:layout_constraintTop_toBottomOf="@+id/shimmerFrameLayout" />

    <com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmerFrameLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:shimmer_base_alpha="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="4dp"
            app:cardCornerRadius="@dimen/_6sdp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="@dimen/_120sdp"
                    android:layout_height="@dimen/_180sdp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/edit_bg" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

    </com.facebook.shimmer.ShimmerFrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

您可以将 aShimmer.ColorHighlightBuilder()与自定义 BaseColor 和 HighlightColor 一起使用,如下所示:

val shimmer = Shimmer.ColorHighlightBuilder()
    .setDuration(2000)
    .setBaseAlpha(0.9f)
    .setHighlightAlpha(0.93f)
    .setWidthRatio(1.5f)
    .setDirection(Shimmer.Direction.LEFT_TO_RIGHT)
    .setAutoStart(true)
    .setBaseColor(ContextCompat.getColor(itemView.context, android.R.color.darker_gray))
    .setHighlightColor(ContextCompat.getColor(itemView.context,  android.R.color.white))
    .build()

前后结果:

before_image 后像

于 2021-06-29T10:45:02.903 回答