属性动画的使用

Android动画

android实现动画的方式

1. View Animation:补间动画
    1. 位移,缩放,旋转,渐变,集合
2. Drawable Animation:帧动画
3. Property Animation:属性动画  
    1. 补间动画可以做的属性动画都可以做,反之则不是
    2. android 3.0才有
    3. 补间动画,控件看似改变了位置,但是控件的焦点原来的位置
4. 自定义动画
    1. 自己通过继承Animation自己实现动画类,但是有了属性动画,可以用属性动画替代这种做法.

如果让android 3.0也可以执行属性动画?

使用nineoldandroids兼容库

补间动画的实现原理/核心?

Animation.class中定义的protected void applyTransformation(float interpolatedTime, Transformation t),回调一些列的渐变值

//子类需要实现该方法,通过给定的interpolatedTime(0-1)的渐变值,实现自己的变化效果
protected void applyTransformation(float interpolatedTime, Transformation t) {

}

属性动画和补间动画的最大区别是?

补间动画,控件看似改变了位置,但是控件的焦点原来的位置

属性动画2个关键类

* public class ValueAnimator extends Animator

        public static ValueAnimator ofFloat(float... values) 
        public static ValueAnimator ofInt(int... values)

* public final class ObjectAnimator extends ValueAnimator

        public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) 
        public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values)

通过xml配置属性动画

属性动画2个关键监听

* 监听动画过程

        public void addListener(AnimatorListener listener) 

* 得到动画执行过程中的渐变值

        public void addUpdateListener(AnimatorUpdateListener listener)

属性动画原理

和普通动画类似,就是生成一些列渐变值,然后计算不同时间点的渐变值,然后改变UI

public interface TypeEvaluator<T> {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * <code>fraction</code> representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>,
     * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
     * and <code>t</code> is <code>fraction</code>.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value.
     * @param endValue   The end value.
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public T evaluate(float fraction, T startValue, T endValue);

}

练习

  1. 位移动画(translationX,translationY,translation)

    ObjectAnimator animator = ObjectAnimator.ofFloat(mIv, "translationX", 0, 100, 200, 100);
                animator.setInterpolator(new AccelerateDecelerateInterpolator());
                animator.setDuration(300);
                animator.start();
    
  2. 缩放(scrollX,scrollY,scroll)

    ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mIv, "scaleX", 0, 1, 2, 1, 0);
                    scaleXAnimator.setDuration(1000);
                    scaleXAnimator.start();
    
  3. 透明度(alpha)

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mIv, "alpha", 0, 1, 0);
                        alphaAnimator.setDuration(2000);
                        alphaAnimator.start();
    
  4. 旋转(rotateX,rotateY,rotation)

    ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mIv, "rotation", 0, 360, 0);
                    rotationAnimator.setDuration(1000);
                    rotationAnimator.start();
    
  5. 设置背景颜色渐变

       ObjectAnimator backgroundColorAnimator =
                                ObjectAnimator.ofObject(mIv, "backgroundColor", new ArgbEvaluator(), Color.RED,
                                        Color.BLUE);
                        backgroundColorAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                        backgroundColorAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                        backgroundColorAnimator.start();
    
  6. 组合动画

    ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mIv, "rotation", 0, 360, 0);
    ObjectAnimator translationAnimator =
            ObjectAnimator.ofFloat(mIv, "translationX", 0, 100, 200, 100, 0);
    
    AnimatorSet set = new AnimatorSet();
    // set.playSequentially(rotationAnimator, translationAnimator);// 顺序播放
    set.playTogether(rotationAnimator, translationAnimator);// 一起播放
    set.setDuration(2000);
    set.start();
    
  7. xml定义组合动画

    Animator animator =
    AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_rotation_translate);
    animator.setTarget(mIv);
    animator.start();

  8. 监听动画执行过程

    ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
            valueAnimator.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
                }
            });
            valueAnimator.start();
    }
    
  9. 得到动画渐变值

    valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int progress = (Integer) animation.getAnimatedValue();
                    ((Button) findViewById(R.id.btn10)).setText(progress + "%");
                }
    });
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值