Skip to content

Commit

Permalink
add state animation demo;
Browse files Browse the repository at this point in the history
  • Loading branch information
OCNYang committed Mar 29, 2018
1 parent f4d7d7b commit 76fbc4e
Show file tree
Hide file tree
Showing 36 changed files with 262 additions and 5 deletions.
Expand Up @@ -15,6 +15,7 @@
import com.ocnyang.propertyanimation.PropertyAnimationActivity;
import com.ocnyang.revealanimation.RevealAnimationActivity;
import com.ocnyang.rippleanimation.TouchFeedbackActivity;
import com.ocnyang.stateanimation.StateAnimationActivity;
import com.ocnyang.transitionanimation.TransitionAnimationActivity;
import com.ocnyang.viewanimation.ViewAnimationActivity;

Expand Down Expand Up @@ -74,6 +75,7 @@ public void onClick(View v) {
mContext.startActivity(new Intent(mContext,TransitionAnimationActivity.class));
break;
case 6:
mContext.startActivity(new Intent(mContext, StateAnimationActivity.class));
break;
case 7:
break;
Expand Down
12 changes: 8 additions & 4 deletions state-animation/README.md
Expand Up @@ -36,8 +36,12 @@
> 如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。
View 状态改变的动画主要是两个类:
1. StateListAnimator 是个动画, 在res/anim中
2. AnimatedStateListDrawable 是个 Drawable, 在res/drawable中。
1. StateListAnimator 是个动画, 在 res/anim (或者 res/animator)中
2. AnimatedStateListDrawable 是个 Drawable, 在 res/drawable 中。

> ps:这里的 StateListAnimator 大多教程都说放在 `res/anim` 目录下,放在此目录下能够正常使用,但在 AS3.0.1 中会有警示
*`<selector> XML file should be in either "animator" or "drawable", not "anim"`*
> 我尝试放到 `res/animator` 后不再有警示,并且完全不影响使用。所以这里建议大家以后都放到此目录下。
### StateListAnimator

Expand Down Expand Up @@ -81,8 +85,8 @@ xml文件:你可以改成任意一种objectAnimator动画。 这里使用的

### AnimatedStateListDrawable

xml布局:这个效果有点意思
当你是 pressed 状态的时候 animation-list 正着走一遍,drawable 使用最后一个。
xml布局:这个效果有点意思(ps:但没卵用啊,目前我是没有发现那种场景可以使用)
当你是 pressed 状态的时候 animation-list 正着走一遍,drawable 使用最后一个。
当你是 default 状态时 animation-list 反着走一遍,drawable 使用第一个。

`res/drawable/myanimstatedrawable`
Expand Down
1 change: 1 addition & 0 deletions state-animation/build.gradle
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
Expand Down
10 changes: 9 additions & 1 deletion state-animation/src/main/AndroidManifest.xml
@@ -1,2 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ocnyang.stateanimation"/>
package="com.ocnyang.stateanimation">

<application>
<activity android:name=".StateAnimationActivity">
</activity>
</application>

</manifest>
@@ -0,0 +1,39 @@
package com.ocnyang.stateanimation;

import android.animation.AnimatorInflater;
import android.animation.StateListAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class StateAnimationActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_animation);
initToolbar();
setStateListAnimator();
}

private void setStateListAnimator() {
StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(this, R.animator.anim_view_state_change_2);
findViewById(R.id.view_puppet2).setStateListAnimator(stateListAnimator);
}

