基础动画全纪录

1. View/(Tween)补间动画 Animation

View动画时通过对View进行平移、旋转、缩放和显隐变换,从而产生动画效果,是一种渐进式的动画
View动画种类

1.1 平移 TranslateAnimation
Java代码
//起点X,Y 增加的X,Y
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
animationView.startAnimation(ta);

XML代码 xml动画文件路径:res/anim/xxxanimation.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0%p"
    android:fromXDelta="0%p"
    android:toYDelta="90%p"
    android:toXDelta="90%p"
    android:duration="1000" />

Animation ta1 = AnimationUtils.loadAnimation(this, R.anim.translate);
animationView.startAnimation(ta1);

更复杂实现

//fromXType,fromXValue, toXType,toXValue,fromYType,fromYValue, toYType,toYValue
//起点X方向参照值,起点X,终点X方向参照值,终点X,起点Y方向参照值,起点Y,终点Y方向参照值,终点Y
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,
Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
参照值描述
Animation.ABSOLUTE具体的坐标值,指绝对的屏幕像素单位
Animation.RELATIVE_TO_SELF相对自己的坐标值,0.1f是指自己的坐标值乘以0.1
Animation.RELATIVE_TO_PARENT相对父容器的坐标值,0.1f是指父容器的坐标值乘以0.1

简单实现实际上是参照值全为Animation.ABSOLUTE的情况

1.2缩放 ScaleAnimation
Java代码
//起始X倍数,最终X倍数,起始Y倍数,最终X倍数
ScaleAnimation sa = new ScaleAnimation(4, 1, 4, 1);
sa.setDuration(2000);
baseAnimationView.startAnimation(sa);

XML代码
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0"/>

Animation sa1 = AnimationUtils.loadAnimation(this, R.anim.scale);
                animationView.startAnimation(sa1);

更复杂实现

//起始X倍数,最终X倍数,起始Y倍数,最终X倍数,缩放模式,0.5f自身中心,缩放模式,0.5f自身中心
//ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
animationView.startAnimation(sa);
1.3旋转 RotateAnimation
Java代码
//起始角度,最终角度,圆心X,Y坐标
//起始角度,最终角度,旋转模式,0.5f自身中心,旋转模式,0.5f自身中心
//RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(1000);
animationView.startAnimation(ra);

XML代码
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration ="1000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%" />
//调用
Animation ra1 = AnimationUtils.loadAnimation(this, R.anim.rorate);
animationView.startAnimation(ra1);
1.4 透明度 AlphaAnimation
Java代码
//起始透明度,结束透明度
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
animationView.startAnimation(aa);

XML代码
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    />
//调用
Animation aa1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
animationView.startAnimation(aa1);
1.5 动画集合
Java代码
 TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 0);
ScaleAnimation sa = new ScaleAnimation(4, 2, 4, 2,
	Animation.RELATIVE_TO_SELF, 0.5f,
	Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, 		Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation aa = new AlphaAnimation(0, 1);
AnimationSet animationSet = new AnimationSet(true);
ta.setDuration(1000);
sa.setDuration(1000);
ra.setDuration(1000);
aa.setDuration(1000);
animationSet.addAnimation(ta);
animationSet.addAnimation(sa);
animationSet.addAnimation(ra);
animationSet.addAnimation(aa);
animationSet.setDuration(1000);
baseAnimationView.startAnimation(animationSet);
XML代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially"
    android:fillAfter="false"
    android:zAdjustment="normal"
    >
    <!-- 数字为绝对值,x%p为父容器坐标值的x%-->
    <translate
        android:duration="1000"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="90%p"
        android:toYDelta="90%p" />
    <scale
        android:duration="1000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        />
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        />
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>
调用
Animation a = AnimationUtils.loadAnimation(this, R.anim.base_animation);
baseAnimationView.startAnimation(a);

旋转动画与平移动画做集合时,旋转坐标轴异常,即如何实现 滚动至指定位置动画?

2. 帧动画/AnimationDrawable

帧动画是通过顺序播放一系列图像从而产生动画效果,是一种简单的图片切换动画

XML代码 xml动画文件路径:res/drawable/xxxanimation.xml 
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/p4" android:duration="100"/>
    <item android:drawable="@drawable/p5" android:duration="100"/>
    <item android:drawable="@drawable/p6" android:duration="100"/>

</animation-list>
调用
 fAnimaitonView.setBackgroundResource(R.drawable.drawable_animation);
((AnimationDrawable) fAnimaitonView.getBackground()).start();

oneshot ture表示执行一次 false表示循环执行

3. 自定义动画

我们可以通过继承Animation类,重写它的initialize和applyTransformation方法来实现我们想要的效果,中间涉及数学上矩阵变换的概念,但实际开发中几乎用不到,因此待后续有时间做基础整理

4. 使用补充

4.1 动画监听

View动画不经常使用监听,但也有需要的时候

ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
	public void onAnimationStart(Animation animation) {
		//启动时回调
	}
	@Override
	public void onAnimationEnd(Animation animation) {
		//结束时回调
	}
	@Override
	public void onAnimationRepeat(Animation animation) {
		//重复时回调
	}
});
4.2 插值器

