Rexsee API介绍:Animations动画学习笔记及源码

android技术开发 发布于 2013/08/07 14:24
阅读 318
收藏 2

Android上实现动画,官方的SDK提供了Animations,并且介绍了两种不同模式,分别是:

1. Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;
2. Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。

同时,Animation由四种类型组成:
XML文件:
· alpha 渐变透明度动画效果
· scale 渐变尺寸伸缩动画效果
· translate 画面转换位置移动动画效果
· rotate 画面转移旋转动画效果
Java 源码中定义了相应的类,可以使用这些类的方法来获取和操作相应的属性:
· AlphaAnimation 渐变透明度动画效果
· ScaleAnimation 渐变尺寸伸缩动画效果
· TranslateAnimation 画面转换位置移动动画效果
· RotateAnimation 画面转移旋转动画效果

具体Android的原生就不再多说了,相对复杂,有兴趣的可以直接去看googleSDK。这里分享了RexseeAPI,基于对原生的封装,可以直接使用JS实现功能调用。如:
【事件】 void onAnimationStart(String id)
【说明】 当动画开始播放时触发。

Rexsee APIAnimations源码

Java代码

1 packagerexsee.core.animation; 

3 importrexsee.core.style.StyleSheet; 

4 importrexsee.core.utilities.RexseeUtilities; 

5 importandroid.view.View; 

6 importandroid.view.animation.AccelerateDecelerateInterpolator; 

7 importandroid.view.animation.AccelerateInterpolator; 

8 importandroid.view.animation.AlphaAnimation; 

9 importandroid.view.animation.Animation; 

10 importandroid.view.animation.AnimationSet; 

11 importandroid.view.animation.AnticipateInterpolator; 

12 importandroid.view.animation.AnticipateOvershootInterpolator; 

13 importandroid.view.animation.BounceInterpolator; 

14 importandroid.view.animation.CycleInterpolator; 

15 importandroid.view.animation.DecelerateInterpolator; 

16 importandroid.view.animation.Interpolator; 

17 importandroid.view.animation.LinearInterpolator; 

18 importandroid.view.animation.OvershootInterpolator; 

19 importandroid.view.animation.RotateAnimation; 

20 importandroid.view.animation.ScaleAnimation; 

21 importandroid.view.animation.TranslateAnimation; 

22 

23 publicclassAnimationsextendsAnimation{ 

24 

25 publicAnimations(){ 

26 super(); 

27 } 

28 

29 publicstaticAnimationgetAnimation(StyleSheetstyle,AnimationListenerlistener,Viewview,ViewviewParent){ 

30 if(view==null)returnnull; 

31 if(viewParent==null){ 

32 try{ 

33 viewParent=(View)view.getParent(); 

34 }catch(Exceptione){ 

35 viewParent=view; 

36 } 

37 } 

38 AnimationSetanimation=newAnimationSet(true); 

39 String[]types=style.animation_type.split("\\+"); 

40 intlength=0; 

41 animation.setDuration(Integer.parseInt(style.animation_rotate_duration)); 

42 animation.setRepeatCount(Integer.parseInt(style.animation_rotate_repeat_count)); 

43 animation.setRepeatMode(style.animation_rotate_repeat_mode.equalsIgnoreCase("normal")?1:2); 

44 animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_start_time,0)); 

45 animation.setInterpolator(getInterPolator(style.animation_rotate_interpolator)); 

46 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

47 }catch(Exceptione){ 

48 } 

49 returnanimation; 

50 } 

51 publicstaticAnimationgetAlphaAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

52 Animationanimation=null; 

53 try{ 

54 floatfrom=Float.parseFloat(style.animation_alpha_from.replaceAll("%",""))/100; 

55 floatto=Float.parseFloat(style.animation_alpha_to.replaceAll("%",""))/100; 

56 animation=newAlphaAnimation(from,to); 

57 animation.setDuration(Integer.parseInt(style.animation_alpha_duration)); 

58 animation.setRepeatCount(Integer.parseInt(style.animation_alpha_repeat_count)); 

59 animation.setRepeatMode(style.animation_alpha_repeat_mode.equalsIgnoreCase("normal")?1:2); 

60 animation.setStartOffset(RexseeUtilities.getLong(style.animation_alpha_start_time,0)); 

61 animation.setInterpolator(getInterPolator(style.animation_alpha_interpolator)); 

62 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

63 }catch(Exceptione){ 

64 } 

