当前位置:编程学习 > XML/UML >>

Android动画之XML(二)

Animation也可以放在XML文件中,这样程序的可维护性提高了。在XML中写动画的步骤如下

1.在res文件夹下面新建一个名为anim的文件夹

2.创建xml文件,并首先加入set标签,改标签如下

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
 
</set>


3.在该标签当中加入rotate,alpha,scale或者translate标签

4.在代码当中使用AnimationUtils加载xml文件,并生成Animation对象

Alpha动画


[java]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <alpha 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" 
        android:startOffset="500" 
        android:duration="2000" 
            /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="2000"
            />

</set>Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(a);

Scale动画


[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <scale 
        android:fromXScale="1.0" 
        android:toXScale="0.0" 
        android:fromYScale="1.0" 
        android:toYScale="0.0" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="2000" 
    /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
    />

</set>
Rotate动画


[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <rotate 
        android:fromDegrees="0" 
        android:toDegrees="400" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="3000" 
    /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="400"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"
    />

</set>
Translate动画


[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <translate 
        android:fromXDelta="50%" 
        android:toXDelta="100%" 
        android:fromYDelta="50%" 
        android:toYDelta="100%" 
        android:duration="3000" 
    /> 
 
</set></SPAN> 

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

</set>
这里重点提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta

android:pivotX="50"使用绝对坐标

android:pivotX="50%"相对自己

android:pivotX="50%p"相对父控件

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,