-
长整型值:
-
duration 属性为动画持续时间
-
说明: 时间以毫秒为单位
-
布尔型值:
-
fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-
–>
3.translate
- <?xml version="1.0" encoding="utf-8"?>
-
<translate
-
android:fromXDelta=“30”
-
android:toXDelta="-80"
-
android:fromYDelta=“30”
-
android:toYDelta=“300”
-
android:duration=“2000”
-
/>
4.rotate
- <?xml version="1.0" encoding="utf-8"?>
-
<rotate
-
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
-
android:fromDegrees=“0”
-
android:toDegrees="+350"
-
android:pivotX=“50%”
-
android:pivotY=“50%”
-
android:duration=“3000” />
三、如何使用XML中定义的动画
public static Animation loadAnimation (Contextcontext, int id)
//第一个参数Context为程序的上下文
//第二个参数id为动画XML文件的引用
//例子:
myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
四、如何在java代码中定义动画
1.代码:
-
//在代码中定义 动画实例对象
-
private Animation myAnimation_Alpha;
-
private Animation myAnimation_Scale;
-
private Animation myAnimation_Translate;
-
private Animation myAnimation_Rotate;
-
//根据各自的构造方法来初始化一个实例对象
-
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
-
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
-
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
-
myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
-
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
-
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
2. 分析:
-
1.1 AlphaAnimation
-
第一步:AlphaAnimation类对象定义
-
private AlphaAnimation myAnimation_Alpha;
-
第二步:AlphaAnimation类对象构造
-
AlphaAnimation(float fromAlpha, float toAlpha)
-
//第一个参数fromAlpha为 动画开始时候透明度
-
//第二个参数toAlpha为 动画结束时候透明度
-
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
-
//说明:
-
// 0.0表示完全透明
-
// 1.0表示完全不透明
-
第三步:设置动画持续时间
-
myAnimation_Alpha.setDuration(5000);
-
//设置时间持续时间为 5000毫秒
-
1.2 ScaleAnimation
-
第一步:ScaleAnimation类对象定义
-
private AlphaAnimation myAnimation_Scale;
-
第二步:ScaleAnimation类对象构造
-
ScaleAnimation(float fromX, float toX, float fromY, float toY,
-
int pivotXType, float pivotXValue, int pivotYT
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
ype, float pivotYValue)
-
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
-
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
-
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
-
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
-
/*说明:
-
以上四种属性值
-
0.0表示收缩到没有
-
1.0表示正常无伸缩
-
值小于1.0表示收缩
-
值大于1.0表示放大
-
*/
-
//第五个参数pivotXType为动画在X轴相对于物件位置类型
-
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
-
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
-
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
-
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
-
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
-
第三步:设置动画持续时间
-
myAnimation_Scale.setDuration(700);
-
//设置时间持续时间为 700毫秒
-
1.3 TranslateAnimation
-
第一步:TranslateAnimation类对象定义
-
private AlphaAnimation myAnimation_Translate;
-
第二步:TranslateAnimation类对象构造
-
TranslateAnimation(float fromXDelta, float toXDelta,
-
float fromYDelta, float toYDelta)
-
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
-
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
-
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
-
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
-
第三步:设置动画持续时间 m
-
myAnimation_Translate.setDuration(2000);
-
1.4 RotateAnimation
-
第一步:RotateAnimation类对象定义
-
private AlphaAnimation myAnimation_Rotate;
-
第二步:RotateAnimation类对象构造
-
RotateAnimation(float fromDegrees, float toDegrees,
-
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
-
//第一个参数fromDegrees为动画起始时的旋转角度
-
//第二个参数toDegrees为动画旋转到的角度
-
//第三个参数pivotXType为动画在X轴相对于物件位置类型
-
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
-
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
-
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
-
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
-
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
-
第三步:设置动画持续时间
-
myAnimation_Rotate.setDuration(3000);
五、如何使用java代码中的动画效果
-
使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果
-
public void startAnimation (Animation animation)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个根元素和若干个子元素。
实现一个人跳舞的Frame动画,6张图片如下所示:
1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。
2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:
<? xml version="1.0" encoding="utf-8" ?><
animation-list
xmlns:apk
=“http://schemas.android.com/apk/res/android”
apk:oneshot
=“false”
>
<
item
apk:drawable
="@drawable/p01"
apk:duration
=“500”
/>
<
item
apk:drawable
="@drawable/p02"
apk:duration
=“500”
/>
[外链图片转存中…(img-Euoepzmy-1641182541187)][外链图片转存中…(img-lsTAvbfh-1641182541188)][外链图片转存中…(img-jZ3YfE3e-1641182541189)][外链图片转存中…(img-P5AprzhG-1641182541189)][外链图片转存中…(img-5I8RAu7H-1641182541190)][外链图片转存中…(img-2eHocG7U-1641182541190)]
1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。
2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:
<? xml version="1.0" encoding="utf-8" ?><
animation-list
xmlns:apk
=“http://schemas.android.com/apk/res/android”
apk:oneshot
=“false”
>
<
item
apk:drawable
="@drawable/p01"
apk:duration
=“500”
/>
<
item
apk:drawable
="@drawable/p02"
apk:duration
=“500”
/>