【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTransition ActivityOptions类
App->Activity->Animation示例用于演示不同Activity切换时动态效果。
android 5.0例子中定义了6种动画效果:
渐变Fade In 缩放Zoom In Modern fade In Modern zoom in Scale up Thumbnail zoom
Android 中 Animation 资源可以分为两种:
- Tween Animation 对单个图像进行各种变换(缩放,平移,旋转等)来实现动画。
- Frame Animation 由一组图像顺序显示显示动画。
Animation 中使用的是Tween Animation, 使用的资源为R.anim.fade, R.anim.hold,R.anim.zoom_enter, R.anim.zoom_exit。
其中R.anim.fade, R.anim.zoom_enter分别为Fade In 和 Zoom动画资源。其定义为
fade.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" />
zoom_center.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<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>
tween animation 资源定义的格式如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”
android:interpolator=”@[package:]anim/interpolator_resource”
android:shareInterpolator=[ ” true ” false “>
<alpha
android:fromAlpha=”float”
android:toAlpha=”float” />
<scale
android:fromXScale=”float”
android:toXScale=”float”
android:fromYScale=”float”
android:toYScale=”float”
android:pivotX=”float”
android:pivotY=”float” />
<translate
android:fromXDelta=”float”
android:toXDelta=”float”
android:fromYDelta=”float”
android:toYDelta=”float” />
<rotate
android:fromDegrees=”float”
android:toDegrees=”float”
android:pivotX=”float”
android:pivotY=”float” />
<set> …
</set>
</set>
<set> 为其它animation类型<alpha>,<scale>,<translate>和<rotate>或其它<set>的容器。
android:interpolator 为Interpolator资源ID,Interpolator定义了动画的变化速率,动画的各帧的显示可以加速,减速,重复显示。
android:shareInterpolator 如果想为<set>中的各个子动画定义共享interpolator,shareInterpolator 则设为true.
<alpha> 定义Fade in ,Fade out 动画,其对应的Android类AlphaAnimation,参数由fromAlpha,toAlpha定义。
<scale>定义缩放动画,其对应的Android类为ScaleAnimation,参数由 fromXScale,toXScale,fromYScale,toYScale,pivotX,pivotY定义,pivotX,pivotY定义了 缩放时的中心。
<translate>定义平移动画,其对应的Android类为TranslateAnimation,参数由fromXDelta,toXDelta,fromYDelta,toYDelta定义。
<rotate>定义选择动画,其对应的Android类RotateAnimation,参数由fromDegrees,toDegrees,pivotX,pivotY, pivotX,pivotY定义选择中心。
Animation中的Fade In和Zoom In按钮的事件处理代码:
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, AlertDialogSamples.class));
// Supply a custom animation. This one will just fade the new
// activity on top. Note that we need to also supply an animation
// (here just doing nothing for the same amount of time) for the
// old activity to prevent it from going away too soon.
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
private OnClickListener mZoomListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, AlertDialogSamples.class));
// This is a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
};
从代码可以看到Activity Animation到其它Activity Controls1 切换的动画使用overridePendingTransition 来定义,函数overridePendingTransition(int enterAnim, int exitAnim) 必须定义在StartActivity(Intent)或是 Activity.finish()之后来定义两个Activity切换时的动画,enterAnim 为新Activity出现时动画效果,exitAnim则定义了当前Activity退出时动画效果。
剩下的Modern fade In, Modern zoom in, Scale up ,Thumbnail zoom使用ActivityOptions实现Activity跳转的动画效果:
private OnClickListener mModernFadeListener = new OnClickListener() {
public void onClick(View v) {
// Create the desired custom animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.fade, R.anim.hold);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
};
private OnClickListener mModernZoomListener = new OnClickListener() {
public void onClick(View v) {
// Create a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.zoom_enter, R.anim.zoom_enter);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
};
private OnClickListener mScaleUpListener = new OnClickListener() {
public void onClick(View v) {
// Create a scale-up animation that originates at the button
// being pressed.
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(
v, 0, 0, v.getWidth(), v.getHeight());
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
};
private OnClickListener mZoomThumbnailListener = new OnClickListener() {
public void onClick(View v) {
// Create a thumbnail animation. We are going to build our thumbnail
// just from the view that was pressed. We make sure the view is
// not selected, because by the time the animation starts we will
// have finished with the selection of the tap.
v.setDrawingCacheEnabled(true);
v.setPressed(false);
v.refreshDrawableState();
Bitmap bm = v.getDrawingCache();
Canvas c = new Canvas(bm);
//c.drawARGB(255, 255, 0, 0);
ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation(
v, bm, 0, 0);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
v.setDrawingCacheEnabled(false);
}
};
Activity跳转动画是指一个Activity跳转到另外一个Activity的动画效果,我们可以通过Acitivity的overridePendingTransition()方法实现跳转动画。
这个函数接受两个参数:
- 第一个参数为第一个Activity退出时的动画
- 第二个参数为第二个Activity进入时的动画
这个方法必须在startActivity方法或者finish方法之后调用。
示例:
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
Android 4.1(API16)提供了一个新类ActivityOptions,用来实现Activity的切换动画。
ActivityOptions类提供了三个方法
- makeCustomAnimation() 创建一个动画,由你自己的资源所定义:一个用来定义活动开启的动画,一个用来定义活动被关闭的动画。
- makeScaleUpAnimation() 创建一个动画,能够从屏幕指定的位置和指定的大小拉伸一个活动窗口。例如,当打开一个应用时,Android 4.1的主屏幕使用了这个方法。
- makeThumbnailScaleUpAnimation() 创建一个动画,能够从屏幕指定的位置和提供的缩略图拉伸一个活动窗口。例如,在Android 4.1的最近使用程序窗口中,当往回一个应用程序时使用了这个动画。
通过这三个方法中的一个就可以创建一个ActivityOptions示例,然后调用toBundle()方法获取一个Bundle对象,传递给startActivity方法。
示例:
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
ActivityOptions opts = ActivityOptions.makeCustomAnimation(MainActivity.this, R.anim.fade, R.anim.hold);
startActivity(intent, opts.toBundle());
【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTransition ActivityOptions类的更多相关文章
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
前给例子介绍了如何使用PreferenceActivity 来显示修改应用偏好,用户对Preferences的修改自动存储在应用对应的Shared Preferences中. 本例介绍了如何从一个Ac ...
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:
- 【起航计划 032】2015 起航计划 Android APIDemo的魔鬼步伐 31 App->Search->Invoke Search 搜索功能 Search Dialog SearchView SearchRecentSuggestions
Search (搜索)是Android平台的一个核心功能之一,用户可以在手机搜索在线的或是本地的信息.Android平台为所有需要提供搜索或是查询功能的应用提 供了一个统一的Search Framew ...
随机推荐
- linux线程切换和进程切换
进程切换分两步: 1.切换页目录以使用新的地址空间 2.切换内核栈和硬件上下文 对于linux来说,线程和进程的最大区别就在于地址空间,对于线程切换,第1步是不需要做的,第2是进程和线程切换都要做的. ...
- Laplace(拉普拉斯)算子
[摘要] Laplace算子作为边缘检测之一,和Sobel算子一样也是工程数学中常用的一种积分变换,属于空间锐化滤波操作.拉普拉斯算子(Laplace Operator)是n维欧几里德空间中的一个二阶 ...
- Property 'XXX' not found on type java.lang.String解决方案
一,标签指令错误. 原指令标签: <%@ taglib prefix="c" uri="http://j ava.sun.com/jstl/core" % ...
- pre 标签 防止 其撑开 div...
pre 里面 的内容如果不换行,会导致 div 横向 出现 滚动条...加入下列 css可解决! pre{ white-space: pre-wrap; word-wrap: break-word; ...
- C语言中类型限定符
通常用类型和存储类别来描述一个变量. C90还增加了两个属性:恒常性(constancy).易变性(volatility): 分别用关键字const和volatile来声明. 这两个关键字创建的类型是 ...
- P3943 星空
传送门 观察题目数据,发现 k ≤ 8 ,可能可以从这里入手解决问题 考虑状态压缩 但是我们每次操作都会让一连串的序列改变,而序列的每个状态都是必须要知道的 很麻烦,所以考虑如何把一段区间表示地简单一 ...
- poj3713 Transferring Sylla 枚举+tarjan判割点
其实就是判断是否为三连通图 三连通图指的是去掉3个点就不连通的图,但是并没有直接求三连通的算法.著名的Tarjan算法可以求解连通和割点,再枚举删除一个点就能达到三连通的目的. 先看用例2,是由用例1 ...
- Office 下载地址
Office Professional Plus 2013 64位简体中文版文件名: cn_office_professional_plus_2013_x64_dvd_1134006.iso语言: C ...
- Spring Boot 实现ErrorController接口处理404、500等错误页面
在项目中我们遇到404找不到的错误.或者500服务器错误都需要配置相应的页面给用户一个友好的提示,而在Spring Boot中我们需要如何设置. 我们需要实现ErrorController接口,重写h ...
- gRPC框架
https://blog.csdn.net/shuanger_/article/details/47281381 https://grpc.io/ gRPC是利用通讯协议是HTTP2,序列化用prot ...