Skip to content

Instantly share code, notes, and snippets.

@mpost
Last active January 8, 2020 02:09
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mpost/180bf15676006a50c3f7 to your computer and use it in GitHub Desktop.
Save mpost/180bf15676006a50c3f7 to your computer and use it in GitHub Desktop.
This gist demonstrates how to create an animated pause/resume media playback button for Android. It uses animated vector drawables and state transitions to orchestrate the effect. Some background: https://plus.google.com/u/0/+MoritzPost/posts/3EFF8uC7jXv
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="action_pause">#FF8F00</color>
<color name="action_resume">#43A047</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="fillColor"
android:valueFrom="@color/action_pause"
android:valueTo="@color/action_resume" />
<?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="rotation"
android:valueFrom="0"
android:valueTo="90" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translateX"
android:valueFrom="0"
android:valueTo="8" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/drawable_vector_pause_left"
android:valueTo="@string/drawable_vector_resume_left"
android:valueType="pathType" />
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/drawable_vector_pause_right"
android:valueTo="@string/drawable_vector_resume_right"
android:valueType="pathType" />
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="fillColor"
android:valueFrom="@color/action_resume"
android:valueTo="@color/action_pause" />
<?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="rotation"
android:valueFrom="90"
android:valueTo="180" />
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translateX"
android:valueFrom="8"
android:valueTo="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/drawable_vector_resume_left"
android:valueTo="@string/drawable_vector_pause_left"
android:valueType="pathType" />
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/drawable_vector_resume_right"
android:valueTo="@string/drawable_vector_pause_right"
android:valueType="pathType" />
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="192dp"
android:height="192dp"
android:viewportWidth="192"
android:viewportHeight="192">
<path
android:name="background"
android:fillColor="@color/action_pause"
android:pathData="l192,0 0,192 -192,0 z" />
<group
android:name="rotationGroup"
android:pivotX="96"
android:pivotY="96">
<path
android:name="left"
android:fillColor="@android:color/white"
android:pathData="@string/drawable_vector_pause_left" />
<path
android:name="right"
android:fillColor="@android:color/white"
android:pathData="@string/drawable_vector_pause_right" />
</group>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item
android:id="@+id/pressed_selected"
android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/ic_resume" />
<item
android:id="@+id/action_resume_state"
android:drawable="@drawable/ic_resume"
android:state_selected="true" />
<item
android:id="@+id/action_pause_state"
android:drawable="@drawable/ic_pause" />
<transition
android:fromId="@id/pressed_selected"
android:toId="@id/action_resume_state"
android:drawable="@drawable/ic_pause_to_ic_resume" />
<transition
android:fromId="@id/pressed_selected"
android:toId="@id/action_pause_state"
android:drawable="@drawable/ic_resume_to_ic_pause" />
</animated-selector>
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_pause">
<target
android:name="background"
android:animation="@anim/drawable_pause_to_resume_background" />
<target
android:name="rotationGroup"
android:animation="@anim/drawable_pause_to_resume_group" />
<target
android:name="left"
android:animation="@anim/drawable_pause_to_resume_left" />
<target
android:name="right"
android:animation="@anim/drawable_pause_to_resume_right" />
</animated-vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="192dp"
android:height="192dp"
android:viewportWidth="192"
android:viewportHeight="192">
<path
android:name="background"
android:fillColor="@color/action_resume"
android:pathData="l192,0 0,192 -192,0 z" />
<group
android:name="rotationGroup"
android:pivotX="96"
android:pivotY="96"
android:translateX="8"
android:rotation="90">
<path
android:name="left"
android:fillColor="@android:color/white"
android:pathData="@string/drawable_vector_resume_left" />
<path
android:name="right"
android:fillColor="@android:color/white"
android:pathData="@string/drawable_vector_resume_right" />
</group>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_resume">
<target
android:name="background"
android:animation="@anim/drawable_resume_to_pause_background" />
<target
android:name="rotationGroup"
android:animation="@anim/drawable_resume_to_pause_group" />
<target
android:name="left"
android:animation="@anim/drawable_resume_to_pause_left" />
<target
android:name="right"
android:animation="@anim/drawable_resume_to_pause_right" />
</animated-vector>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="drawable_vector_pause_left">M72,64 l16,0 0,64 -16,0 z</string>
<string name="drawable_vector_pause_right">M104,64 l16,0 0,64 -16,0 z</string>
<string name="drawable_vector_resume_left">M96,64 l0,0 0,64 -40,0 z</string>
<string name="drawable_vector_resume_right">M96,64 l0,0 40,64 -40,0 z</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment