簡體   English   中英

如何在cardview android中添加動畫?

[英]How to add a animation in cardview android?

如何在Android的CardView中添加動畫,如果我點擊CardView我想要顯示一些動畫。

這是我為CardView創建的CardView的xml。 如何在此內添加動畫:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardMaxElevation="6dp"
    card_view:contentPadding="5dp"
    android:layout_margin="5dp"
    >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:layout_toRightOf="@id/iv_icon"
            android:layout_toEndOf="@id/iv_icon"
            android:paddingLeft="10dp"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#848484"
            android:layout_below="@id/app_label"
            android:layout_toRightOf="@id/iv_icon"
            android:layout_toEndOf="@id/iv_icon"
            android:textStyle="italic"
            android:paddingLeft="10dp"
            />

    </RelativeLayout>
</android.support.v7.widget.CardView>

將這段代碼放在卡片視圖中並創建一個animation.xml並調用

android:stateListAnimator="@anim/animation"
android:foreground="?attr/selectableItemBackground"
        android:stateListAnimator="@anim/animation"
        android:clickable="true"

animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- animate the translationZ property of a view when pressed -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_enabled="true"
            android:state_pressed="true">
            <set>
                <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:propertyName="translationZ"
                    android:valueTo="6dp"
                    android:valueType="floatType"/>
            </set>
        </item>
        <item>
            <set>
                <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:propertyName="translationZ"
                    android:valueTo="0"
                    android:valueType="floatType"/>
            </set>
        </item>
    </selector>
</set>

同樣,您可以使用任何類型的動畫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM