【Android基础】动画

一、帧动画 Frame Animation
1.在res/drawable文件夹下创建.xml文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@drawable/pic1"
        android:duration="200" />

    <item
        android:drawable="@drawable/pic2"
        android:duration="200" />

    <item
        android:drawable="@drawable/pic3"
        android:duration="200" />

    <item
        android:drawable="@drawable/pic4"
        android:duration="200" />

</animation-list>

2.在代码中获取ImageView的背景强转为AnimationDrawable类型的对象

ImageView imageView = findViewById(R.id.imageView);  imageView.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable background = (AnimationDrawable) imageView.getBackground();
background.start();

2.布局动画 Layout Animation
设置ViewGroup的子控件动态显示的动画

ListView listView = (ListView)findViewById(R.id.listView);
LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.layout_animation));       lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
listView.setLayoutAnimation(lac);
listView.startLayoutAnimation();

3.补间动画 Tween Animation
从xml文件加载动画:AnimationUtils.loadAnimation(this,R.anim.layout_animation)
1.TranslationAnimation
构造函数:1)TranslationAnimation(fromXDelta,toXDelta,fromYDelta,toYDelta);
分别表示相对于控件原始坐标原点的相对偏移量
2)TranslationAnimation(fromXType,fromXValue,toXType,toXValue,fromYType,fromYValue,toYType,toYValue);
Type为Animation.RELATIVE_TO_SELF时,Value表示相对于自身原点坐标的自身尺寸倍数的偏移量
Type为Animation.RELATIVE_TO_PARENT时,Value表示相对于父控件原点的父控件尺寸倍数的偏移量
2.ScaleAnimation
构造函数1)
ScaleAnimation(fromX,toX,fromY,toY);
以控件左上角为缩放点,x,y方向上的缩放倍数
2)ScaleAnimation(fromX,toX,fromY,toY,pivotX,pivotY);
pivotX,pivotY表示以控件左上角的偏移量作为缩放点
3)ScaleAnimation(fromX,toX,fromY,toY,pivotXType,pivotXValue,pivotXType,pivotYValue);
Type为Animation.RELATIVE_TO_SELF时,Value表示相对于自身原点坐标的自身尺寸倍数的偏移量作为缩放点
Type为Animation.RELATIVE_TO_PARENT时,Value表示相对于父控件原点的父控件尺寸倍数的偏移量作为缩放点
3.RotateAnimation
1)RotateAnimation(fromDegree,toDegree);
旋转中心点为控件左上角
2)RotateAnimation(fromDegree,toDegree,pivotX,pivotY);
旋转中心点为相对控件左上角的偏移
3)RotateAnimation(fromDegree,toDegree,pivotXType,pivotXValue,pivotXType,pivotYValue);
Type为Animation.RELATIVE_TO_SELF时,Value表示相对于自身原点坐标的自身尺寸倍数的偏移量作为旋转中心点
Type为Animation.RELATIVE_TO_PARENT时,Value表示相对于父控件原点的父控件尺寸倍数的偏移量作为旋转中心点

动画集合:AnimationSet.addAnimation();

4.AlphaAnimation
构造函数:AlphaAnimation(fromAlpha,toAlpha);
四、属性动画 Property Animation
ObjectAnimator只能操作有get,set方法的属性,如果要对其它属性进行操作,用一个类来包装原始对象,间接为其提供get,set方法。

ObjectAnimator oa1 = ObjectAnimator.ofFloat(imageView,"translationX",0,0.5f,1,1.5f,2f);
oa1.start();

PropertyValuesHolder来对多个属性进行同时操作,该类对多属性动画进行了优化,会合并一些invalidate()来减少刷新视图

PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX",0,2f);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("rotation",0,360f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2);
objectAnimator.start();

AnimatorSet可以对多个属性进行不同顺序的操作

        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView,"scaleX",0.5f,1);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView,"scaleY",0.5f,1);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView,"rotation",0,360f);
        animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3);
        animatorSet.playSequentially(objectAnimator1,objectAnimator2,objectAnimator3);
        animatorSet.play(objectAnimator1).with(objectAnimator2);
        animatorSet.play(objectAnimator3).after(objectAnimator2);
        animatorSet.start();

为属性动画设置监听:addListener(new Animator.AnimatorListener())或者addListener(new Animator.AnimatorListenerAdapter())
ValueAnimator

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,5);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animation.getAnimatedValue();
            }
        });

估值器TypeEvaluator
插值器Interpolater

  • AccelerateInterpolator  加速,开始时慢中间加速
  • DecelerateInterpolator 减速,开始时快然后减速
  • AccelerateDecelerateInterolator 先加速后减速,开始结束时慢,中间加速
  • AnticipateInterpolator  反向 ,先向相反方向改变一段再加速播放
  • AnticipateOvershootInterpolator  反向加回弹,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
  • BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
  • CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
  • LinearInterpolator  线性,线性均匀改变
  • OvershottInterpolator 回弹,最后超出目的值然后缓慢改变到目的值
  • TimeInterpolator 一个接口,允许你自定义interpolator,以上几个都是实现了这个接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值