65 returnanimation; 

66 } 

67 publicstaticAnimationgetScaleAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

68 Animationanimation=null; 

69 try{ 

70 floatscaleCenterX,scaleCenterY; 

71 try{ 

72 scaleCenterX=Float.parseFloat(style.animation_scale_center_x.replaceAll("%","")); 

73 scaleCenterX=scaleCenterX/100; 

74 if(scaleCenterX<0)scaleCenterX=0; 

75 if(scaleCenterX>1)scaleCenterX=1; 

76 }catch(Exceptione){ 

77 scaleCenterX=(float)0.5; 

78 } 

79 try{ 

80 scaleCenterY=Float.parseFloat(style.animation_scale_center_y.replaceAll("%","")); 

81 scaleCenterY=scaleCenterY/100; 

82 if(scaleCenterY<0)scaleCenterY=0; 

83 if(scaleCenterY>1)scaleCenterY=1; 

84 }catch(Exceptione){ 

85 scaleCenterY=(float)0.5; 

86 } 

87 floatfromX=Float.parseFloat(style.animation_scale_x_from); 

88 floattoX=Float.parseFloat(style.animation_scale_x_to); 

89 floatfromY=Float.parseFloat(style.animation_scale_y_from); 

90 floattoY=Float.parseFloat(style.animation_scale_y_to); 

91 animation=newScaleAnimation(fromX,toX,fromY,toY,Animation.RELATIVE_TO_PARENT,scaleCenterX,Animation.RELATIVE_TO_PARENT,scaleCenterY); 

92 animation.setDuration(Integer.parseInt(style.animation_scale_duration)); 

93 animation.setRepeatCount(Integer.parseInt(style.animation_scale_repeat_count)); 

94 animation.setRepeatMode(style.animation_scale_repeat_mode.equalsIgnoreCase("normal")?1:2); 

95 animation.setStartOffset(RexseeUtilities.getLong(style.animation_scale_start_time,0)); 

96 animation.setInterpolator(getInterPolator(style.animation_scale_interpolator)); 

97 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

98 }catch(Exceptione){ 

99 } 

100 returnanimation; 

101 } 

102 publicstaticAnimationgetTranslateAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

103 Animationanimation=null; 

104 try{ 

105 floatfromX=Float.parseFloat(style.animation_translate_x_from); 

106 floattoX=Float.parseFloat(style.animation_translate_x_to); 

107 floatfromY=Float.parseFloat(style.animation_translate_y_from); 

108 floattoY=Float.parseFloat(style.animation_translate_y_to); 

109 fromX=fromX/100; 

110 toX=toX/100; 

111 fromY=fromY/100; 

112 toY=toY/100; 

113 animation=newTranslateAnimation(Animation.RELATIVE_TO_PARENT,fromX,Animation.RELATIVE_TO_PARENT,toX,Animation.RELATIVE_TO_PARENT,fromY,Animation.RELATIVE_TO_PARENT,toY); 

114 animation.setDuration(Integer.parseInt(style.animation_translate_duration)); 

115 animation.setRepeatCount(Integer.parseInt(style.animation_translate_repeat_count)); 

116 animation.setRepeatMode(style.animation_translate_repeat_mode.equalsIgnoreCase("normal")?1:2); 

117 animation.setStartOffset(RexseeUtilities.getLong(style.animation_translate_start_time,0)); 

118 animation.setInterpolator(getInterPolator(style.animation_translate_interpolator)); 

119 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

120 }catch(Exceptione){ 

121 } 

122 returnanimation; 

123 } 

124 

125 publicstaticAnimationgetRotate3dyAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

126 Animationanimation=null; 

