常用界面交替动画样式以及View动态样式

来源:互联网 发布:java开发在太原多少钱 编辑:程序博客网 时间:2024/06/05 08:08
样式一般放在res文件夹的anim文件夹下,一般为了便于区分可写为“样式名字_in.xml”或 “样式名字_out.xml”。
可用于不同的Activity跳转时in与out的样式,或者View控件的动态显示(补间动画)。
样式包含:translate(移动),alpha(透明度),rotate(旋转),scale(缩放)

1. translate:移动
(实现指定样式的移动)
格式一般为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <translate
        android:fromXDelta="a"
        android:fromYDelta="b"
        android:toXDelta="c"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="d"
        android:duration="e"/>
</set>
其中a,b,c,d表示坐标位置(-100%~0~100%p,p表示屏幕)



fromXDelta或fromYDelta: 
         表示起始时界面左上角x,y的位置
toXDelta或toYDelta:           
        表示最终界面左上角x,y的位置
duration:  
        表示持续时间,单位为毫秒(ms)   
                     
如:左移移出,持续时间1秒可以表示为:

名字命名为: translate_right_left_out.xml
代码为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="-100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0"
        android:duration="1000"/> 

2.alpha
(实现不同透明度)
格式一般为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:fromAlpha="a"
        android:toAlpha="b"
        android:duration="c"
         /> 
</set>
其中a,b表示alpha的值,即透明度,0.0~1.0,0.0为透明,1.0为不透明
fromAlpha
         表示起始时指定的Activity的透明度
toAlpha:           
        表示最终指定的Activity的透明度
duration:  
        表示持续时间,单位为毫秒(ms)   
                     
如:在Activity进入时,可从透明变为不透明,可以表示为:

名字命名为: alpha_in.xml
代码为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="4000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>
3.Scale
(实现缩放效果)
一般格式为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:fromXScale="a"
        android:toXScale="b"
        android:fromYScale="c"
        android:toYScale="d"
        android:pivotX="e"
        android:pivotY="f"
        android:duration="g" /> 
</set>

其中a,b, c,d表示缩小值,0.0~1.0,0.0完全没有,1.0全部显示
e,f表示旋转中心的位置,0~100%p
g表时间(ms)
fromXScale或 fromYScale:
         表示起始时指定的Activity的缩小值
toXScale  或  toYScale :     
        表示最终指定的Activity的缩小值
pivotX:
        旋转的中心点的位置的X值
pivotY:
        旋转的中心点的位置的Y值
duration:  
        表示持续时间,单位为毫秒(ms)   
                     
如:实现进入的Activity从上往下慢慢显示,持续时间为4秒
名字命名为: scale_in.xml
代码为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="4000"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:pivotX="0%p"
        android:pivotY="0%p"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>
<!-- 由0.0放大到1.0,即由0大小放大到原始大小,经历时间为4000ms, 中心为正中由pivotX与pivotY的值决定 -->



4.rotate
<实现旋转>
一般格式为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
       
        android:fromDegrees="a"
        android:toDegrees="b" 
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="c"
        android:pivotY="d"
        android:duration="e" />
</set>

其中a,b表示旋转角度的值,0~无穷大
c,d表示旋转中心的位置,0~100%p
e表时间(ms)
fromDegrees: 
         表示起始时指定的Activity的角度
toDegrees:           
        表示最终指定的Activity的角度
其转的大小为这两个的差值
pivotX:
        旋转的中心点的位置的X值
pivotY:
         旋转的中心点的位置的Y值
duration:  
        表示持续时间,单位为毫秒(ms)   
                     
如:在Activity出去时,可缩小并以屏幕中心点旋转两圈,持续时间为4秒,可以表示为:

名字命名为: rotate_scale.xml
代码为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="4000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toDegrees="720" /> 
    <scale
        android:duration="4000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>
<!--
<rotate>标签:旋转,本文配置表示旋转时间为4000ms,旋转的中心为正中心,由属性android:pivotX="50%"与
android:pivotY="50%"决定的。旋转0度由旋转到360度。
<scale>标签:缩放: 缩放时间为4000ms,缩放的中心为正中,由属性android:pivotX="50%p"
        android:pivotY="50%p"决定,缩放由1.0缩放到0.0,即由原始大小缩到0大小
-->

在相应的Activity文件中调用.xml文件为:
使用 void android.app.Activity.overridePendingTransition(int enterAnim, int exitAnim)方法,其中
int enterAnim为即将跳转至的Activity的效果, int exitAnim为当前的Activity退出显示的效果。
使用为:(使用透明度)
 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
在aday4_7Activity跳转与动画.java项目中为:

注意:
1.在同一个xml中同一种类型的会从上至下依次执行,不同的类型会同时执行
2.结合实际实现效果

相关项目为: aday4_7Activity跳转与动画.java
0 0
原创粉丝点击