您的位置:首页 > 移动开发

Android ApiDemos详解之App/Activity/Animation(3)

2013-04-25 19:17 429 查看
转载:/article/9556647.html

Android_ApiDemos详解之App->Activity->Animation

本篇是ApidDemos详解系列文章的第一篇,该系列文章的发布顺序很简单,就是按照ApiDemos中从列表由上至下的顺序,如下所示:

1. App->Activity

2. App->Alarm

3. App->DeviceAdmin

4. App->Dialog

5. App->Intents

6. App->LauncherShortcuts

7. App->Menu

8. App->Notification

9. App->Preferences

10. App->Search

11. App->Service

12. App->Text-To-Speech

13. App->VoiceRecognition

14. Content->Assets

15. Content->Provider

16. Content->Resources

17. Content->Storage

18. Graphics->AlphaBitmap

19. Graphics->AnimateDrawables

20. Graphics->Arcs

21. Graphics->BitmapDecode

22. Graphics->BitmapMesh

23. Graphics->BitmapPixels

24. Graphics->CameraPreview

25. Graphics->Clipping

26. Graphics->ColorFilters

27. Graphics->ColorMatrix

28. Graphics->Compass

29. Graphics->CreateBitmap

30. Graphics->Density

31. Graphics->Drawable

32. Graphics->FingerPaint

33. Graphics->Layers

34. Graphics->MeasureText

35. Graphics->OpenGLES

36. Graphics->PathEffects

37. Graphics->PathFillTypes

38. Graphics->Patterns

39. Graphics->Pictures

40. Graphics->Points

41. Graphics->PolyToPoly

42. Graphics->PurgeableBitmap

43. Graphics->Regions

44. Graphics->RoundRects

45. Graphics->ScaleToFit

46. Graphics->ScaleToTest

47. Graphics->SurfaceWindow

48. Graphics->SurfaceViewOverlay

49. Graphics->Sweep

50. Graphics->TextAlign

51. Graphics->TouchPaint

52. Graphics->Typefaces

53. Graphics->UnicodeChart

54. Graphics->Vertices

55. Graphics->Xfermodes

56. Media->AudioFx

57. Media->MediaPlayer

58. Media->VideoView

59. NFC->ForegroundDispatch

60. NFC->ForegroundNdefPush

61. NFC->TechFilter

62. OS->MorseCode

63. OS->RotationVector

64. OS->Sensors

65. OS->SMSMessaging

66. Text->Linkify

67. Text->LogTextBox

68. Text->Marquee

69. Views->Animation

70. Views->AutoComplete

71. Views->Buttons

72. Views->Chronometer

73. Views->Controls

74. Views->Custom

75. Views->DateWidgets

76. Views->ExpandableLists

77. Views->Focus

78. Views->Gallery

79. Views->Grid

80. Views->ImageButton

81. Views->ImageSwitcher

82. Views->ImageView

83. Views->LayoutAnimation

84. Views->Layouts

85. Views->Lists

86. Views->ProgressBar

87. Views->RadioGroup

88. Views->ScrollBars

89. Views->SecureView

90. Views->SeekBar

91. Views->Spinner

92. Views->Tabs

93. Views->TextSwitcher

94. Views->Visibility

95. Views->WebView

今天首先从App->Activity开始,其中包括Animation , Custom Dialog , Custom Title , Dialog , Forwarding ,HelloWorld , Persistent State , QuickContactsDemo , Receive Result ,Redirection ,Reorder Activites , Save & Restore
State , ScreenOrientation , SetWallpaper , Translucent , Translucent Blur ,Wallpaper.

第一篇为大家讲解

(1)App->Activity->Animation

这个Demo很简单,讲的就是两个Activity在跳转过程中的动画应用。如下图所示有两种变换效果:



Fade in按钮和Zoom in按钮分别通过不同的动画效果,实现了从Animation跳转至Controls1(Animation与Controls1均为继承自Activity)。跳转后的Activty界面如下:



首先来看一下Animation中的主要代码:

[java] view
plaincopy

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_animation);

// Watch for button clicks.

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

button.setOnClickListener(mFadeListener);

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

button.setOnClickListener(mZoomListener);

}

代码很简单,两个Button分别点击,分别进行相对应的跳转,首先来看第一个id是R.id.fade_animation的Button,该Button注册的监听器为自定义的mFadeListener,实现如下:

[java] view
plaincopy

privateOnClickListener mFadeListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activitytransition (here starting a new one).

startActivity(newIntent(Animation.this, Controls1.class));

// Supply a custom animation.This one will just fade the new

// activity on top. Notethat we need to also supply an animation

// (here just doing nothingfor the same amount of time) for the

// old activity to preventit from going away too soon.

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

}

}

