android动画(三)属性动画

来源:互联网 发布:java开源项目 编辑:程序博客网 时间:2024/06/01 22:10
引入属性动画:
补间动画缺点:1、改变了View的显示效果而已,而不会真正去改变View的属性。什么意思呢?比如说,现在屏幕的左上角有一个按钮,然后我们通过补间动画将它移动到了屏幕的右下角,现在你可以去尝试点击一下这个按钮,点击事件是绝对不会触发的,因为实际上这个按钮还是停留在屏幕的左上角,只不过补间动画将这个按钮绘制到了屏幕的右下角而已。
 2、补间动画是只能对View对象进行动画操作的
 所以引入属性动画:
1、属性动画
高级用法:可以不只针对view使用,补间动画是只能对View对象进行动画操作的。而属性动画就不再受这个限制,它可以对任意对象进行动画操作。
如说我们有一个自定义的View,在这个View当中有一个Point对象用于管理坐标,然后在onDraw()方法当中就是根据这个Point对象的坐标值来进行绘制的。也就是说,如果我们可以对Point对象进行动画操作,那么整个自定义View的动画效果
TypeEvaluator:ValueAnimator.ofFloat()方法就是实现了初始值与结束值之间的平滑过度实际上使用了FloatEvaluator(继承于TypeEvaluator)

ofObject():ValueAnimator的ofFloat()和ofInt()方法,分别用于对浮点型和整型的数据进行动画操作的,但实际上ValueAnimator中还有一个ofObject()方法,是用于对任意对象进行动画操作的
ValueAnimator anim =ValueAnimator.ofFloat(0f,1f);//0过渡到1,可以设置更多参数anim.setDuration(300);anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {        float currentValue = (float) animation.getAnimatedValue();        Log.i("XQLOG", "cuurent value is " + currentValue);    }});TextView tv =null;//淡入淡出ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0f, 1f);//旋转ObjectAnimator animator01 = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f);float curTranslationX = tv.getTranslationX();//平移ObjectAnimator animator02 = ObjectAnimator.ofFloat(tv, "translationX", curTranslationX, -500f, curTranslationX);//垂直方向放大后还原ObjectAnimator animator03 = ObjectAnimator.ofFloat(tv, "scaleY", 1f, 3f, 1f);animator.setDuration(5000);animator.start();anim.start();animator.addListener(new Animator.AnimatorListener() {    @Override    public void onAnimationStart(Animator animation) {    }    @Override    public void onAnimationEnd(Animator animation) {    }    @Override    public void onAnimationCancel(Animator animation) {    }    @Override    public void onAnimationRepeat(Animator animation) {    }});animator.addListener(new AnimatorListenerAdapter() {    @Override    public void onAnimationCancel(Animator animation) {        super.onAnimationCancel(animation);    }});//组合动画ObjectAnimator moveIn = ObjectAnimator.ofFloat(tv, "translationX", -500f, 0f);ObjectAnimator rotate = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f);ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0f, 1f);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(rotate).with(fadeInOut).after(moveIn);

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*
xml和上述组合动画效果一样
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);animator.setTarget(view);animator.start(); <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <objectAnimator android:duration="2000" android:propertyName="translationX" android:valueFrom="-500" android:valueTo="0" android:valueType="floatType" > </objectAnimator> <set android:ordering="together" > <objectAnimator android:duration="3000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" android:valueType="floatType" > </objectAnimator> <set android:ordering="sequentially" > <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" > </objectAnimator> <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" > </objectAnimator> </set> </set> </set> */

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//高级用法        Point point1 = new Point(0, 0);        Point point2 = new Point(300, 300);        ValueAnimator anim01 = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);        anim01.setDuration(5000);        anim01.start();        //Interpolator实现非线性运动的    }    class Point {        private float x;        private float y;        public Point(float x, float y) {            this.x = x;            this.y = y;        }        public float getX() {            return x;        }        public float getY() {            return y;        }    }    class PointEvaluator implements TypeEvaluator {        @Override        public Object evaluate(float fraction, Object startValue, Object endValue) {            Point startPoint = (Point) startValue;            Point endPoint = (Point) endValue;            float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());            float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());            Point point = new Point(x, y);            return point;        }    }

2、Interpolator可以实现非线性运动等
很多实现类类似于:AccelerateInterpolator就是一个加速运动的Interpolator,而DecelerateInterpolator就是一个减速运动的Interpolator。
使用属性动画时,系统默认的Interpolator其实就是一个先加速后减速的Interpolator,对应的实现类就是AccelerateDecelerateInterpolator。
调用 anim.setInterpolator(new AccelerateInterpolator(2f));  使用不同的Interpolator
private void startAnimation() {  
    Point startPoint = new Point(getWidth() / 2, RADIUS);  
    Point endPoint = new Point(getWidth() / 2, getHeight() - RADIUS);  
    ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);  
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
        @Override  
        public void onAnimationUpdate(ValueAnimator animation) {  
            currentPoint = (Point) animation.getAnimatedValue();  
            invalidate();  
        }  
    });  
    anim.setInterpolator(new AccelerateInterpolator(2f));  
    anim.setDuration(2500);  
    anim.start();  

}  


转载:

http://blog.csdn.net/guolin_blog/article/details/43536355http://blog.csdn.net/guolin_blog/article/details/43816093http://blog.csdn.net/guolin_blog/article/details/44171115

0 0
原创粉丝点击