当前位置: 代码网 > it编程>App开发>Android > Android之 动画总结

Android之 动画总结

2024年08月01日 Android 我要评论
动画总结

一 动画种类

1.1 动画在android中运用也非常广泛,如点击按钮,加载框,activity的转场等都有动画的身影

1.2 常用的动画有以下以下几种

逐帧动画【frame animation】,即顺序播放事先准备的图片

补间动画【tween animation】,view的动画效果可以实现简单的平移、缩放、旋转。

属性动画【property animation】,补间动画增强版,支持对对象执行动画。

过渡动画【transition animation】,实现activity或view过渡动画效果。包括5.0之后的md过渡动画等。

二 逐帧动画 

2.1 定义一个动画xml,设置图片集合

<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00000" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00001" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00002" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00003" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00004" />
</animation-list>

2.2 java设置动画

//定义组件
imageview ivimage = findviewbyid(r.id.iv_refresh_header);
//开始动画
ivimage.setimageresource(r.drawable.anim_loading);
manimationdrawable = (animationdrawable) ivimage.getdrawable();
manimationdrawable.start();

//停止动画
ivimage.clearanimation();
if (manimationdrawable != null){
	manimationdrawable.stop();
}

三 补间动画

2.1 补间动画种类

透明度变化,大小缩放变化,位移变化,旋转变化

2.2 透明度动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <alpha 
        android:duration="1000" 
        android:fromalpha="0.0" 
        android:toalpha="1.0" /> 
</set>

java定义

alphaanimation alpha = new alphaanimation(0, 1); 
alpha.setduration(500);          //设置持续时间 
alpha.setfillafter(true);                   //动画结束后保留结束状态 
alpha.setinterpolator(new accelerateinterpolator());        //添加差值器 
ivimage.setanimation(alpha);

2.3 大小缩放动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <scale 
        android:duration="1000" 
        android:fillafter="false" 
        android:fromxscale="0.0" 
        android:fromyscale="0.0" 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotx="50%" 
        android:pivoty="50%" 
        android:toxscale="1.4" 
        android:toyscale="1.4" /> 
</set>

java定义

scaleanimation scale = new scaleanimation(1.0f, scalexy, 1.0f, scalexy, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); 
scale.setduration(durationmillis); 
scale.setfillafter(true); 
ivimage.setanimation(scale);

2.4 位移动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="2000" 
        android:fromxdelta="30" 
        android:fromydelta="30" 
        android:toxdelta="-80" 
        android:toydelta="300" /> 
</set>

java定义

translateanimation translate = new translateanimation(fromxdelta, toxdelta, fromydelta, toydelta); 
translate.setduration(durationmillis); 
translate.setfillafter(true); 
ivimage.setanimation(translate);

2.5 旋转动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <rotate 
        android:duration="3000" 
        android:fromdegrees="0" 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotx="50%" 
        android:pivoty="50%" 
        android:todegrees="+350" /> 
</set>

java定义

rotateanimation rotate = new rotateanimation(fromdegrees, todegrees, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); 
rotate.setduration(durationmillis); 
rotate.setfillafter(true); 
ivimage.setanimation(rotate);

2.6 组合动画animationset

relativelayout rlroot = (relativelayout) findviewbyid(r.id.rl_root);

//旋转动画
rotateanimation animrotate = new rotateanimation(0, 360,
			animation.relative_to_self, 0.5f, animation.relative_to_self,
			0.5f);
animrotate.setduration(1000);// 动画时间
animrotate.setfillafter(true);// 保持动画结束状态

//缩放动画
scaleanimation animscale = new scaleanimation(0, 1, 0, 1,
			animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f);
animscale.setduration(1000);
animscale.setfillafter(true);// 保持动画结束状态

//渐变动画
alphaanimation animalpha = new alphaanimation(0, 1);
animalpha.setduration(2000);// 动画时间
animalpha.setfillafter(true);// 保持动画结束状态

//动画集合
animationset set = new animationset(true);
set.addanimation(animrotate);
set.addanimation(animscale);
set.addanimation(animalpha);

//启动动画
rlroot.startanimation(set);

三 属性动画

3.1 xml+java调用方式

定义animator.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <animator 
        android:valuefrom="0" 
        android:valueto="100" 
        android:valuetype="inttype" 
        android:duration="3000" 
        android:startoffset ="1000" 
        android:fillbefore = "true" 
        android:fillafter = "false" 
        android:fillenabled= "true" 
        android:repeatmode= "restart" 
        android:repeatcount = "0" 
        android:interpolator="@android:anim/accelerate_interpolator"/> 
</set>

    java调用

button button = (button) findviewbyid(r.id.button); 
animator manim = animatorinflater.loadanimator(this, r.animator.animator); 
manim.settarget(button); 
manim.start();

3.2 纯java方式

objectanimator manimator = objectanimator.offloat(view, type, start, end); 

// 设置动画重复播放次数 = 重放次数+1 
// 动画播放次数 = infinite时,动画无限重复 
manimator.setrepeatcount(valueanimator.infinite); 
// 设置动画运行的时长 
manimator.setduration(time); 
// 设置动画延迟播放时间 
manimator.setstartdelay(0); 
// 设置重复播放动画模式 
manimator.setrepeatmode(valueanimator.restart); 
// valueanimator.restart(默认):正序重放 
// valueanimator.reverse:倒序回放 
//设置差值器 
manimator.setinterpolator(new linearinterpolator()); 
return manimator; 

