Android-SDK Samples --- ApiDemos(Animation)

Technorati 标签:
这个是ApiDemo中的一个小例子,我们可以试着自己写一遍,加上自己的注释。这个例子的位置为:API Demos – App – Activity – Animation

QQ截图未命名

打开后如图所示,该程序的功能是演示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 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: 
      
      

    
  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:     <TextView
  25:         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的类,他的作用是显示这些控件:

QQ截图未命名

大概看了一下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.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: 
    
    
     
     "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:     };

试验一下,没问题不过是普QQ截图未命名通切屏的效果,我们需要的是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 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: <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 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: <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可以对照着源代码自己试着实现一下。

这篇我写的比较详细,以后应该就不会这样了,请不懂的朋友在下边跟帖,我尽量一一解答!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值