127 try{ 

128 floatrotate3DCenterX,rotate3DCenterY; 

129 try{ 

130 rotate3DCenterX=Float.parseFloat(style.animation_rotate_3dy_center_x.replaceAll("%","")); 

131 rotate3DCenterX=rotate3DCenterX/100; 

132 if(rotate3DCenterX<0)rotate3DCenterX=0; 

133 if(rotate3DCenterX>1)rotate3DCenterX=1; 

134 }catch(Exceptione){ 

135 rotate3DCenterX=(float)0.5; 

136 } 

137 rotate3DCenterX=view.getWidth()*rotate3DCenterX; 

138 try{ 

139 rotate3DCenterY=Float.parseFloat(style.animation_rotate_3dy_center_y.replaceAll("%","")); 

140 rotate3DCenterY=rotate3DCenterY/100; 

141 if(rotate3DCenterY<0)rotate3DCenterY=0; 

142 if(rotate3DCenterY>1)rotate3DCenterY=1; 

143 }catch(Exceptione){ 

144 rotate3DCenterY=(float)0.5; 

145 } 

146 rotate3DCenterY=view.getHeight()*rotate3DCenterY; 

147 floatfrom=RexseeUtilities.getFloat(style.animation_rotate_3dy_from,0f); 

148 floatto=RexseeUtilities.getFloat(style.animation_rotate_3dy_to,90f); 

149 floatrotate3DDepthZ=RexseeUtilities.getFloat(style.animation_rotate_3dy_depth_z,310.0f); 

150 booleanreverse=(style.animation_rotate_3dy_reverse.equalsIgnoreCase("true"))?true:false; 

151 animation=newRotate3dyAnimation(from,to,rotate3DCenterX,rotate3DCenterY,rotate3DDepthZ,reverse); 

152 animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dy_duration,1000)); 

153 animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dy_repeat_count,0)); 

154 animation.setRepeatMode(style.animation_rotate_3dy_repeat_mode.equalsIgnoreCase("normal")?1:2); 

155 animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dy_start_time,0)); 

156 animation.setInterpolator(getInterPolator(style.animation_rotate_3dy_interpolator)); 

157 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

158 }catch(Exceptione){ 

159 } 

160 returnanimation; 

161 } 

162 

163 publicstaticAnimationgetRotate3dxAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

164 Animationanimation=null; 

165 try{ 

166 floatrotate3DCenterX,rotate3DCenterY; 

167 try{ 

168 rotate3DCenterX=Float.parseFloat(style.animation_rotate_3dx_center_x.replaceAll("%","")); 

169 rotate3DCenterX=rotate3DCenterX/100; 

170 if(rotate3DCenterX<0)rotate3DCenterX=0; 

171 if(rotate3DCenterX>1)rotate3DCenterX=1; 

172 }catch(Exceptione){ 

173 rotate3DCenterX=(float)0.5; 

174 } 

175 rotate3DCenterX=view.getWidth()*rotate3DCenterX; 

176 try{ 

177 rotate3DCenterY=Float.parseFloat(style.animation_rotate_3dx_center_y.replaceAll("%","")); 

178 rotate3DCenterY=rotate3DCenterY/100; 

179 if(rotate3DCenterY<0)rotate3DCenterY=0; 

180 if(rotate3DCenterY>1)rotate3DCenterY=1; 

181 }catch(Exceptione){ 

182 rotate3DCenterY=(float)0.5; 

183 } 

184 rotate3DCenterY=view.getHeight()*rotate3DCenterY; 

185 floatfrom=RexseeUtilities.getFloat(style.animation_rotate_3dx_from,0f); 

186 floatto=RexseeUtilities.getFloat(style.animation_rotate_3dx_to,90f); 

187 floatrotate3DDepthZ=RexseeUtilities.getFloat(style.animation_rotate_3dx_depth_z,310.0f); 

188 booleanreverse=(style.animation_rotate_3dx_reverse.equalsIgnoreCase("true"))?true:false; 

189 animation=newRotate3dxAnimation(from,to,rotate3DCenterX,rotate3DCenterY,rotate3DDepthZ,reverse); 

190 animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dx_duration,1000)); 

191 animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dx_repeat_count,0)); 

192 animation.setRepeatMode(style.animation_rotate_3dx_repeat_mode.equalsIgnoreCase("normal")?1:2); 

193 animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dx_start_time,0)); 

194 animation.setInterpolator(getInterPolator(style.animation_rotate_3dx_interpolator)); 

