美文网首页
Android属性动画的基本用法

Android属性动画的基本用法

作者: 疯子一般的勇士 | 来源:发表于2017-05-21 18:27 被阅读0次

    1.ObjectAnimator

            ImageView iv = (ImageView) findViewById(R.id.iv);
            oa1 = ObjectAnimator.ofFloat(iv, "translationX", -100, 2000);
            oa1.setDuration(1000);
            oa1.setRepeatCount(2);
            oa1.setInterpolator(new AccelerateDecelerateInterpolator());
            oa1.setRepeatMode(ObjectAnimator.REVERSE);
            oa1.start();
            oa2 = ObjectAnimator.ofFloat(iv, "translationY", 100, -100);
            oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 0.5f, 1.3f);
            oa4 = ObjectAnimator.ofFloat(iv, "scaleY", .1f, 0.5f,1.5f);
            oa5 = ObjectAnimator.ofFloat(iv, "alpha", 0.4f, 0.7f,0.2f,1f);
            oa6 = ObjectAnimator.ofFloat(iv, "rotation", -90, 200,-500,300);
            oa6.setDuration(1000);
            oa7 = ObjectAnimator.ofFloat(iv, "rotationX", -90, 200);
            oa7.setDuration(1000);
            oa8 = ObjectAnimator.ofFloat(iv, "rotationY", -90, 180);
            oa8.setDuration(1000);
            oa9 = ObjectAnimator.ofFloat(iv, "x", 100,200);
            oa9.setDuration(1000);
            oa10 = ObjectAnimator.ofFloat(iv, "y", 300,500);
            oa10.setDuration(1000);
            oa11 = ObjectAnimator.ofInt(iv, "backgroundColor", Color.argb
                                        (200,130,180,150),Color.argb(200,80,50,70));
            oa11.setDuration(1000);
    

    2.ValueAnimator

            ValueAnimator va1 = ValueAnimator.ofFloat(0,1);
            va1.setDuration(3000);
            va1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();
                    iv.setAlpha(value);
                    iv.setScaleX(value);
                    iv.setRotation(value*360);
                }
            });
            va1.start();
            ValueAnimator va2 = ValueAnimator.ofInt(0,300,100);
    

    3 AnimatorSet

            AnimatorSet set1 = new AnimatorSet();
            set1.setDuration(300);
            set1.setTarget(iv);
            set1.playTogether(oa2,oa3,oa4,oa5);
            set1.start();
    
            AnimatorSet set2 = new AnimatorSet();
            set2.setTarget(iv);
            set2.play(oa6).before(oa7).with(oa8).with(oa9).with(oa10).after(oa11);
            set2.start();
    

    4.PropertyValuesHolder

            PropertyValuesHolder pr1 = PropertyValuesHolder.ofFloat("translationX",100,     
                                                                       -300,500);
            PropertyValuesHolder pr2 = PropertyValuesHolder.ofFloat("alpha",0.3f,1f);
            PropertyValuesHolder pr3 = PropertyValuesHolder.ofFloat("rotationX",-90,135);
            ObjectAnimator oa12 = ObjectAnimator.ofPropertyValuesHolder(iv,pr1,pr2,pr3);
            oa12.setDuration(2000);
            oa12.start();
    
            ValueAnimator va3 = ValueAnimator.ofPropertyValuesHolder(pr1,pr2,pr3);
            va3.setDuration(2000);
            va3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float translationX = (float) animation.getAnimatedValue("translationX");
                    float alpha = (float) animation.getAnimatedValue("alpha");
                    float rotationX = (float) animation.getAnimatedValue("rotationX");
                    iv.setTranslationX(translationX);
                    iv.setAlpha(alpha);
                    iv.setRotationX(rotationX);
                }
            });
            va3.start();
    

    5.ViewPropertyAnimator

             iv.animate().setDuration(3000).scaleX(0.5f).scaleY(1.3f).translationX(300).rotation(90).start();
    

    在res文件夹下创建一个animator文件夹

    ValueAnimator
    新建一个value.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <animator xmlns:android="http://schemas.android.com/apk/res/android"
        android:valueFrom="0.1f"
        android:valueTo="1f"
        android:valueType="floatType"
        android:duration="3000"
        />
    

    ObjectAnimator
    新建一个object.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="backgroundColor"
        android:repeatCount="infinite"
        android:duration="300"
        android:repeatMode="reverse"
        android:valueFrom="#456412"
        android:valueTo="#780054"
        />
    

    AnimatorSet
    新建一个animatorSet.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:ordering="together">
        <objectAnimator
            android:duration="500"
            android:valueType="floatType"
            android:propertyName="x"
            android:valueFrom="0"
            android:valueTo="300dp"
            />
    
        <set android:ordering="sequentially">
            <objectAnimator
                android:propertyName="alpha"
                android:valueFrom="0"
                android:valueTo="1f"
                android:valueType="floatType"
                android:duration="500"
                />
    
            <objectAnimator
                android:propertyName="rotation"
                android:valueType="floatType"
                android:valueFrom="-90"
                android:valueTo="180"
                android:duration="1000"
                />
        </set>
    </set>
    

    在代码中使用

            ObjectAnimator oa13 = (ObjectAnimator) AnimatorInflater.loadAnimator 
                          (this,R.animator.object);
    
            ValueAnimator va4 = (ValueAnimator)AnimatorInflater.loadAnimator
                           (this,R.animator.value);
            
            AnimatorSet set3 = (AnimatorSet) AnimatorInflater.loadAnimator                       
                           (this,R.animator.animatorSet);
    

    PS.属性动画是一个很强大的动画库,卫星菜单、抽屉等效果都可以通过属性动画来实现,这些只是最基本的使用方法而已。
    再PS. 如果想进一步深入了解属性动画请移步启舰、郭霖、张鸿洋、任玉刚等大神的博客

    相关文章

      网友评论

          本文标题:Android属性动画的基本用法

          本文链接:https://www.haomeiwen.com/subject/tfmuxxtx.html