android recyclerView 动画有锯齿
时间: 2023-11-16 21:30:23 浏览: 154
如果在 Android `RecyclerView` 中使用默认的动画效果时出现锯齿,可以尝试以下解决方案:
1. 设置 `RecyclerView` 的 `clipToPadding` 属性为 `false`。
```xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
```
2. 在 `RecyclerView` 中使用 `ItemDecoration`。
```kotlin
class ItemDecoration : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
outRect.set(0, 0, 0, 1) // 设置底部分割线,数值可以根据自己的需求调整
}
override fun onDraw(
canvas: Canvas,
parent: RecyclerView,
state: RecyclerView.State
) {
super.onDraw(canvas, parent, state)
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val left = child.left + params.leftMargin
val right = child.right + params.rightMargin
val top = child.bottom + params.bottomMargin
val bottom = top + 1 // 设置底部分割线,数值可以根据自己的需求调整
canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
}
}
private val paint = Paint().apply {
color = Color.parseColor("#e5e5e5") // 分割线颜色
style = Paint.Style.FILL
}
}
```
3. 使用第三方库 `RecyclerView Animators`。
```gradle
dependencies {
implementation 'jp.wasabeef:recyclerview-animators:4.0.2'
}
```
```kotlin
val animator = SlideInUpAnimator(OvershootInterpolator(1f))
recyclerView.itemAnimator = animator
```
希望以上解决方案可以帮助到你。
阅读全文