Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74dc69b

Browse files
committed
添加 Android Animation 使用栗子
1 parent 3072752 commit 74dc69b

5 files changed

Lines changed: 150 additions & 1 deletion

File tree

‎src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
android:name=".activity.PhotoPreviewActivity"
3030
android:label="@string/photo_preview"
3131
android:screenOrientation="portrait" />
32+
<activity android:name=".activity.AndroidAnimActivity"></activity>
3233
</application>
3334

3435
</manifest>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.clock.study.activity;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
import android.view.animation.AlphaAnimation;
7+
import android.view.animation.Animation;
8+
import android.view.animation.AnimationSet;
9+
import android.view.animation.RotateAnimation;
10+
import android.view.animation.ScaleAnimation;
11+
import android.view.animation.TranslateAnimation;
12+
import android.widget.Button;
13+
14+
import com.clock.study.R;
15+
16+
/**
17+
* Android动画效果实现复习
18+
*/
19+
public class AndroidAnimActivity extends AppCompatActivity implements View.OnClickListener {
20+
21+
private Button mBtnTranslate;
22+
private Button mBtnScale;
23+
private Button mBtnRotate;
24+
private Button mBtnAlpha;
25+
private Button mBtnSet;
26+
27+
@Override
28+
protected void onCreate(Bundle savedInstanceState) {
29+
super.onCreate(savedInstanceState);
30+
setContentView(R.layout.activity_android_anim);
31+
32+
mBtnTranslate = (Button) findViewById(R.id.btn_translate);
33+
mBtnTranslate.setOnClickListener(this);
34+
35+
mBtnScale = (Button) findViewById(R.id.btn_scale);
36+
mBtnScale.setOnClickListener(this);
37+
38+
mBtnRotate = (Button) findViewById(R.id.btn_rotate);
39+
mBtnRotate.setOnClickListener(this);
40+
41+
mBtnAlpha = (Button) findViewById(R.id.btn_alpha);
42+
mBtnAlpha.setOnClickListener(this);
43+
44+
mBtnSet = (Button) findViewById(R.id.btn_set);
45+
mBtnSet.setOnClickListener(this);
46+
47+
}
48+
49+
@Override
50+
public void onClick(View v) {
51+
int viewId = v.getId();
52+
if (viewId == R.id.btn_translate) {//偏移动画
53+
//TranslateAnimation translateAnim = new TranslateAnimation(0, 500, 0, 500);
54+
/*TranslateAnimation translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 500, Animation.ABSOLUTE, 0,
55+
Animation.ABSOLUTE, 500);*/
56+
TranslateAnimation translateAnim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0,
57+
Animation.RELATIVE_TO_PARENT, 1.0f);
58+
translateAnim.setDuration(2000);
59+
mBtnTranslate.startAnimation(translateAnim);
60+
//translateAnim.setFillAfter(true);//保持动画效果
61+
62+
} else if (viewId == R.id.btn_scale) {//缩放动画
63+
64+
//ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1);
65+
//ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1, 300, 300);
66+
ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
67+
scaleAnim.setDuration(1000);
68+
mBtnScale.startAnimation(scaleAnim);
69+
70+
} else if (viewId == R.id.btn_rotate) {//旋转动画
71+
72+
//RotateAnimation rotateAnim = new RotateAnimation(0, 360);
73+
//RotateAnimation rotateAnim = new RotateAnimation(0, 360, 100, 100);
74+
RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
75+
rotateAnim.setDuration(2000);
76+
mBtnRotate.startAnimation(rotateAnim);
77+
78+
} else if (viewId == R.id.btn_alpha) {//透明度动画
79+
80+
AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
81+
alphaAnim.setDuration(2000);
82+
mBtnAlpha.startAnimation(alphaAnim);
83+
84+
} else if (viewId == R.id.btn_set) {//动画合集
85+
86+
AnimationSet animSet = new AnimationSet(true);
87+
animSet.setDuration(2000);
88+
RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
89+
animSet.addAnimation(rotateAnim);
90+
AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
91+
animSet.addAnimation(alphaAnim);
92+
mBtnSet.startAnimation(animSet);
93+
94+
}
95+
96+
}
97+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected void onCreate(Bundle savedInstanceState) {
1515
setContentView(R.layout.activity_main);
1616

1717
findViewById(R.id.btn_camera_take_photo).setOnClickListener(this);
18+
findViewById(R.id.btn_android_anim).setOnClickListener(this);
1819
}
1920

2021
@Override
@@ -23,6 +24,9 @@ public void onClick(View v) {
2324
if (viewId == R.id.btn_camera_take_photo) {
2425
Intent takePhotoIntent = new Intent(this, CapturePhotoActivity.class);
2526
startActivity(takePhotoIntent);
27+
} else if (viewId == R.id.btn_android_anim) {
28+
Intent androidAnimIntent = new Intent(this, AndroidAnimActivity.class);
29+
startActivity(androidAnimIntent);
2630
}
2731
}
2832
}
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_translate"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:text="Translate Animation" />
16+
17+
<Button
18+
android:id="@+id/btn_scale"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:text="Scale Animation" />
22+
23+
<Button
24+
android:id="@+id/btn_rotate"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:text="Rotate Animation" />
28+
29+
<Button
30+
android:id="@+id/btn_alpha"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:text="Alpha Animation" />
34+
35+
<Button
36+
android:id="@+id/btn_set"
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:text="Animation Set" />
40+
</LinearLayout>

‎src/main/res/layout/activity_main.xml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:orientation="vertical"
56
android:paddingBottom="@dimen/activity_vertical_margin"
67
android:paddingLeft="@dimen/activity_horizontal_margin"
78
android:paddingRight="@dimen/activity_horizontal_margin"
@@ -11,5 +12,11 @@
1112
android:id="@+id/btn_camera_take_photo"
1213
android:layout_width="match_parent"
1314
android:layout_height="wrap_content"
14-
android:text="拍照保存实现代码" />
15+
android:text="拍照保存实现" />
16+
17+
<Button
18+
android:id="@+id/btn_android_anim"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:text="Android 动画实现" />
1522
</LinearLayout>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /