I am following the article given here
I am trying to create a "like" button animation and need help since my animator does not seem to be doing anything
I have created the required animator file, created the stateListDrawable file and set it as the background to the CheckBox The animator file uses a set of objectAnimator tags which animate the scaleX, scaleY and translationZ properties.
This is my layout file "activity_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/checkbox"
android:button="@null"
android:checked="false"
android:background="@drawable/like_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:stateListAnimator="@animator/scale2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the stateListDrawable "like_icon.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_thumb_up_red_24dp"
android:state_checked="true" />
<item android:drawable="@drawable/ic_thumb_up_black_24dp" />
</selector>
This is my animator named "scale.xml". It is placed correctly in the "res/animator/" folder
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="scaleX"
android:valueTo="1.525"
android:valueType="floatType" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="scaleY"
android:valueTo="1.525"
android:valueType="floatType" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translationZ"
android:valueTo="4dp"
android:valueType="floatType" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="scaleX"
android:valueTo="1.0"
android:startOffset="@android:integer/config_longAnimTime"
android:valueType="floatType" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="scaleY"
android:startOffset="@android:integer/config_longAnimTime"
android:valueTo="1.0"
android:valueType="floatType" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translationZ"
android:startOffset="@android:integer/config_longAnimTime"
android:valueTo="0dp"
android:valueType="floatType" />
</set>
Aucun commentaire:
Enregistrer un commentaire