Animation的理解
今天学习了如何设置动画,给大家分享一下。
1、动画的分类:View动画 Drawable动画
2、这里我只分享View动画的做法:
有两种设置:编码设置 即在Activity里面进行设置动画。
Xml文件中设置。
3、制作动画的基本过程:1)编码设置
1、创建动画对象
2、进行设置
3、启动动画
2)Xml设置
1、定义动画文件 (注意生成的Xml文件不在layout文件夹中,要创建一个anim文件夹,动画的Xml文件都要存放在此文件夹)
2、加载动画文件得到动画对象
3、启动动画
3)复合动画的设置
1、创建透明动画并设置
2、创建旋转动画并设置
3、创建复合动画
4、添加两个动画
5、启动复合动画对象
4、动画的类型:缩放动画、旋转动画、透明度动画、移动动画、复合动画
下面是我做的动画:
缩放动画:
//创建动画对象 设置 启动动画public void codeScale(View v){
tv_va.setText(
"Code缩放动画:宽度从0.5到1.5,高度从0.0到1.0,圆心为顶部中点,延迟1s开始,持续2s,最终还原。"); ScaleAnimation scaleAnimation =
new ScaleAnimation(
0.5f,
1.5f,
0,
1, Animation.
ABSOLUTE,
0.5f,Animation.
ABSOLUTE,
0);
//scaleAnimation = new ScaleAnimation(0.5f,1.5f,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0); scaleAnimation.setStartOffset(
1000); scaleAnimation.setDuration(
2000); scaleAnimation.setFillBefore(
true);
imageView.startAnimation(scaleAnimation);}
// 定义动画文件 加载动画文件得到动画对象 启动动画public void xmlScale(View v){
tv_va.setText(
"Xml缩放动画:宽度从0到1.5,高度从0.0到1.0,圆心为右下角,延迟1s开始,持续3s,最终固定。"); Animation animation= AnimationUtils.
loadAnimation(
this,R.anim.
scale_test1);
imageView.startAnimation(animation);}
<?xml version="1.0" encoding="utf-8"?><
scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:fromYScale="0" android:toXScale="1.5" android:toYScale="1" android:startOffset="1000" android:duration="3000" android:pivotX="100%" android:pivotY="100%" android:fillAfter="true"></
scale>
旋转动画:
public void codeRotate(View v){
tv_va.setText(
"code旋转动画:以图片中心为中心,从负90到正90,持续5s。"); RotateAnimation rotateAnimation=
new RotateAnimation(-
90,
90,Animation.
RELATIVE_TO_SELF,
0.5f,Animation.
RELATIVE_TO_SELF,
0.5f); rotateAnimation.setDuration(
5000);
imageView.startAnimation(rotateAnimation);}
public void xmlRotate(View v){
tv_va.setText(
"Xml旋转动画:以左顶点为中心,从正90到负90,持续5s。"); Animation animation= AnimationUtils.
loadAnimation(
this,R.anim.
scale_test);
imageView.startAnimation(animation);}
<?xml version="1.0" encoding="utf-8"?><
rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="90" android:toDegrees="-90" android:duration="5000"></
rotate>
透明度动画:
public void codeAlpha(View v){
tv_va.setText(
"Code透明度动画:从完全透明到完全不透明,持续5s。"); AlphaAnimation alphaAnimation=
new AlphaAnimation(
0,
1); alphaAnimation.setDuration(
5000);
imageView.startAnimation(alphaAnimation);}
public void xmlAlpha(View v){
tv_va.setText(
"Xml透明度动画:从完全透明到完全不透明,持续4s。"); Animation animation = AnimationUtils.
loadAnimation(
this,R.anim.
alpha_test);
imageView.startAnimation(animation);}
<?xml version="1.0" encoding="utf-8"?><
alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration="4000"></
alpha>
移动动画:
public void codeTranslation(View v){
tv_va.setText(
"Code移动动画:向右移动一个自己的宽度,向下移动一个自己的高度,持续2秒。"); TranslateAnimation translateAnimation =
new TranslateAnimation(Animation.
ABSOLUTE,
0,Animation.
RELATIVE_TO_SELF,
1,Animation.
ABSOLUTE,
0,Animation.
RELATIVE_TO_SELF,
1); translateAnimation.setDuration(
2000);
imageView.startAnimation(translateAnimation);}
public void xmlTranslation(View v){
tv_va.setText(
"Xml移动动画:从屏幕的右边逐渐回到原来的位置,持续2秒。"); Animation animation = AnimationUtils.
loadAnimation(
this,R.anim.
translation_test);
imageView.startAnimation(animation);}
<?xml version="1.0" encoding="utf-8"?><
translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="100%p" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="0" android:duration="2000"></
translate>
public void codeAnimationset(View v){
tv_va.setText(
"Code复合动画:透明度从透明到不透明," +
"持续2s,接着进行旋转360度的动画,持续1s");
//1 创建透明动画并设置 2创建旋转动画并设置 3创建复合动画 4添加两个动画 5启动复合动画对象 AlphaAnimation alphaAnimation=
new AlphaAnimation(
0,
1); alphaAnimation.setDuration(
2000); RotateAnimation rotateAnimation=
new RotateAnimation(
0,
360,Animation.
RELATIVE_TO_SELF,
0.5f,Animation.
RELATIVE_TO_SELF,
0.5f); rotateAnimation.setDuration(
1000); rotateAnimation.setStartOffset(
2000); AnimationSet animationSet =
new AnimationSet(
true); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);}
以上是我分享的内容。