在 android 中以编程方式声明时,在卡片视图中显示黑色边缘的微光
shimmer showing black edges in card View when declaring it programmatically in android
首先,这是我的回收站视图项目布局
<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)
这是我得到的结果
是的,微光效果工作正常,图像在下载时也会加载,但在微光效果期间我得到这些奇怪的后边缘,我试图改变上述布局中某些视图的背景颜色但没有任何效果,如果我删除微光效果和占位符,那么就没有边缘,显然也没有微光
为了避免这些黑边,您可以将整个 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>
并且您可以将 Shimmer.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()
前后结果:
首先,这是我的回收站视图项目布局
<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)
这是我得到的结果
是的,微光效果工作正常,图像在下载时也会加载,但在微光效果期间我得到这些奇怪的后边缘,我试图改变上述布局中某些视图的背景颜色但没有任何效果,如果我删除微光效果和占位符,那么就没有边缘,显然也没有微光
为了避免这些黑边,您可以将整个 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>
并且您可以将 Shimmer.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()
前后结果: