Technorati 标签: Android这个是ApiDemo中的一个小例子,我们可以试着自己写一遍,加上自己的注释。这个例子的位置为:API Demos – App – Activity – Animation
打开后如图所示,该程序的功能是演示Fade In(淡入)和Zoom In(缩放模式进入)的效果,可以点击一下看看效果。
在ApiDemo的源码中,这些例子是在一起的,所以它写的比较复杂,为了简单,第一个例子我们单独写它,等到写第二个的时候再进行重构,把他们组装到一起。
通过阅读源码我们可以发现,这个Activity对应的是Animation.java这个文件中的Animation类,下边是Animation.java的源文件:
1: /*
2: * Copyright (C) 2009 The Android Open Source Project
3: *
4: * Licensed under the Apache License, Version 2.0 (the "License");
5: * you may not use this file except in compliance with the License.
6: * You may obtain a copy of the License at
7: *
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package com.example.android.apis.app;
18:
19: // Need the following import to get access to the app resources, since this
20: // class is in a sub-package.
21: import com.example.android.apis.R;
22: import com.example.android.apis.view.Controls1;
23:
24: import android.app.Activity;
25: import android.content.ComponentName;
26: import android.content.Intent;
27: import android.os.Bundle;
28: import android.view.View;
29: import android.view.View.OnClickListener;
30: import android.widget.Button;
31:
32:
33: /**
34: *Example of explicitly starting and stopping the {@link LocalService}.
35: * This demonstrates the implementation of a service that runs in the same
36: * process as the rest of the application, which is explicitly started and stopped
37: * as desired.
38: */
39: public class Animation extends Activity {
40: @Override
41: protected void onCreate(Bundle savedInstanceState) {
42: super.onCreate(savedInstanceState);
43:
44: setContentView(R.layout.activity_animation);
45:
46: // Watch for button clicks.
47: Button button = (Button)findViewById(R.id.fade_animation);
48: button.setOnClickListener(mFadeListener);
49: button = (Button)findViewById(R.id.zoom_animation);
50: button.setOnClickListener(mZoomListener);
51: }
52:
53: private OnClickListener mFadeListener = new OnClickListener() {
54: public void onClick(View v) {
55: // Request the next activity transition (here starting a new one).
56: startActivity(new Intent(Animation.this, Controls1.class));
57: // Supply a custom animation. This one will just fade the new
58: // activity on top. Note that we need to also supply an animation
59: // (here just doing nothing for the same amount of time) for the
60: // old activity to prevent it from going away too soon.
61: overridePendingTransition(R.anim.fade, R.anim.hold);
62: }
63: };
64:
65: private OnClickListener mZoomListener = new OnClickListener() {
66: public void onClick(View v) {
67: // Request the next activity transition (here starting a new one).
68: startActivity(new Intent(Animation.this, Controls1.class));
69: // This is a more complicated animation, involving transformations
70: // on both this (exit) and the new (enter) activity. Note how for
71: // the duration of the animation we force the exiting activity
72: // to be Z-ordered on top (even though it really isn't) to achieve
73: // the effect we want.
74: overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
75: }
76: };
77: }
78:
暂时对我们有用的只有两行:21行和22行,有了这两行我们就可以大概明白程序的架构,我们找到其中的com.example.android.apis.view.Controls1(android-sdk/samples/android-7/ApiDemos/src/com/example/android/apis/view/Controls1.java)
1: /*
2: * Copyright (C) 2007 The Android Open Source Project
3: *
4: * Licensed under the Apache License, Version 2.0 (the "License");
5: * you may not use this file except in compliance with the License.
6: * You may obtain a copy of the License at
7: *
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package com.example.android.apis.view;
18:
19: // Need the following import to get access to the app resources, since this
20: // class is in a sub-package.
21: import com.example.android.apis.R;
22:
23: import android.app.Activity;
24: import android.os.Bundle;
25: import android.widget.Spinner;
26: import android.widget.ArrayAdapter;
27:
28:
29: /**
30: * A gallery of basic controls: Button, EditText, RadioButton, Checkbox,
31: * Spinner. This example uses the light theme.
32: */
33: public class Controls1 extends Activity {
34:
35: @Override
36: protected void onCreate(Bundle savedInstanceState) {
37: super.onCreate(savedInstanceState);
38: setContentView(R.layout.controls_1);
39:
40: Spinner s1 = (Spinner) findViewById(R.id.spinner1);
41: ArrayAdapter adapter = new ArrayAdapter ( this,
42: android.R.layout.simple_spinner_item, mStrings);
43: adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
44: s1.setAdapter(adapter);
45: }
46:
47: private static final String[] mStrings = {
48: "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"
49: };
50: }
我们发现,Controls1.java这个文件导入了com.example.android.apis.R这个包,不过上边的注释告诉我们,这是因为该类是一个sub-package。由于咱们的程序不是sub-package,所以不必导入。OK,下边就让咱们开工写了,首先我们先观察类Animation,其中我们看到,首先是第44行
44: setContentView(R.layout.activity_animation);
设置了该页面需要显示的xml文件,我们打开这个xml文件看一下
1: xml version="1.0" encoding="utf-8"?>
2:3:
4: Licensed under the Apache License, Version 2.0 (the "License");5: you may not use this file except in compliance with the License.6: You may obtain a copy of the License at7:8: http://www.apache.org/licenses/LICENSE-2.09:10: Unless required by applicable law or agreed to in writing, software11: distributed under the License is distributed on an "AS IS" BASIS,12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13: See the License for the specific language governing permissions and14: limitations under the License.15: -->16:
17:18: See corresponding Java code com.android.sdk.app.LocalSerice.java. -->19:
20: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"21: android:gravity="center_horizontal"22: android:layout_width="fill_parent" android:layout_height="fill_parent">23:
24: <TextView25: android:layout_width="fill_parent" android:layout_height="wrap_content"26: android:layout_weight="0"27: android:paddingBottom="4dip"28: android:text="@string/activity_animation_msg"/>29:
30: <Button android:id="@+id/fade_animation"31: android:layout_width="wrap_content" android:layout_height="wrap_content"32: android:text="@string/activity_animation_fade">33: <requestFocus />34: Button>35:
36: <Button android:id="@+id/zoom_animation"37: android:layout_width="wrap_content" android:layout_height="wrap_content"38: android:text="@string/activity_animation_zoom">39: Button>40:
41: LinearLayout>42:
其实不看也没问题,只是上边的那个界面,很简单就能写出来了,下边是我的:
1: xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:orientation="vertical"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent"
6: >
7: <TextView
8: android:layout_width="fill_parent"
9: android:layout_height="wrap_content"
10: android:text="@string/activity_animation_msg"
11: />
12: <Button
13: android:id="@+id/fade_animation"
14: android:layout_width="fill_parent"
15: android:layout_height="wrap_content"
16: android:text="@string/activity_animation_fade"
17: />
18: <Button
19: android:id="@+id/zoom_animation"
20: android:layout_width="fill_parent"
21: android:layout_height="wrap_content"
22: android:text="@string/activity_animation_zoom"
23: />
24: LinearLayout>
和ApiDemo不太一样,不过这只是布局,暂时不必大动干戈地去搞他,抓住主要矛盾嘛。之后就是新建了两个Button的引用,并且把对应的Listener设置了一下。我们自己也可以写一下(Animation.java),我们先把Button和对应的Listener联系起来,里边的东西等一会儿再填。
1: package lili.animation.demo;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.view.View.OnClickListener;
7: import android.widget.Button;
8:
9: public class Animation extends Activity
10: {
11: /** Called when the activity is first created. */
12: @Override
13: public void onCreate(Bundle savedInstanceState)
14: {
15: super.onCreate(savedInstanceState);
16: setContentView(R.layout.main);
17:
18: Button button = (Button)findViewById(R.id.fade_animation);
19: button.setOnClickListener(mFadeListener);
20: button = (Button)findViewById(R.id.zoom_animation);
21: button.setOnClickListener(mZoomListener);
22: }
23:
24:
25: private OnClickListener mFadeListener = new OnClickListener()
26: {
27:
28: public void onClick(View arg0)
29: {
30: // TODO Auto-generated method stub
31:
32: }
33:
34: };
35:
36: private OnClickListener mZoomListener = new OnClickListener()
37: {
38:
39: public void onClick(View v)
40: {
41: // TODO Auto-generated method stub
42:
43: }
44: };
45: }
我们通过阅读源码可以看出需要新建一个名字为Controls1的继承自Activity的类,他的作用是显示这些控件:
大概看了一下controls_1.xml这个文件,就是从上往下依次添加控件,其中的Spinner中的内容是后来动态添加的,OK,为了简单我们直接复制一下。
1: "1.0" encoding="utf-8"?>
2:3:
4: Licensed under the Apache License, Version 2.0 (the "License");5: you may not use this file except in compliance with the License.6: You may obtain a copy of the License at
7:
8: http://www.apache.org/licenses/LICENSE-2.09:
10: Unless required by applicable law or agreed to in writing, software
11: distributed under the License is distributed on an "AS IS" BASIS,12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: See the License for the specific language governing permissions and14: limitations under the License.
15: -->
16:
17: "http://schemas.android.com/apk/res/android"
18: android:orientation="vertical"19: android:layout_width="fill_parent"20: android:layout_height="fill_parent">21:
22:
23: android:orientation="vertical"24: android:layout_width="fill_parent"25: android:layout_height="wrap_content">26:
27:
28: android:text="@string/controls_1_save"29: android:layout_width="wrap_content"30: android:layout_height="wrap_content"/>31:
32: "@+id/edit"
33: android:layout_width="fill_parent"34: android:layout_height="wrap_content"/>35:
36: "@+id/check1"
37: android:paddingBottom="24sp"38: android:paddingTop="24sp"39: android:layout_width="wrap_content"40: android:layout_height="wrap_content"41: android:text="@string/controls_1_checkbox_1" />42:
43: "@+id/check2"
44: android:layout_width="wrap_content"45: android:layout_height="wrap_content"46: android:text="@string/controls_1_checkbox_2" />47:
48:
49: android:layout_width="fill_parent"50: android:layout_height="wrap_content"51: android:orientation="vertical">52:
53: "@+id/radio1"
54: android:layout_width="wrap_content"55: android:layout_height="wrap_content"56: android:text="@string/controls_1_radiobutton_1" />57:
58: "@+id/radio2"
59: android:layout_width="wrap_content"60: android:layout_height="wrap_content"61: android:text="@string/controls_1_radiobutton_2" />62:
63:
64:
65: "@+id/star"
66: style="?android:attr/starStyle"67: android:layout_width="wrap_content"68: android:layout_height="wrap_content"69: android:text="@string/controls_1_star" />70:
71: "@+id/toggle1"
72: android:layout_width="wrap_content"73: android:layout_height="wrap_content" />74:
75: "@+id/toggle2"
76: android:layout_width="wrap_content"77: android:layout_height="wrap_content" />78:
79: "@+id/spinner1"
80: android:layout_width="fill_parent"81: android:layout_height="wrap_content"82: android:drawSelectorOnTop="true"83: />
84:
85:
86: android:layout_width="fill_parent"87: android:layout_height="wrap_content"88: android:layout_marginTop="5dip"89: android:text="@string/textColorPrimary"90: android:textAppearance="?android:attr/textAppearanceLarge"91: android:focusable="true"92: />
93:
94:
95: android:layout_width="fill_parent"96: android:layout_height="wrap_content"97: android:layout_marginTop="5dip"98: android:text="@string/textColorSecondary"99: android:textAppearance="?android:attr/textAppearanceLarge"100: android:textColor="?android:attr/textColorSecondary"101: android:focusable="true"102: />
103:
104:
105: android:layout_width="fill_parent"106: android:layout_height="wrap_content"107: android:layout_marginTop="5dip"108: android:text="@string/textColorTertiary"109: android:textAppearance="?android:attr/textAppearanceLarge"110: android:textColor="?android:attr/textColorTertiary"111: android:focusable="true"112: />
113:
114:
115: style="?android:attr/listSeparatorTextViewStyle"116: android:text="@string/listSeparatorTextViewStyle"117: android:layout_marginTop="5dip"118: />
119:
120:
121:
122:
接下来我们就要写Controls1这个类了,从Eclipse新建类,继承于Activity(这个不懂的朋友请自己查找)。
重写onCreate函数,我们着急先看一下效果,所以我们在Controls1.java这个文件中写下了这些:
1: package lili.animation.demo;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5:
6: public class Controls1 extends Activity
7: {
8:
9: @Override
10: protected void onCreate(Bundle savedInstanceState)
11: {
12: // TODO Auto-generated method stub
13: super.onCreate(savedInstanceState);
14:
15: setContentView(R.layout.controls_1);
16: }
17: }
先把我们写的layout显示出来,效果过一会儿再改。
想要启动一个新的Activity,需要在AndroidManifest.xml中添加一些东西,找到Activity标签,在下边新建一个Activity标签,也就是这样:
1: <activity android:name=".Animation"
2: android:label="@string/app_name">
3: <intent-filter>
4: <action android:name="android.intent.action.MAIN" />
5: <category android:name="android.intent.category.LAUNCHER" />
6: intent-filter>
7: activity>
8: <activity android:name=".Controls1">
9: activity>
其中第7到第8行是我们新建的
然后我们修改Animation.java中Fade In的onClick函数:
1: private OnClickListener mFadeListener = new OnClickListener()
2: {
3:
4: public void onClick(View arg0)
5: {
6: // TODO Auto-generated method stub
7: startActivity(new Intent(Animation.this, Controls1.class));
8: }
9:
10: };
试验一下,没问题不过是普通切屏的效果,我们需要的是Fade In,我们发现,源码中startActivity下边有一个
1: overridePendingTransition(R.anim.fade, R.anim.hold);
可以大概猜出来,这个的功能就是使程序淡入。我们通过查文档发现:
1: public void overridePendingTransition (int enterAnim, int exitAnim)
2: Since: API Level 5 Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
3:
4: Parameters
5: enterAnim A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
6: exitAnim A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
enterAnim是第二个Activity进入的效果,exitAnim是第一个Activity退出的效果,对应的两个xml文件我们可以看下边:
fade.xml
1: xml version="1.0" encoding="utf-8"?>
2:3:
4: Licensed under the Apache License, Version 2.0 (the "License");5: you may not use this file except in compliance with the License.6: You may obtain a copy of the License at7:8: http://www.apache.org/licenses/LICENSE-2.09:10: Unless required by applicable law or agreed to in writing, software11: distributed under the License is distributed on an "AS IS" BASIS,12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13: See the License for the specific language governing permissions and14: limitations under the License.15: -->16:
17: <alpha xmlns:android="http://schemas.android.com/apk/res/android"18: android:interpolator="@android:anim/accelerate_interpolator"19: android:fromAlpha="0.0" android:toAlpha="1.0"20: android:duration="@android:integer/config_longAnimTime" />
hold.xml
1: xml version="1.0" encoding="utf-8"?>
2:3:
4: Licensed under the Apache License, Version 2.0 (the "License");5: you may not use this file except in compliance with the License.6: You may obtain a copy of the License at7:8: http://www.apache.org/licenses/LICENSE-2.09:10: Unless required by applicable law or agreed to in writing, software11: distributed under the License is distributed on an "AS IS" BASIS,12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13: See the License for the specific language governing permissions and14: limitations under the License.15: -->16:
17: <alpha xmlns:android="http://schemas.android.com/apk/res/android"18: android:interpolator="@android:anim/accelerate_interpolator"19: android:fromAlpha="0.0" android:toAlpha="1.0"20: android:duration="@android:integer/config_longAnimTime" />
里边大概的意思我们也不必搞太懂,能知道用fade和hold以后能出现什么效果就好了。
OK,接下来的Zoom In可以对照着源代码自己试着实现一下。
这篇我写的比较详细,以后应该就不会这样了,请不懂的朋友在下边跟帖,我尽量一一解答!