【问题标题】:Scale animation when user touches a recyclerview item当用户触摸 recyclerview 项目时缩放动画
【发布时间】:2019-09-06 22:24:43
【问题描述】:

我正在开发一个应用程序,其中有一个带有一些卡片的回收视图,并且希望在用户触摸卡片时创建放大效果并在释放触摸时缩小。上一张和下一张卡片在扩展时应尊重卡片的大小,而不是向后或向前

我想创建类似于this layout manager 的东西,但通过触摸屏幕而不是滚动。

   //RecyclerView
    recyclerView = view.findViewById(R.id.recyclerView)

    recyclerView.setHasFixedSize(false)

    recyclerView.layoutManager = CenterZoomLayoutManager(activity, LinearLayoutManager.VERTICAL, false)

如何制作这种效果? 提前致谢。

【问题讨论】:

  • 到目前为止,为了实现这一目标,您完成了哪些工作?您能否向我们展示代码以供我们查看和帮助?
  • @Matthew 已更新代码

标签: android android-recyclerview android-animation android-cardview


【解决方案1】:

您可以使用stateListAnimator 实现类似的效果。在此示例中,我将 stateListAnimator 附加到 recyclerView 项目。项目按下时比例为1,未按下时比例为0.8

recyclerview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/item_animal_height"
    android:background="#f00"
    android:stateListAnimator="@animator/list_animator"/>

res/animator/list_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <set>
            <objectAnimator android:propertyName="scaleX"
                android:valueTo="1"/>

            <objectAnimator android:propertyName="scaleY"
                android:valueTo="1"/>
        </set>
    </item>
    <item
        android:state_pressed="false">
        <set>
            <objectAnimator android:propertyName="scaleX"
                android:valueTo="0.8"/>

            <objectAnimator android:propertyName="scaleY"
                android:valueTo="0.8"/>
        </set>
    </item>
</selector>

【讨论】:

    【解决方案2】:

    请在onBindViewHolder中使用下面的代码来实现项目缩放

        itemView.setOnTouchListener { view, motionEvent ->
        when (motionEvent.action) {
             MotionEvent.ACTION_DOWN -> {                  
               view.animate().scaleX(1.10f).scaleY(1.10f).setDuration(100).start()
               (itemView.parent as RecyclerView).addOnItemTouchListener(object : 
               RecyclerView.OnItemTouchListener {
                 override fun onInterceptTouchEvent(rv: RecyclerView, 
                 e: MotionEvent): Boolean {
                       if (rv.scrollState == RecyclerView.SCROLL_STATE_DRAGGING) {
                 view.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start() 
                 }
                 return false               }
                 override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
                 override fun 
                 onRequestDisallowInterceptTouchEvent(disallowIntercept: 
                              Boolean) { }})
                 }
             MotionEvent.ACTION_UP -> {                     
                   view.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start()
                 }
             }
             true
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2017-12-20
      相关资源
      最近更新 更多