Skip to content

Commit b44ef50

Browse files
committed
添加Animator练习代码
1 parent 28fc432 commit b44ef50

8 files changed

Lines changed: 160 additions & 5 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ android {
1717

1818
defaultConfig {
1919
applicationId "com.clock.study"
20-
minSdkVersion 9
20+
minSdkVersion 11
2121
targetSdkVersion 23
2222
versionCode 1
2323
versionName "1.0"

src/main/AndroidManifest.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@
2929
android:name=".activity.PhotoPreviewActivity"
3030
android:label="@string/photo_preview"
3131
android:screenOrientation="portrait" />
32-
<activity android:name=".activity.AnimationActivity" />
33-
<activity android:name=".activity.AnimationTestActivity"/>
32+
<activity
33+
android:name=".activity.AnimationActivity"
34+
android:label="@string/android_animation" />
35+
<activity
36+
android:name=".activity.AnimationTestActivity"
37+
android:label="@string/animation_test" />
38+
<activity
39+
android:name=".activity.AnimatorActivity"
40+
android:label="@string/android_animator" />
3441
</application>
3542

3643
</manifest>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.clock.study.activity;
2+
3+
import android.animation.Animator;
4+
import android.animation.AnimatorInflater;
5+
import android.animation.AnimatorSet;
6+
import android.animation.ObjectAnimator;
7+
import android.animation.ValueAnimator;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.view.View;
12+
import android.view.animation.AnimationSet;
13+
14+
import com.clock.study.R;
15+
16+
/**
17+
* About Android Animator
18+
*
19+
* @author Clock
20+
* @since 2016-07-21
21+
*/
22+
public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener {
23+
24+
private static final String TAG = AnimatorActivity.class.getSimpleName();
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.activity_animator);
30+
31+
findViewById(R.id.btn_value_anim).setOnClickListener(this);
32+
findViewById(R.id.btn_object_anim_alpha).setOnClickListener(this);
33+
findViewById(R.id.btn_object_anim_rotation).setOnClickListener(this);
34+
findViewById(R.id.btn_object_anim_set).setOnClickListener(this);
35+
findViewById(R.id.btn_object_anim_xml).setOnClickListener(this);
36+
37+
}
38+
39+
@Override
40+
public void onClick(View v) {
41+
int viewId = v.getId();
42+
if (viewId == R.id.btn_value_anim) {
43+
ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
44+
valueAnimator.setDuration(3000);
45+
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
46+
@Override
47+
public void onAnimationUpdate(ValueAnimator animation) {
48+
int currentValue = (int) animation.getAnimatedValue();
49+
Log.i(TAG, "currentValue: " + currentValue);
50+
}
51+
});
52+
valueAnimator.start();
53+
} else if (viewId == R.id.btn_object_anim_alpha) {
54+
ObjectAnimator alphaObjectAnimator = ObjectAnimator.ofFloat(v, "alpha", 1f, 0f, 1f);
55+
alphaObjectAnimator.setDuration(3000);
56+
alphaObjectAnimator.start();
57+
} else if (viewId == R.id.btn_object_anim_rotation) {
58+
ObjectAnimator rotationObjectAnimator = ObjectAnimator.ofFloat(v, "rotation", 0f, 360f);
59+
rotationObjectAnimator.setDuration(3000);
60+
rotationObjectAnimator.start();
61+
} else if (viewId == R.id.btn_object_anim_set) {
62+
AnimatorSet animatorSet = new AnimatorSet();
63+
ObjectAnimator alphaObjectAnimator = ObjectAnimator.ofFloat(v, "alpha", 1f, 0f, 1f);
64+
ObjectAnimator rotationObjectAnimator = ObjectAnimator.ofFloat(v, "rotation", 0f, 360f);
65+
animatorSet.play(alphaObjectAnimator).with(rotationObjectAnimator);
66+
//animatorSet.play(alphaObjectAnimator).after(rotationObjectAnimator);
67+
//animatorSet.playTogether(alphaObjectAnimator, rotationObjectAnimator);
68+
animatorSet.setDuration(3000);
69+
animatorSet.start();
70+
} else if (viewId == R.id.btn_object_anim_xml) {
71+
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.simple_animator);
72+
animatorSet.setTarget(v);
73+
animatorSet.start();
74+
}
75+
}
76+
}

src/main/java/com/clock/study/activity/MainActivity.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ protected void onCreate(Bundle savedInstanceState) {
1616

1717
findViewById(R.id.btn_camera_take_photo).setOnClickListener(this);
1818
findViewById(R.id.btn_animation).setOnClickListener(this);
19+
findViewById(R.id.btn_animator).setOnClickListener(this);
20+
1921
}
2022

2123
@Override
@@ -25,8 +27,11 @@ public void onClick(View v) {
2527
Intent takePhotoIntent = new Intent(this, CapturePhotoActivity.class);
2628
startActivity(takePhotoIntent);
2729
} else if (viewId == R.id.btn_animation) {
28-
Intent androidAnimIntent = new Intent(this, AnimationActivity.class);
29-
startActivity(androidAnimIntent);
30+
Intent animationIntent = new Intent(this, AnimationActivity.class);
31+
startActivity(animationIntent);
32+
} else if (viewId == R.id.btn_animator) {
33+
Intent animatorIntent = new Intent(this, AnimatorActivity.class);
34+
startActivity(animatorIntent);
3035
}
3136
}
3237
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:ordering="together">
4+
5+
<objectAnimator
6+
android:duration="3000"
7+
android:propertyName="rotation"
8+
android:valueFrom="0f"
9+
android:valueTo="360f"
10+
android:valueType="floatType" />
11+
12+
<objectAnimator
13+
android:duration="3000"
14+
android:propertyName="alpha"
15+
android:valueFrom="0f"
16+
android:valueTo="1f"
17+
android:valueType="floatType" />
18+
</set>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
android:paddingBottom="@dimen/activity_vertical_margin"
7+
android:paddingLeft="@dimen/activity_horizontal_margin"
8+
android:paddingRight="@dimen/activity_horizontal_margin"
9+
android:paddingTop="@dimen/activity_vertical_margin">
10+
11+
<Button
12+
android:id="@+id/btn_value_anim"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:text="Value Animator" />
16+
17+
<Button
18+
android:id="@+id/btn_object_anim_alpha"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:text="Object Animator (Alpha)" />
22+
23+
<Button
24+
android:id="@+id/btn_object_anim_rotation"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:text="Object Animator (Rotation)" />
28+
29+
<Button
30+
android:id="@+id/btn_object_anim_set"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:text="Object Animator (Set)" />
34+
35+
<Button
36+
android:id="@+id/btn_object_anim_xml"
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:text="Object Animator (Xml)" />
40+
</LinearLayout>

src/main/res/layout/activity_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919
android:layout_width="match_parent"
2020
android:layout_height="wrap_content"
2121
android:text="Android Animation" />
22+
23+
<Button
24+
android:id="@+id/btn_animator"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:text="Android Animator" />
2228
</LinearLayout>

src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
<string name="help_content">当前权限被禁用,建议到设置界面开启权限!</string>
1010
<string name="cancel">取消</string>
1111
<string name="settings">设置</string>
12+
<string name="android_animation">Android Animation</string>
13+
<string name="android_animator">Android Animator</string>
14+
<string name="animation_test">Animation Test</string>
1215
</resources>

0 commit comments

Comments
 (0)