本代码示例介绍如何设定Activity间切换时的动画效果。本示例使用Eclipse的Android工程编译测试。

1.定义清单文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="my.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".Animation"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".Controls1"

android:label="@string/app_name"

android:theme="@android:style/Theme.Light">

</activity>

</application>

<uses-sdk android:minSdkVersion="9" />

</manifest>

2.定义字符串资源(res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, Aniation!</string>

<string name="app_name">Animation</string>

<string name="activity_animation_msg">Press a button to launch an activity with a custom animation.</string>

<string name="activity_animation_fade">Fade in</string>

<string name="activity_animation_zoom">Zoom in</string>

<string name="controls_1_save">Save</string>

<string name="controls_1_checkbox_1">Checkbox 1</string>

<string name="controls_1_checkbox_2">Checkbox 2</string>

<string name="controls_1_radiobutton_1">RadioButton 1</string>

<string name="controls_1_radiobutton_2">RadioButton 2</string>

<string name="controls_1_star">Star</string>

<string name="textColorPrimary">textColorPrimary</string>

<string name="textColorSecondary">textColorSecondary</string>

<string name="textColorTertiary">textColorTertiary</string>

<string name="listSeparatorTextViewStyle">listSeparatorTextViewStyle</string>

</resources>

3.定义渐入动画资源定义(res/anim/fade.xmlres/anim/hold.xml)

res/anim/fade.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 声明动画对象的透明度,本例使用渐入的方式,显示Activity

属性说明参照zoom_exit.xml -->

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"

android:fromAlpha="0.0" android:toAlpha="1.0"

android:duration="@android:integer/config_longAnimTime" />

res/anim/hold.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 声明动画对象的水平和垂直移动量,本例使用水平移动方式,

android:interpolator:指定在设定时间内动画移动过程中插补器,用于改善动画的平滑度

android:fromXDelta:指定动画开始时,动画对象的水平位置,可以用像素智设定,也可以用

相对父窗口宽度的百分比来设定。

android:toXDelta:指定动画结束时,动画对象的水平位置,可以用像素值来设定,也可以用

相对父窗口宽度的百分比来设定。

android:duration:指定动画的播放时间

-->

<translate xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"

android:fromXDelta="0" android:toXDelta="0"

android:duration="@android:integer/config_longAnimTime" />

4.定义缩放动画资源定义(res/anim/zoom_enter.xmlres/anim/zoom_exit.xml)

res/anim/zoom_enter.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 声明动画对象进入屏幕时的动画资源

android:interpolator:指定在设定时间内动画移动过程中插补器,用于改善动画的平滑度

-->

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/decelerate_interpolator">

<!-- 声明动画对象进入屏幕时的缩放动画,

属性说明参照zoom_exit.xml -->

<scale android:fromXScale="2.0" android:toXScale="1.0"

android:fromYScale="2.0" android:toYScale="1.0"

android:pivotX="50%p" android:pivotY="50%p"

android:duration="@android:integer/config_mediumAnimTime" />

</set>

res/anim/zoom_exit.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 声明Activity退出时使用的动画资源

android:interpolator:指定在设定时间内动画移动过程中插补器,用于改善动画的平滑度

android:zAdjustment:允许再动画播放期间,调整播放内容在Z轴方向的顺序,normal0):真正播放的

动画内容保持当前的Z轴顺序,top1):在动画播放期间,强制把当前播放的内容放到其他内容的上面;

bottom-1):在动画播放期间,强制把当前播放的内容放到其他内容之下。

-->

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/decelerate_interpolator"

android:zAdjustment="top">

<!-- 指定动画对象的缩放因子和播放时间

android:fromXScaleandroid:toXScale指定X轴的动画开始和结束时的缩放因子

android:fromYScaleandroid:toYScale指定Y轴的动画开始和结束时的缩放因子

android:pivoteX:在动画对象被缩放时,X轴要保留的原始尺寸的百分比。

android:pivoteY:在动画对象被缩放时,Y轴要保留的原始尺寸的百分比。

android:duration指定动画的播放时间

-->

<scale android:fromXScale="1.0" android:toXScale=".5"

android:fromYScale="1.0" android:toYScale=".5"

android:pivotX="50%p" android:pivotY="50%p"

android:duration="@android:integer/config_mediumAnimTime" />

<!-- 定义动画对象的透明度,该动画在动画缩放之后播放。

android:fromAlpha:指定动画初始时的透明度

android:toAlpha:指定动画结束时的透明度

android:duratiion:指定动画透明处理的执行时间

-->

<alpha android:fromAlpha="1.0" android:toAlpha="0"

android:duration="@android:integer/config_mediumAnimTime"/>

</set>

5.定义布局Activity布局资源(res/layout/activity_animation.xmlres/layout/controls_1.xml)

res/layout/activity_animation.xml文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/activity_animation_msg"/>

<Button android:id="@+id/fade_animation"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/activity_animation_fade">