插值器可以理解为动画进行过程中不同阶段时候设置动画的速度值,如匀速运动,加速运动,减速运动等

  • interpolator 设置插值器,默认android:anim/accelerate_decelerate_interpolator 加速减速

    • @android:anim/accelerate_interpolator: 越来越快
    • @android:anim/decelerate_interpolator:越来越慢
    • @android:anim/accelerate_decelerate_interpolator:先快后慢
    • @android:anim/anticipate_interpolator: 先后退一小步然后向前加速
    • @android:anim/overshoot_interpolator:快速到达终点超出一小步然后回到终点
    • @android:anim/anticipate_overshoot_interpolator:先后退一小步,到达终点超出一小步然后回到终点
    • @android:anim/bounce_interpolator:到达终点产生弹球效果,弹几下回到终点
    • @android:anim/linear_interpolator:均匀速度。
    • @android:anim/cycle_interpolator 循环播放,其速率为正弦曲线
  • shareInterpolator 设置集合是否与集合中的动画共享一个插值器,不指定则使用单独指定或是默认插值器

4.3 特殊使用场景
  • LayoutAnimation 为ViewGroup指定子元素的出场动画

    //Java代码
    Animation aSet = AnimationUtils.loadAnimation(this, R.anim.animation_set_1);
    LayoutAnimationController controller = new LayoutAnimationController(aSet);
    controller.setOrder(LayoutAnimationController.ORDER_RANDOM);
    controller.setDelay(0.5f);
    content_ll.setLayoutAnimation(controller);
    
    //XML代码 
    <?xml version="1.0" encoding="utf-8"?>
    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="0.5"
        android:animationOrder="normal"
        android:animation="@anim/animation_set_1"/>
    //指定动画
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:shareInterpolator="true">
        <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            />
        <alpha
            android:fromAlpha="0.0"
            android:toAlpha="1.0" />
    </set>
    使用时在ViewGroup中指定     android:layoutAnimation="@anim/layout_animation"
    

    delay 表示子元素之间动画间隔时间,实际时间为动画周期*delay值

    animationOrder 子元素动画顺序: normal-顺序 reverse-逆序 random-随机

    animation 指定动画

  • Activity切换动画

    使用方法:overridePendingTransition(int enterAnim,int exitAnim)

    • 必须在startActivity或finish之后调用
    • enterAnim-打开时的动画资源,进入动画
    • exitAnim - 被暂停时的动画资源,退出动画
  • Fragment切换动画

    使用方法:setCustomAnimations(@AnimatorRes @AnimRes int enter, @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter, @AnimatorRes @AnimRes int popExit)

    • 在FragmentManager开启事务时进行操作
    • enter Fragment被添加或绑定时所呈现动画的资源id
    • exit Fragment被移除或解绑时所呈现动画的资源id
    • popEnter Fragment从栈中弹出时所呈现动画的资源id
    • popExit Fragment压入栈时所呈现动画的资源id

    v4包下的Fragment只能采用补间动画,因为属性动画是API11新引入的

  • 共享元素动画 5.0支持

    示例代码:

    1. 在共享的控件元素中 加入属性·`android:transitionName=“your_str”`
    2.跳转时设置 指定共享的视图元素
    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), shareView, "image");
    ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
    
    • ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,View sharedElement, String sharedElementName)
      activity-发起跳转的Activity,shareElement-共享的控件,sharedElementName-定义的字符串

    在Fragment如何使用共享元素转场动画
    Fragment的startActivity()方法无法传入ActivityOptionsCompat,所以需要使用
    ActivityCompat.startActivity()来进行跳转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值