Android-Animation动画学习

来源:互联网 发布:跟兄弟连学php百度云 编辑:程序博客网 时间:2024/05/11 04:01

Android动画主要分为 补间动画逐帧动画

动画的实现方式
1.代码实现
2.xml文件实现

所有演示都基于一张图片

补间动画

补间动画 之 Alpha:透明度渐变动画

(1)编写Alpha渐变动画文件

duration:持续时长,以ms为单位
fromAlpha:开始时的渐变度
toAlpha:结束时的渐变度

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha        android:duration="2000"        android:fromAlpha="0.1"        android:toAlpha="1.0"        >    </alpha></set>

(2)加载动画

 //代码创建 AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);alphaAnimation.setDuration(2000);
//加载xml文件 Animation alphaAnimation= AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);
 //调用动画image_Example.startAnimation(alphaAnimation);

效果如下:
alpha

补间动画 之 Alpha:缩放动画

(1)编写xml文件
fromXScale:在X轴上的开始点
toXScale:在X轴上的结束点
fromYScale:在Y轴上的开始点
toYScale:在Y轴上的结束点
pivotX:变换起点X坐标
pivotY:变换起点Y坐标

<ml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <scale        android:duration="3000"        android:fromXScale="0"        android:toXScale="1.0"        android:fromYScale="0"        android:toYScale="1.0"        android:pivotX="100%"        android:pivotY="100%">    </scale></set>

(2)加载动画

//代码生成动画 ScaleAnimation scaleAnimation=new ScaleAnimation(0f,1f,0f,1f); scaleAnimation.setDuration(2000);
 //调用资源文件生成动画 Animation animation1=AnimationUtils.loadAnimation(this,R.anim.scale);
//加载动画image_Example.startAnimation(scaleAnimation);

(3)效果如下
这里写图片描述

补间动画 之 Alpha:旋转动画

(1)编写xml文件
上面有的这里不再重复,只叙述新的
android:fromDegrees:起始旋转角度
android:toDegrees:一共旋转多少度
repeatMode:重复模式,从有restart和reserve两种。就是一种正着来,一种反着来。
repeatCount:重复次数

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:duration="3000"        android:fromDegrees="0"        android:toDegrees="360"        android:pivotY="50%"        android:pivotX="50%"        android:repeatMode="reverse"        android:repeatCount="3">    </rotate></set>

(2)加载动画

//代码生成动画 RotateAnimation rotateAnimation=new RotateAnimation(0,360);                rotateAnimation.setDuration(3000);                rotateAnimation.setRepeatCount(2);                rotateAnimation.setRepeatMode(Animation.REVERSE);
 //调用资源文件生成动画 Animation rotateAnimation=AnimationUtils.loadAnimation(this,R.anim.rotate);
//加载动画image_Example.startAnimation(rotateAnimation);

(3)效果如下
这里写图片描述

补间动画 之 Alpha:平移动画

(1)编写xml文件
上面有的这里不再重复,只叙述新的
android:fromXDelta:起始X轴平移坐标
android:toXDelta:起始X轴终点坐标
android:fromYDelta:起始Y轴的平移坐标
android:toYDelta:起始Y轴的终点坐标

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="0"        android:toXDelta="100"        android:fromYDelta="0"        android:toYDelta="100"        android:duration="3000">    </translate></set>

(2)加载动画

//代码生成动画  TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);               translateAnimation.setDuration(3000);
 //调用资源文件生成动画  Animation animation3=AnimationUtils.loadAnimation(this,R.anim.translate);
//加载动画image_Example.startAnimation(translateAnimation);

(3)效果如下
这里写图片描述


逐帧动画

下次在介绍