当前位置: 移动技术网 > IT编程>移动开发>Android > Android 使用XML做动画UI的深入解析

Android 使用XML做动画UI的深入解析

2019年07月24日  | 移动技术网IT编程  | 我要评论

福州跆拳道,海南人才招聘,异界风流神帝下载


第一步: 创建anim文件夹放置动画xml文件
在res文件夹下,创建一个anim的子文件夹。

         

第二步: 加载动画
接着在activity创建一个animation类,然后使用animationutils类加载动画xml

复制代码 代码如下:

animation animfadein;

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_fadein);

txtmessage = (textview) findviewbyid(r.id.txtmessage);
btnstart = (button) findviewbyid(r.id.btnstart);

// 加载动画
animfadein = animationutils.loadanimation(getapplicationcontext(),
r.anim.fade_in);



第三步: 设置动画监听器
如果你要监听动画的事件,如开始,结束等,你需要实现animationlistener监听器,重写以下方法。
onanimationend(animation animation) - 当动画结束时调用
onanimationrepeat(animation animation) - 当动画重复时调用
onaniamtionstart(animation animation) - 当动画启动时调用
复制代码 代码如下:

@override
public void onanimationend(animation animation) {
// 在动画结束后使用

// check for fade in animation
if (animation == animfadein) {
toast.maketext(getapplicationcontext(), "animation stopped",
toast.length_short).show();
}

}

@override
public void onanimationrepeat(animation animation) {
//当动画重复时使用

}

@override
public void onanimationstart(animation animation) {
//当动画开始使用

}

最后一步: 让动画动起来啦。可以使用任何ui元素调用startanimation方法。
以下是一个textview元素调用的。
txtmessage.startanimation(animfadein);
完整代码:
复制代码 代码如下:

fadeinactivity(淡入动画)
?package com.chaowen.androidanimations;

import info.androidhive.androidanimations.r;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.view.animation.animation;
import android.view.animation.animationutils;
import android.view.animation.animation.animationlistener;
import android.widget.button;
import android.widget.textview;
import android.widget.toast;

/**
 * 
 * @author chaowen
 *
 */
public class fadeinactivity extends activity implements animationlistener {

    textview txtmessage;
    button btnstart;

    animation animfadein;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_fadein);

        txtmessage = (textview) findviewbyid(r.id.txtmessage);
        btnstart = (button) findviewbyid(r.id.btnstart);

        // 加载动画
        animfadein = animationutils.loadanimation(getapplicationcontext(),
                r.anim.fade_in);

        // 设置监听
        animfadein.setanimationlistener(this);

        // 按钮
        btnstart.setonclicklistener(new view.onclicklistener() {

            @override
            public void onclick(view v) {
                txtmessage.setvisibility(view.visible);

                // 开始动画
                txtmessage.startanimation(animfadein);
            }
        });

    }

    @override
    public void onanimationend(animation animation) {
        // 在动画结束后使用

        // check for fade in animation
        if (animation == animfadein) {
            toast.maketext(getapplicationcontext(), "animation stopped",
                    toast.length_short).show();
        }

    }

    @override
    public void onanimationrepeat(animation animation) {
        //当动画重复时使用

    }

    @override
    public void onanimationstart(animation animation) {
        //当动画开始使用

    }

}

一些重要的xml属性
重要的xml动画属性
android:duration 动画持续时间,时间以毫秒为单位
android:startoffset 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
android:interpolator 指定一个动画的插入器
android:fillafter 当设置为true ,该动画转化在动画结束后被应用
android:repeatmode 定义重复的行为
android:repeatcount 动画的重复次数
 
以下是一些基本的动画xml.
fade in:  淡入
alpha是渐变透明度效果,值由0到1
fade_in.xml 
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillafter="true" >

    <alpha
        android:duration="1000"
        android:fromalpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toalpha="1.0" />

</set>

fade out : 淡出
 以fade in刚好相反,值由1到0.
fade_out.xml
复制代码 代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillafter="true" >

    <alpha
        android:duration="1000"
        android:fromalpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toalpha="0.0" />

</set>

cross fading:  交叉的淡入和淡出
 同时使用fade in和fade out可以达到交叉的效果
复制代码 代码如下:

public class crossfadeactivity extends activity implements animationlistener {

    textview txtmessage1, txtmessage2;
    button btnstart;

     
    animation animfadein, animfadeout;

    @override
    protected void oncreate(bundle savedinstancestate) {
        // todo auto-generated method stub
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_crossfade);

        txtmessage1 = (textview) findviewbyid(r.id.txtmessage1);
        txtmessage2 = (textview) findviewbyid(r.id.txtmessage2);
        btnstart = (button) findviewbyid(r.id.btnstart);

        // load animations
        animfadein = animationutils.loadanimation(getapplicationcontext(),
                r.anim.fade_in);
        animfadeout = animationutils.loadanimation(getapplicationcontext(),
                r.anim.fade_out);

        // set animation listeners
        animfadein.setanimationlistener(this);
        animfadeout.setanimationlistener(this);

        // button click event
        btnstart.setonclicklistener(new view.onclicklistener() {

            @override
            public void onclick(view v) {

                txtmessage2.setvisibility(view.visible);

                txtmessage2.startanimation(animfadein);

                 
                txtmessage1.startanimation(animfadeout);
            }
        });

    }

    @override
    public void onanimationend(animation animation) {

 

        if (animation == animfadeout) {
            txtmessage1.setvisibility(view.gone);
        }

        if(animation == animfadein){
            txtmessage2.setvisibility(view.visible);
        }

    }

    @override
    public void onanimationrepeat(animation animation) {
        // todo auto-generated method stub

    }

    @override
    public void onanimationstart(animation animation) {
        // todo auto-generated method stub

    }

}

blink:  若隐若现,酷
blink.xml
复制代码 代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromalpha="0.0"
        android:toalpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="600"
        android:repeatmode="reverse"
        android:repeatcount="infinite"/>
</set>

zoom in : 放大
zoom_in.xml 
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillafter="true" >

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromxscale="1"
        android:fromyscale="1"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="3"
        android:toyscale="3" >
    </scale>

</set>

zoom out: 缩小
zoom_out.xml 
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillafter="true" >

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromxscale="1.0"
        android:fromyscale="1.0"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="0.5"
        android:toyscale="0.5" >
    </scale>

</set>

rotate: 旋转
rotate.xml
复制代码 代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromdegrees="0"
        android:todegrees="360"
        android:pivotx="50%"
        android:pivoty="50%"
        android:duration="600"
        android:repeatmode="restart"
        android:repeatcount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>

还有几个就不再列出,有兴趣下源码看。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网