【问题标题】:Android Animations - Moving objects closerAndroid 动画 - 将对象移近
【发布时间】:2020-02-13 18:19:13
【问题描述】:

我一直在尝试为scenario 制作动画。基本上我想从一些开始 ImageViews 垂直排列并散布在屏幕上,在执行特定操作(按下按钮等)后,它们应该靠近。值得一提的是,ImageView 的数量可能会因情况而异(并不总是像图片中那样 3 个),因此更可取的是通用解决方案。

我的第一种方法是在Recyclerview 中列出图像,然后使用自定义ItemDecoration 传播图像,如下所示:

class CustomItemDecorator(private val spaceHeight: Int) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        with(outRect) {
            if (parent.getChildAdapterPosition(view) == 0) {
                top = spaceHeight
            }
            bottom = spaceHeight
        }
    }
}

通过删除 ItemDecorator (myRecyclerView.removeItemDecoration(my_decorator)) 我能够使图像更接近。 尽管这适用于任意数量的图像以改善用户体验,但我想动画这个过渡。我尝试将一些更复杂的动画应用到我的 RecyclerView,但到目前为止没有任何效果。

如果有任何建议,我会很高兴 :-)

【问题讨论】:

    标签: android android-recyclerview android-animation objectanimator item-decoration


    【解决方案1】:

    对于运行 Android 5+(API 级别 21+)的设备,您可以使用TransitionManager 来实现流畅的动画效果:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            TransitionManager.beginDelayedTransition(sceneRoot as ViewGroup, Slide())
    }
    recyclerView.removeItemDecoration(itemDecorator)
    

    这里,itemDecorator 是您要删除的 CustomItemDecorator 的实例,sceneRoot 是 ViewGroup 的一个(直接或间接)父级Views 属性(可见性、位置、大小)的变化应该被动画化。

    阅读有关动画Transitions 的更多信息:

    Animate layout changes using a transition

    Getting Started with Activity & Fragment Transitions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多