android ui动画效果怎么做,Android 使用XML做动画UI的深入解析

效果: http://www.56.com/u82/v_otm4mdk5mtk.html

第一步: 创建anim文件夹放置动画xml文件

在res文件夹下,创建一个anim的子文件夹。

124A52154-0.jpg

第二步: 加载动画

接着在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

android:fillafter="true" >

android:duration="1000"

android:fromalpha="0.0"

android:interpolator="@android:anim/accelerate_interpolator"

android:toalpha="1.0" />

fade out : 淡出

以fade in刚好相反,值由1到0.

fade_out.xml

android:fillafter="true" >

android:duration="1000"

android:fromalpha="1.0"

android:interpolator="@android:anim/accelerate_interpolator"

android:toalpha="0.0" />

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

android:toalpha="1.0"

android:interpolator="@android:anim/accelerate_interpolator"

android:duration="600"

android:repeatmode="reverse"

android:repeatcount="infinite"/>

zoom in : 放大

zoom_in.xml

android:fillafter="true" >

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" >

zoom out: 缩小

zoom_out.xml

android:fillafter="true" >

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" >

rotate: 旋转

rotate.xml

android:todegrees="360"

android:pivotx="50%"

android:pivoty="50%"

android:duration="600"

android:repeatmode="restart"

android:repeatcount="infinite"

android:interpolator="@android:anim/cycle_interpolator"/>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值