您的位置:首页 > 移动开发 > Android开发

Android的View动画使用总结

2017-03-16 11:52 232 查看

View动画的四种变换

名称标签子类效果
平移动画<translate>TranslateAnimation移动View
缩放动画<scale>ScaleAnimatioin放大或缩小View
旋转动画<Rotate>RotateAnimation旋转View
透明度动画<alpha>AlphaAnimation改变View的透明度
例子(文件保存在res/anim文件夹下):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000" //持续时间 set的属性会覆盖下面子动画的属性
android:interpolator="@anim/interpolator_resource" //动画集合采用的插值器 默认为加速减速插值器
android:shareInterpolator="true"  //表示集合动画是否和集合共享同一个插值器。如果集合不指定插值器,子动画就需要单独指定所需的插值器或者默认值
android:startOffset="1000"  //动画开始前延时 可以在子动画中加入,实现顺序执行 默认为一起执行
android:fillAfter="false" //动画结束后View是否留在结束后的状态 默认为false
android:fillBefore="false"> //动画开始前View是否留在开始时的状态 默认为false

<alpha
android:duration="1000"
android:fromAlpha="0" //起始透明度 0 ~ 1
android:toAlpha="0.8"/>

<rotate
android:duration="2000"
android:fromDegrees="60"  //起始旋转的角度
android:pivotX="50%" //起始旋转中点 默认是坐上角
android:pivotY="50%"
android:toDegrees="360"/>

<scale
android:duration="1500"
android:fromXScale="100%"  //x方向的缩放起始值
android:fromYScale="100%"
android:pivotX="50%" //缩放位置的x坐标 默认是View左上角
android:pivotY="50%"
android:toXScale="0%"  //x方向的缩放结束值
android:toYScale="0%"/>

<translate
android:duration="3000"
android:fromXDelta="10" //x的起始位置(相对于原View)单位像素(也可使用50%这样的百分比 相对于原View大小)
android:fromYDelta="10"
android:toXDelta="1"  //x的结束位置
android:toYDelta="1"/>
</set>


系统提供的插值器

java类xmlid值描述
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator动画始末速率较慢,中间加速
AccelerateInterpolator@android:anim/accelerate_interpolator动画开始速率较慢,之后慢慢加速
AnticipateInterpolator@android:anim/anticipate_interpolator开始的时候从后向前甩
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator类似上面AnticipateInterpolator
BounceInterpolator@android:anim/bounce_interpolator动画结束时弹起
CycleInterpolator@android:anim/cycle_interpolator循环播放速率改变为正弦曲线
DecelerateInterpolator@android:anim/decelerate_interpolator动画开始快然后慢
LinearInterpolator@android:anim/linear_interpolator动画匀速改变
OvershootInterpolator@android:anim/overshoot_interpolator向前弹出一定值之后回到原来位置
PathInterpolator新增,定义路径坐标后按照路径坐标来跑。

使用View动画

Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_anim);
imageView.startAnimation(animation);

//设置监听
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.d("myinfo", "onAnimationStart");
}

@Override
public void onAnimationEnd(Animation animation) {
Log.d("myinfo", "onAnimationEnd");
}

@Override
public void onAnimationRepeat(Animation animation) {
Log.d("myinfo", "onAnimationRepeat");//动画重复
}
});


注意:

set本身也是继承自Animation类,他的属性会覆盖子动画的相关属性。

repeatMode属性必须和repeatCount属性结合使用,并且不能在set中设置,否则无效

set的AnimationListener不能监听到动画重复事件,并且如果子动画没有结束,同样监听不到动画结束

android:repeatCount={infinite(无限)|100(具体的重复次数)}
android:repeatMode={reverse(颠倒回放)|restart(重新回放)}


自定义View动画

主要通过继承Animatioin抽象类 并应用矩阵变换操作

View动画的应用

1. LayoutAnimation 作用于 ViewGroup,当他的子元素出场时都会具有这种动画效果。

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/rcv_item_anim"
android:delay="0.1" //延时执行多少周期,比如动画时长为10s,这的0.1代表,下一个View的出场动画在上一个View的出场动画执行后1s(0.1*10)再执行。默认是一起执行
android:animationOrder="normal" //子View动画执行顺序,默认为顺序执行
/>

在ViewGroup的xml中加上
android:layoutAnimation="@anim/layout_anim"

或者:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rcv_item_anim);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
recyclerView.setLayoutAnimation(controller);


2. Activity的切换效果

overridePendingTransition(R.anim.act_in_anim, R.anim.act_out_anim);
第一个参数是Activity入场的动画,第二个参数是Activity出场等待动画。
**注意:**这个方法必须在startActivity() 或 finish()这两个方法的后面调用,否则无效。


帧动画

帧动画容易引起OOM 避免使用大尺寸图片

例子(文件保存在res/drawable文件夹下):

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">  //动画是否只执行1次,为false的话可以一直重复执行

<item android:drawable="@drawable/ic_launcher" android:duration="1000"/>
<item android:drawable="@drawable/ic_launcher_round" android:duration="1000"/>

</animation-list>


linearLayout.setBackgroundResource(R.drawable.drawable_anim);
AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground();
animationDrawable.start();


参考文章:

- 《Android开发艺术探究》

- http://blog.csdn.net/yanbober/article/details/46481171
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 动画