android - 当用户触摸 recyclerview 项目时缩放动画

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

我正在开发一个应用程序,其中有一个带有一些卡片的 recyclerview,我想在用户触摸卡片时创建放大效果,并在释放触摸时创建缩小效果。前一张和下一张卡片在扩展时应遵循卡片的大小,而不是向前或向后

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

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

    recyclerView.setHasFixedSize(false)

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

如何创建这种效果? 提前致谢。

最佳答案

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

result

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>

关于android - 当用户触摸 recyclerview 项目时缩放动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829139/

相关文章:

java try catch block 返回什么

android - 使用折叠工具栏布局实现 RecyclerView

android - XML 定义的动画中指定的持续时间是否真的以毫秒为单位?

android - Sprite 图像动画

java - 我的代码有什么问题?我想在Android中制作 'card-flip'动画

android - 介于IOS和安卓之间的谷歌

Android - 如何在 640 x 480 上锁定相机分辨率

php - 我无法使用 json 在本地主机服务器中发布数据和图像

android - RecyclerView onClickListener

android - 具有不同高度的 RecyclerView 项目