3.3 valueanimator值动画,主要负责值的计算和过度,以及动画的播放次数、播放模式和动画监听等

valueanimator animator = valueanimator.offloat(1, 0.5f, 1);
animator.setduration(3000);
animator.addupdatelistener(new valueanimator.animatorupdatelistener() {
    @override
    public void onanimationupdate(valueanimator animation) {
        float value = (float) animation.getanimatedvalue();
        imageview.setscalex(value);
    }
});
animator.start();

3.4 objectanimator对象动画,继承valueanimator,可以直接修改对象的属性

objectanimator animator = objectanimator.offloat(imageview, "scalex", 1, 0.5f, 1);
animator.setduration(3000);
animator.start();

3.5 propertyvaluesholder,用来保存属性的值

propertyvaluesholder xholder = propertyvaluesholder.offloat("scalex", 1.0f, 0.5f, 1.0f);
propertyvaluesholder yholder = propertyvaluesholder.offloat("scaley", 1.0f, 0.5f, 1.0f);

objectanimator.ofpropertyvaluesholder(imageview, xholder, yholder)
        .setduration(3000)
        .start();

3.6 animatorset,实现多个动画效果

objectanimator xanimator = objectanimator.offloat(imageview, "scalex", 1.0f, 0.5f, 1.0f);
objectanimator yanimator = objectanimator.offloat(imageview, "scaley", 1.0f, 0.5f, 1.0f);

animatorset animator = new animatorset();
animator.playtogether(xanimator, yanimator);
animator.setduration(3000);
animator.start();

animatorset还可以指定动画的顺序,调用playsequentially()方法依次播放动画 


objectanimator animator1 = objectanimator.offloat(imageview, "alpha", 1.0f, 0.5f, 1.0f);
animator1.setduration(2000);

objectanimator xanimator2 = objectanimator.offloat(imageview, "scalex", 1.0f, 0.5f, 1.0f);
objectanimator yanimator2 = objectanimator.offloat(imageview, "scaley", 1.0f, 0.5f, 1.0f);
animatorset scaleanimator = new animatorset();
scaleanimator.playtogether(xanimator2, yanimator2);
scaleanimator.setduration(2000);

objectanimator animator3 = objectanimator.offloat(imageview, "rotation", 0f, 360f);
animator3.setduration(2000);

animatorset animator = new animatorset();
animator.playsequentially(animator1, scaleanimator, animator3);

animator.start();

3.7 evaluator估值器,告诉动画系统如何从初始值过度到结束值

四 过渡动画,即activity转场动画 

4.1 定义动画xml

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

    <translate
        android:duration="270"
        android:fromxdelta="100%p"
        android:toxdelta="0%p" />

</set>

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

    <translate
        android:duration="270"
        android:fromxdelta="0%p"
        android:toxdelta="-100%p" />

</set>

4.2 设置style

<!--左右进出场的activity动画-->
<style name="my_animationactivity" mce_bogus="1" parent="@android:style/animation.activity">
	<item name="android:activityopenenteranimation">@anim/open_enter</item>
	<item name="android:activitycloseexitanimation">@anim/close_exit</item>
</style>

<!--上下进出场的activity动画-->
<style name="up_down_activity_anim" mce_bogus="1" parent="@android:style/animation.activity">
	<item name="android:activityopenenteranimation">@anim/open_up</item>
	<item name="android:activitycloseexitanimation">@anim/close_down</item>
</style>

4.3 java调用

startactivity(intent);
overridependingtransition(r.anim.bottom_top_anim, r.anim.alpha_hide);

finish();
overridependingtransition(r.anim.alpha_show, r.anim.top_bottom_anim);

4.4 android5.0之后,android就自带几种动画特效。 3种转场动画 ,1种共享元素

   三种转场动画

@requiresapi(api = build.version_codes.lollipop)
public void explode(view view) {
	intent = new intent(this, transitionactivity.class);

	intent.putextra("flag", 0);

	startactivity(intent, activityoptions.makescenetransitionanimation(this).tobundle());

}

@requiresapi(api = build.version_codes.lollipop)
public void slide(view view) {
	intent = new intent(this, transitionactivity.class);

	intent.putextra("flag", 1);

	startactivity(intent, activityoptions.makescenetransitionanimation(this).tobundle());

}

@requiresapi(api = build.version_codes.lollipop)
public void fade(view view) {
	intent = new intent(this, transitionactivity.class);

	intent.putextra("flag", 2);

	startactivity(intent, activityoptions.makescenetransitionanimation(this).tobundle());

}

共享动画

@requiresapi(api = build.version_codes.lollipop)
public void share(view view) {
	view fab = findviewbyid(r.id.fab_button);
	intent = new intent(this, transitionactivity.class);

	intent.putextra("flag", 3);

	//创建单个共享
    //startactivity(intent, activityoptions.makescenetransitionanimation(this, view, "share")
    //            .tobundle());

	//创建多个共享
	startactivity(intent, activityoptions.makescenetransitionanimation(this, pair.create
			(view, "share"),
			pair.create(fab,"fab"))
			.tobundle());

}
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com