<requestFocus />

</Button>

<Button android:id="@+id/zoom_animation"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/activity_animation_zoom">

</Button>

</LinearLayout>

res/layout/controls_1.xml文件

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button android:id="@+id/button"

android:text="@string/controls_1_save"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<EditText android:id="@+id/edit"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<CheckBox android:id="@+id/check1"

android:paddingBottom="24sp"

android:paddingTop="24sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/controls_1_checkbox_1" />

<CheckBox android:id="@+id/check2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/controls_1_checkbox_2" />

<RadioGroup

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<RadioButton android:id="@+id/radio1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/controls_1_radiobutton_1" />

<RadioButton android:id="@+id/radio2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/controls_1_radiobutton_2" />

</RadioGroup>

<CheckBox android:id="@+id/star"

style="?android:attr/starStyle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/controls_1_star" />

<ToggleButton android:id="@+id/toggle1"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<ToggleButton android:id="@+id/toggle2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

<Spinner android:id="@+id/spinner1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="true"

/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dip"

android:text="@string/textColorPrimary"

android:textAppearance="?android:attr/textAppearanceLarge"

android:focusable="true"

/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dip"

android:text="@string/textColorSecondary"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="?android:attr/textColorSecondary"

android:focusable="true"

/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dip"

android:text="@string/textColorTertiary"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="?android:attr/textColorTertiary"

android:focusable="true"

/>

<TextView

style="?android:attr/listSeparatorTextViewStyle"

android:text="@string/listSeparatorTextViewStyle"

android:layout_marginTop="5dip"

/>

</LinearLayout>

</ScrollView>

6.创建Activity窗口(Animation.javaControls1.java

Animation.java文件

package my.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.content.ComponentName;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass Animation extends Activity {

/** Activity在首次创建时调用这个方法 */

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//填充布局

setContentView(R.layout.activity_animation);

//查找渐入动画启动按钮,并设置Click事件监听器

Button button = (Button)findViewById(R.id.fade_animation);

button.setOnClickListener(mFadeListener);

//查找缩放动画启动按钮,并设置Click事件监听器

button = (Button)findViewById(R.id.zoom_animation);

button.setOnClickListener(mZoomListener);

}

/**

* 启动一个新的Activity,新旧Activity之间切换时,新的Activity使用渐入

* 的方式启动。

*/

private OnClickListener mFadeListener = new OnClickListener(){

publicvoid onClick(View v){

//启动Controls1Activity

startActivity(new Intent(Animation.this, Controls1.class));

//startActivity()方法之后立即调用,设定新的Activity进入和

//当前Activity退出时的动画。

overridePendingTransition(R.anim.fade, R.anim.hold);

}

};

/**

* 启动一个新的Activity,新旧Activity之间切换时,采用缩放的方式启动新的的Activity

*/

private OnClickListener mZoomListener = new OnClickListener(){

publicvoid onClick(View v){

//启动Controls1Activity

startActivity(new Intent(Animation.this, Controls1.class));

//startActivity()方法之后立即调用,设定新的Activity进入和

//当前Activity退出时的动画。

overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);

}

};

}

Controls1.java文件

package my.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Spinner;

import android.widget.ArrayAdapter;

publicclass Controls1 extends Activity {

//设定字符串数组,用于下拉列表中的单选项

privatestaticfinal String[] mStrings = {

"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"

};

/**

* Activity首次创建时,会调用这个方法。

*/

@Override

protectedvoid onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

//填充布局

setContentView(R.layout.controls_1);

//查找布局中的下拉列表组件

Spinner s1 = (Spinner)findViewById(R.id.spinner1);

//用字符串数组初始数组适配器

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item, mStrings);

//给适配器的下拉列表设置布局资源

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//把字符数组适配器与下拉列表组件关联在一起。

s1.setAdapter(adapter);

}

}

更多相关文章

  1. Android(安卓)欢迎页面
  2. android客户端连接人人网之二----获取朋友信息
  3. Android(安卓)内存泄漏 - Animator的使用与释放
  4. 【Android(安卓)开发教程】获取发送短信后的反馈
  5. 图片处理
  6. Android(安卓)ViewFlipper实现页面的滑动切换
  7. 百度地图系列01——最简单的调用百度地图
  8. 屏幕元素的层次
  9. Android(安卓)shap Animation

随机推荐

  1. android两种启动Service方式的区别
  2. Android(安卓)FFmpeg系列——2 播放音频
  3. 【Android】MTK Android(安卓)编译命令
  4. android,自定义ViewGroup,tagView,横竖摆
  5. android 使用uinput模拟输入设备的方法
  6. Android中AIDL的使用详解
  7. Android的ScrollView简单使用实例(附Demo
  8. Android实现3秒钟自动关闭界面
  9. Android(安卓)按钮快速点击问题的解决方
  10. Android(安卓)studio JNI中快速生成头文