private void initToolbar() {
Toolbar toolbar = ((Toolbar) findViewById(R.id.toolbar));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
23 changes: 23 additions & 0 deletions state-animation/src/main/res/anim/anim_view_state_change.xml
@@ -0,0 +1,23 @@
<?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:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="7dp"
android:valueType="floatType"/>
</set>
</item>

<item
android:state_pressed="false">
<set>
<objectAnimator
android:duration="100"
android:propertyName="translationZ"
android:valueTo="2dp"
android:valueType="floatType"/>
</set>
</item>
</selector>
33 changes: 33 additions & 0 deletions state-animation/src/main/res/animator/anim_view_state_change_2.xml
@@ -0,0 +1,33 @@
<?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:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="5dp"
android:valueType="floatType"/>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="rotationX"
android:valueTo="10"
android:valueType="floatType"/>
</set>
</item>

<item
android:state_pressed="false">
<set>
<objectAnimator
android:duration="100"
android:propertyName="translationZ"
android:valueTo="2dp"
android:valueType="floatType"/>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="rotationX"
android:valueTo="0"
android:valueType="floatType"/>
</set>
</item>
</selector>
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<animated-selector
xmlns:android="http://schemas.android.com/apk/res/android">

<!-- provide a different drawable for each state -->
<item
android:id="@+id/pressed"
android:drawable="@drawable/img29"
android:state_pressed="true"/>
<!-- <item
android:id="@+id/focused"
android:drawable="@drawable/btn_focused"
android:state_focused="true"/> -->
<item
android:id="@id/default1"
android:drawable="@drawable/img1"/>

<!-- specify a transition -->
<transition
android:fromId="@+id/default1"
android:toId="@+id/pressed">
<animation-list>
<item
android:drawable="@drawable/img2"
android:duration="100"/>
<item
android:drawable="@drawable/img3"
android:duration="100"/>
<item
android:drawable="@drawable/img4"
android:duration="100"/>
<item
android:drawable="@drawable/img5"
android:duration="100"/>
<item
android:drawable="@drawable/img6"
android:duration="100"/>
<item
android:drawable="@drawable/img7"
android:duration="100"/>
<item
android:drawable="@drawable/img8"
android:duration="100"/>
<item
android:drawable="@drawable/img9"
android:duration="100"/>
<item
android:drawable="@drawable/img10"
android:duration="100"/>
<item
android:drawable="@drawable/img13"
android:duration="100"/>
<item
android:drawable="@drawable/img14"
android:duration="100"/>
<item
android:drawable="@drawable/img15"
android:duration="100"/>
<item
android:drawable="@drawable/img17"
android:duration="100"/>
<item
android:drawable="@drawable/img18"
android:duration="100"/>
<item
android:drawable="@drawable/img19"
android:duration="100"/>
<item
android:drawable="@drawable/img20"
android:duration="100"/>
<item
android:drawable="@drawable/img21"
android:duration="100"/>
<item
android:drawable="@drawable/img22"
android:duration="100"/>
<item
android:drawable="@drawable/img23"
android:duration="100"/>
<item
android:drawable="@drawable/img24"
android:duration="100"/>
<item
android:drawable="@drawable/img25"
android:duration="100"/>
<item
android:drawable="@drawable/img26"
android:duration="100"/>
<item
android:drawable="@drawable/img27"
android:duration="100"/>
<item
android:drawable="@drawable/img28"
android:duration="100"/>
<item
android:drawable="@drawable/img29"
android:duration="100"/>
</animation-list>
</transition>
</animated-selector>
Binary file added state-animation/src/main/res/drawable/img1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img14.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img15.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img17.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img18.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img20.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img21.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img22.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img23.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img24.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img25.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img27.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img28.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img29.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added state-animation/src/main/res/drawable/img8.png
Binary file added state-animation/src/main/res/drawable/img9.png
47 changes: 47 additions & 0 deletions state-animation/src/main/res/layout/activity_state_animation.xml
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ocnyang.stateanimation.StateAnimationActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"/>

<TextView
android:id="@+id/view_puppet1"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="@android:color/white"
android:clickable="true"
android:stateListAnimator="@anim/anim_view_state_change"/>

<TextView
android:id="@+id/view_puppet2"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="@android:color/white"
android:clickable="true"/>

<View
android:id="@+id/view_puppet3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="@drawable/bg_animated_statelist_drawable"
android:clickable="true"/>


</LinearLayout>

0 comments on commit 76fbc4e

Please sign in to comment.