195 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

196 }catch(Exceptione){ 

197 } 

198 returnanimation; 

199 } 

200 publicstaticAnimationgetSkewAnimation(StyleSheetstyle,Viewview,ViewviewParent){ 

201 Animationanimation=null; 

202 try{ 

203 floatskewCenterX,skewCenterY; 

204 try{ 

205 skewCenterX=Float.parseFloat(style.animation_skew_center_x.replaceAll("%","")); 

206 skewCenterX=skewCenterX/100; 

207 if(skewCenterX<0)skewCenterX=0; 

208 if(skewCenterX>1)skewCenterX=1; 

209 }catch(Exceptione){ 

210 skewCenterX=(float)0.5; 

211 } 

212 skewCenterX=view.getWidth()*skewCenterX; 

213 try{ 

214 skewCenterY=Float.parseFloat(style.animation_skew_center_y.replaceAll("%","")); 

215 skewCenterY=skewCenterY/100; 

216 if(skewCenterY<0)skewCenterY=0; 

217 if(skewCenterY>1)skewCenterY=1; 

218 }catch(Exceptione){ 

219 skewCenterY=(float)0.5; 

220 } 

221 skewCenterY=view.getHeight()*skewCenterY; 

222 

223 floatfromX=RexseeUtilities.getFloat(style.animation_skew_x_from,0f); 

224 floattoX=RexseeUtilities.getFloat(style.animation_skew_x_to,1f); 

225 floatfromY=RexseeUtilities.getFloat(style.animation_skew_y_from,0f); 

226 floattoY=RexseeUtilities.getFloat(style.animation_skew_y_to,1f); 

227 

228 floatskewDepthZ=RexseeUtilities.getFloat(style.animation_skew_depth_z,310.0f); 

229 booleanreverse=(style.animation_skew_reverse.equalsIgnoreCase("true"))?true:false; 

230 animation=newSkewAnimation(fromX,toX,fromY,toY,skewCenterX,skewCenterY,skewDepthZ,reverse); 

231 animation.setDuration(RexseeUtilities.getInt(style.animation_skew_duration,1000)); 

232 animation.setRepeatCount(RexseeUtilities.getInt(style.animation_skew_repeat_count,0)); 

233 animation.setRepeatMode(style.animation_skew_repeat_mode.equalsIgnoreCase("normal")?1:2); 

234 animation.setStartOffset(RexseeUtilities.getLong(style.animation_skew_start_time,0)); 

235 animation.setInterpolator(getInterPolator(style.animation_skew_interpolator)); 

236 animation.initialize(view.getWidth(),view.getHeight(),viewParent.getWidth(),viewParent.getHeight()); 

237 

238 }catch(Exceptione){ 

239 } 

240 returnanimation; 

241 } 

242 publicstaticInterpolatorgetInterPolator(Stringname){ 

243 if(name.equalsIgnoreCase("AccelerateDecelerate")){ 

244 returnnewAccelerateDecelerateInterpolator(); 

245 }elseif(name.equalsIgnoreCase("Accelerate")){ 

246 returnnewAccelerateInterpolator(10.0f); 

247 }elseif(name.equalsIgnoreCase("Decelerate")){ 

248 returnnewDecelerateInterpolator(10.0f); 

249 }elseif(name.equalsIgnoreCase("Anticipate")){ 

250 returnnewAnticipateInterpolator(1.0f); 

251 }elseif(name.equalsIgnoreCase("AnticipateOvershoot")){ 

252 returnnewAnticipateOvershootInterpolator(1.0f,1.5f); 

253 }elseif(name.equalsIgnoreCase("Overshoot")){ 

254 returnnewOvershootInterpolator(1.0f); 

255 }elseif(name.equalsIgnoreCase("Bounce")){ 

256 returnnewBounceInterpolator(); 

257 }elseif(name.equalsIgnoreCase("Cycle")){ 

258 returnnewCycleInterpolator(1); 

259 }elseif(name.equalsIgnoreCase("Linear")){ 

260 returnnewLinearInterpolator(); 

261 }else{ 

262 returnnewLinearInterpolator(); 

263 } 

264 } 

265 

266 }


加载中
返回顶部
顶部