第二个Button注册的监听器同样为自定义的mZoomListener,实现如下:

[java] view
plaincopy

privateOnClickListener mZoomListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activitytransition (here starting a new one).

startActivity(newIntent(Animation.this, Controls1.class));

// This is a morecomplicated animation, involving transformations

// on both this (exit) andthe new (enter) activity. Note how for

// the duration of theanimation 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);

}

}

startActivity(new Intent(Animation.this, Controls1.class));这一句相信大家都比较熟悉,实现的是两个Activity之间的跳转。而overridePendingTransition(R.anim.fade,R.anim.hold);需要稍微着重讲一下,该函数实现的是Activity切换时的动画,第一个参数为int型,是第一个Activity退出时的动画资源id;第二个参数同样也为int型,是第二个Activity进入时的动画资源id。

其中anim目录下的fade.xml主要代码如下所示:

[html] view
plaincopy

<alphaxmlns:androidalphaxmlns: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" />

hold.xml主要代码如下:

[html] view
plaincopy

<translatexmlns:androidtranslatexmlns: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" />

zoom_enter.xml主要代码如下:

[html] view
plaincopy

<setxmlns:androidsetxmlns:android="http://schemas.android.com/apk/res/android"

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

<scaleandroid:fromXScalescaleandroid: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>

zoom_exit.xml主要代码如下:

[html] view
plaincopy

<setxmlns:androidsetxmlns:android="http://schemas.android.com/apk/res/android"

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

android:zAdjustment="top">

<scaleandroid:fromXScalescaleandroid: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"/>

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

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

</set>

下面是关于Android中Animation的介绍:
表一
XML节点
功能说明
alpha
渐变透明度动画效果
<alpha

android:fromAlpha=”0.1″

android:toAlpha=”1.0″

android:duration=”3000″ />

fromAlpha
属性为动画起始时透明度
0.0表示完全透明

1.0表示完全不透明

以上值取0.0-1.0之间的float数据类型的数字

duration为动画持续时间,ms单位
toAlpha
属性为动画结束时透明度
表二
scale
渐变尺寸伸缩动画效果
<scale

android:interpolator= “@android:anim/accelerate_decelerate_interpolator”

android:fromXScale=”0.0″

android:toXScale=”1.4″

android:fromYScale=”0.0″

android:toYScale=”1.4″

android:pivotX=”50%”

android:pivotY=”50%”

android:fillAfter=”false”

android:startOffset=“700”

android:duration=”700″

android:repeatCount=”10″ />

fromXScale[float] fromYScale[float]
为动画起始时,X、Y坐标上的伸缩尺寸
0.0表示收缩到没有

1.0表示正常无伸缩

值小于1.0表示收缩

值大于1.0表示放大
toXScale [float]

toYScale[float]
为动画结束时,X、Y坐标上的伸缩尺寸
pivotX[float]

pivotY[float]
为动画相对于物件的X、Y坐标的开始位置
属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置
表三
translate
画面转换位置移动动画效果
<translate

android:fromXDelta=”30″

android:toXDelta=”-80″

android:fromYDelta=”30″

android:toYDelta=”300″

android:duration=”2000″ />

fromXDelta

toXDelta
为动画、结束起始时 X坐标上的位置
fromYDelta

toYDelta
为动画、结束起始时 Y坐标上的位置
表四
rotate
画面转移旋转动画效果
<rotate

android:interpolator=”@android:anim/accelerate_decelerate_interpolator”

android:fromDegrees=”0″

android:toDegrees=”+350″

android:pivotX=”50%”

android:pivotY=”50%”

android:duration=”3000″ />

fromDegrees
为动画起始时物件的角度
说明

当角度为负数——表示逆时针旋转

当角度为正数——表示顺时针旋转

(负数from——to正数:顺时针旋转)

(负数from——to负数:逆时针旋转)

(正数from——to正数:顺时针旋转)

(正数from——to负数:逆时针旋转)
toDegrees
属性为动画结束时物件旋转的角度 可以大于360度
pivotX

pivotY
为动画相对于物件的X、Y坐标的开始位
说明:以上两个属性值从0%-100%中取值

50%为物件的X或Y方向坐标上的中点位置
关于interpolator的解释

interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate,rotate)得以加速,减速,重复等,例如在该例中的四个xml动画文件中均使用了动画插入器。

Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation
(floatinput),该方法maps a point onthe timeline to a multiplier to be applied to thetransformations of ananimation。Android提供了几个 Interpolator
子类,实现了不同的速度曲线,如下:
AccelerateDecelerateInterpolator
在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator
在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator
动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator
在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator
在动画的以均匀的速率改变
好,本篇文章到此结束,明天继续第二篇App->Activity->Custom Dialog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: