动画的介绍,网上一搜索一大把,为什么我还是会写呢?我写这个第一个是为了复习一下这个知识点,加深一点印象,第二是为了以后用的时候如果忘记了的话,查看自己写的很容易就懂了,记起来了,毕竟自己写的自己容易看懂一点;
android动画分类
Android动画分为3种:帧动画(Frame Animation),补间动画(Tween Animation),属性动画(Property Animation);
今天我们来说说补间动画,欢迎指正,虚心接受
补间动画的分类
补间动画又可以分为4种:
透明补间动画(AlphaAnimation),移动补间动画(TranslateAnimation),缩放补间动画(ScaleAnimation),旋转补间动(RotateAnimation)。
而这4种动画可以在XML文件中静态设置,也可以在代码中动态设置,下面我们先来说说静态设置动画的属性;
首先在工程目录下的res文件夹下面新建一个名字为anim的文件夹,在anim下建一个XML文件,下面介绍里面具体的代码
透明补间动画(AlphaAnimation)
XML静态设置
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false">
<alpha
android:duration="1000"
android:fromAlpha="1"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0" />
</set>
android:fillAfter = "false"这个属性的意思是动画结束后是否保持结束的状态,false为不保持,恢复动画之前的状态,true则是保持动画之后的状态,注意一定要写在set标签里面,否则无效
android:duration = "1000" 动画持续的时间,单位是毫秒
android:fromAlpha = "1" 动画起始的透明度,1是完全不透明,0是完全透明
android:toAlpha = "0" 动画结束时的透明度
android:repeatCount = "2" 动画的重复次数,传infinite为无限次
android:repeatMode = "reverse" 动画重复的模式;reverse:动画结束后再从结束状态颠倒回去,restart:动画结束后重新开始动画
然后就可以在类中调用了,具体用法:
//要使用动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//得到动画对象,传两个参数,参数1:context上下文对象;参数2:animation
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
//开始动画
imageView.startAnimation(animation);
代码动态设置
//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//拿到动画实例对象,传入fromAlpha和toAlpha
AlphaAnimation animation = new AlphaAnimation(1, 0);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
// 设置动画结束以后得状态
animation.setFillAfter(false);
//开始动画
imageView.startAnimation(animation);
移动补间动画(TranslateAnimation)
XML静态设置
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXDelta="400"
android:toYDelta="500" />
</set>
android:duration="1000" 动画持续时间
代码动态设置
//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//参数分别是:fromXDelta,toXDelta,fromYDelta,toYDelta
TranslateAnimation animation = new TranslateAnimation(0, 400, 0, 500);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开始动画
imageView.startAnimation(animation);
缩放补间动画(ScaleAnimation)
XML静态设置
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="2"
android:toYScale="2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"/>
</set>
代码动态设置
//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//参数分别是:fromXScale,toXScale,fromYScale,toYScale,
// 相对于谁Animation.RELATIVE_TO_SELF相对于自己,
// 0.5f就是动画相对于控件的X坐标的开始位置,控制偏移量的
ScaleAnimation animation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开始动画
imageView.startAnimation(animation);
以上两种代码实现的效果是一样的,效果为:
旋转补间动(RotateAnimation)
XML静态设置
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount = "2"
android:repeatMode="reverse"
android:toDegrees="360" />
</set>
android:fromDegrees="0" 动画开始时的角度
代码动态设置
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
// 设置执行的时间
animation.setDuration(1000);
// // 设置动画重复的次数
animation.setRepeatCount(2);
// // 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
// //开始动画
imageView.startAnimation(animation);
效果如下
android:pivotY="150%"
混合使用
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toDegrees="360" />
<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toXScale="1"
android:toYScale="1" />
<translate
android:duration="1000"
android:fromXDelta="-300"
android:repeatMode="reverse"
android:toXDelta="0" />
</set>
效果为:
简单的使用
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toDegrees="720" />
<scale
android:duration="2000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toXScale="1"
android:toYScale="1" />
<translate
android:duration="2000"
android:fromXDelta="-300"
android:repeatMode="reverse"
android:toXDelta="0" />
</set>
出去的动画为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toYDelta="900"
android:toXDelta="900" />
</set>
然后在跳转的时候使用,具体代码为:
Intent intent = new Intent(MainActivity.this, AnimationActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in,R.anim.out);