android Animation动画的xml使用


在Android应用程序,使用动画效果,能带给用户更好的感觉,做动画可以通过XML或Android代码来实现。

Animation动画效果的实现可以通过两种方式进行实现,一种是tweened animation (渐变动画),另一种是frame by frame animation (画面转换动画)。

tweened animation渐变动画有以下两种类型:

1.alpha 渐变透明度动画效果

2.scale 渐变尺寸伸缩动画效果

frame by frame animation 画面转换动画有以下两种类型:

1.translate 画面转换位置移动动画效果

2.rotate 画面转移旋转动画效果

在这里,我使用XML来做动画。实现基本的动画,如淡入,旋转等。

步骤:1、首先在res目录中新建anim的文件夹,在anim中新建需要的动画xml资源文件。

anim/alpha.xml(渐变动画)

?
1 2 3 4 5 <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > </alpha></set>

anim/scale.xml(伸缩动画)

?
1 2 3 4 5 <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <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:duration= "700" > </scale></set>
anim/translate.xml(移动动画) ?
1 2 3 4 5 <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android:fromxdelta= "30" android:toxdelta= "-80" android:fromydelta= "30" android:toydelta= "300" android:duration= "2000" > </translate></set>

anim/rotate.xml(旋转动画)

?
1 2 3 4 5 <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <rotate android:interpolator= "@android:anim/accelerate_decelerate_interpolator" android:fromdegrees= "0" android:todegrees= "+350" android:pivotx= "50%" android:pivoty= "50%" android:duration= "3000" > </rotate></set>

2、xml资源文件创建完成之后,接下来就是调用这些资源文件。以alpha为例,先创建AlphaActivity。

在activity_alpha.xml中随便放一张背景图片

?
1 2 3 <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "@drawable/test_bg" tools:context= ".AlphaActivity" > </relativelayout>
AlphaActivity.java
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.example.animation_demo; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.Toast; public class AlphaActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); View view=View.inflate( this , R.layout.activity_alpha, null ); setContentView(view); //使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件 Animation animation=AnimationUtils.loadAnimation( this , R.anim.alpha); view.startAnimation(animation); animation.setAnimationListener( new AnimationListener() { @Override public void onAnimationStart(Animation arg0) {} //在动画开始时使用 @Override public void onAnimationRepeat(Animation arg0) {} //在动画重复时使用 @Override public void onAnimationEnd(Animation arg0) { Toast.makeText(AlphaActivity. this , "在动画结束时使用" , Toast.LENGTH_SHORT).show(); } }); } }

这个是整个layout文件的动画,也可以让任何UI元素调用starAnimation方法。

例如:textMsg.startAnimation(animation); 这时候就不需要下面代码 动画填充指定xml了

?
1 2 View view=View.inflate( this , R.layout.activity_alpha, null ); setContentView(view);
直接setContentView(R.layout.activity_alpha); 就OK了。卒。

今天刚用到,迟点贴出图片。。


附上找到的一些基本的xml效果:

Fade In:淡入

alpha是渐变透明度效果,值由0到1

?
1 2 3 4 5 6 fade_in.xml <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" android:fillafter= "true" > </alpha></set>
Fade Out :淡出
以Fade In刚好相反,值由1到0
?
1 2 3 4 5 6 fade_out.xml <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" android:fillafter= "true" > </alpha></set>
Cross Fading: 交叉的淡入和淡出
同时使用Fade in和Fade out可以达到交叉的效果
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public class CrossfadeActivity extends Activity implements AnimationListener { TextView txtMessage1, txtMessage2; Button btnStart; Animation animFadeIn, animFadeOut; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_crossfade); txtMessage1 = (TextView) findViewById(R.id.txtMessage1); txtMessage2 = (TextView) findViewById(R.id.txtMessage2); btnStart = (Button) findViewById(R.id.btnStart); // load animations animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out); // set animation listeners animFadeIn.setAnimationListener( this ); animFadeOut.setAnimationListener( this ); // button click event btnStart.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { txtMessage2.setVisibility(View.VISIBLE); txtMessage2.startAnimation(animFadeIn); txtMessage1.startAnimation(animFadeOut); } }); } @Override public void onAnimationEnd(Animation animation) { if (animation == animFadeOut) { txtMessage1.setVisibility(View.GONE); } if (animation == animFadeIn){ txtMessage2.setVisibility(View.VISIBLE); } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } }

BLink:若隐若现

?
1 2 3 4 5 blink.xml <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > </alpha></set>
Zoom In:放大 ?
1 2 3 4 5 6 zoom_in.xml <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" android:fillafter= "true" > <scale xmlns:android= "http://schemas.android.com/apk/res/android" android:duration= "1000" android:fromxscale= "1" android:fromyscale= "1" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "3" android:toyscale= "3" > </scale> </set>
Zoom Out:缩小 ?
1 2 3 4 5 <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" android:fillafter= "true" > <scale xmlns:android= "http://schemas.android.com/apk/res/android" android:duration= "1000" android:fromxscale= "1.0" android:fromyscale= "1.0" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "0.5" android:toyscale= "0.5" > </scale> </set>
Rotate:旋转 ?
1 2 3 4 5 rotate.xml <!--?xml version= "1.0" encoding= "utf-8" ?--> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <rotate android:fromdegrees= "0" android:todegrees= "360" android:pivotx= "50%" android:pivoty= "50%" android:duration= "600" android:repeatmode= "restart" android:repeatcount= "infinite" android:interpolator= "@android:anim/cycle_interpolator" > </rotate></set>
好长啊。。。既然你们都看到这里了,再多说几句。。

根节点的属性:

名称

属性

备注

android:shareInterpolator

是否共享插入器

共享时,四个子节点都用一个插入器

android:interpolator

指定一个动画的插入器

使用系统资源

android:fillEnabled

当设置为true时,fillAfter和fill, Befroe将会都为true,此时会忽略fillBefore 和fillAfter两种属性

android:fillAfter

该动画转化是否在动画结束后被应用

boolean

android:fillBefore

该动画转化是否在动画开始前被应用

boolean

android:repeatMode

重复模式

"restart" 或者 "reverse"

android:repeatCount

重复次数

integer

android:duration

动画持续时间

integer

android:startOffset

动画时间间隔

long

android:zAdjustment

定义动画z order的变换

[normal] or [top] or [bottom]

android:detachWallpaper

boolean


更多相关文章

  1. [图文]为移植到Android平台上的Cocos2d-x项目添加xml布局文件
  2. Android三种动画的实现
  3. 今天开始写android的照片浏览器(一)至返回所有图片文件
  4. Android工程内嵌资源文件的两种方法
  5. 理解---Android 向右滑动销毁(finish)Activity, 随着手势的滑动而
  6. Android 动画之Tween动画详细讲解及java源码实现
  7. 删除androidAndroid递归方式删除某文件夹下的所有文件
  8. IntelliJ IDEA 如何导出安卓(Android)apk文件 详细教程
  9. Android属性动画—实现第三方登录的上拉展开,下拉隐藏

随机推荐

  1. Android开发如何调试Service
  2. Recyclerview 点击效果,可以使用
  3. MediaRecorder创建Surface流程学习
  4. android 文件读取(assets、raw)
  5. Android(安卓)上使用FFmpeg一些错误记录
  6. Android(安卓)android.intent.category解
  7. Android(安卓)WiFi直连 双向通信
  8. 功能强大的Vitamio视频播放器的使用教程
  9. 如何创建一个android的react-native组件(
  10. android wap push实现