欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android基础_帧动画 补间动画 属性动画(十一)

程序员文章站 2022-03-16 14:10:57
...

帧动画

1.在drawable目录下创建一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/p1" android:duration="200"></item>
    <item android:drawable="@drawable/p2" android:duration="200"></item>
    <item android:drawable="@drawable/p3" android:duration="200"></item>
    <item android:drawable="@drawable/p4" android:duration="200"></item>
    <item android:drawable="@drawable/p5" android:duration="200"></item>
    <item android:drawable="@drawable/p6" android:duration="200"></item>
    <item android:drawable="@drawable/p7" android:duration="200"></item>
    <item android:drawable="@drawable/p8" android:duration="200"></item>
</animation-list>

2.在布局文件中使用
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_animation_list" />

3.在代码中运行
        mIv=(ImageView) findViewById(R.id.iv);
        AnimationDrawable drawable = (AnimationDrawable) mIv.getDrawable();
        drawable.start();

补间动画之代码实现

    /*  1.Animation ani = new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
     *          Type:参考坐标系类型  1.控件在容器中的坐标系 2.绝对值坐标系 3.以自己本身
     *  2.指定动画的时间
     *      ani.setDuration(3000);
     *  ps:停在最后一帧 
     *      ani.setFillAfter(true);
     *  ps.设置加速
     *      ani.setInterpolater(new AccelerateInterpolator());
     *      减速:new decelerateInterpolator();
     *  3.miv.startanimation(ani);
     * */
    public void fly(View v){
        Animation ani = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0, 
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0, 
                Animation.RELATIVE_TO_PARENT, 0.8f);
        ani.setDuration(3000);

        ani.setFillAfter(true);
        ani.setInterpolator(new AccelerateInterpolator());

        mIv.startAnimation(ani);
    }
    /*  1.Animation ani = new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);
     *          
     *  2.指定动画的时间
     *      ani.setDuration(3000);
     *  3.miv.startanimation(ani);
     * */
    public void beSmall(View v){
        Animation ani = new ScaleAnimation(
                1.0f, 2.0f,
                1.0f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0, 
                Animation.RELATIVE_TO_SELF, 0);
        ani.setDuration(3000);

        ani.setFillAfter(true);
        ani.setInterpolator(new AccelerateInterpolator());

        mIv.startAnimation(ani);
    }
    /*  1.Animation ani = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
                    fromdegress 旋转角  0 - 360
     *  2.指定动画的时间
     *      ani.setDuration(3000);
     *  3.miv.startanimation(ani);
     * */
    public void rotate(View v){

        Animation ani = new RotateAnimation(
                0, 500, 
                Animation.RELATIVE_TO_SELF, 0.5f, 
                Animation.RELATIVE_TO_SELF, 0.5f);
        ani.setDuration(3000);

        ani.setFillAfter(true);
        ani.setInterpolator(new AccelerateInterpolator());

        mIv.startAnimation(ani);
    }
    /*  1.Animation ani = new AlphaAnimation(fromAlpha, toAlpha);
                    fromAlpha 0.0f-1.0f
     *  2.指定动画的时间
     *      ani.setDuration(3000);
     *  3.miv.startanimation(ani);
     * */
    public void transparent(View v){
        Animation ani = new AlphaAnimation(1.0f, 0);
        ani.setDuration(3000);

        ani.setFillAfter(true);
        ani.setInterpolator(new AccelerateInterpolator());

        mIv.startAnimation(ani);
    }
    /*1.创建一个动画集对象
     * AnimationSet/new AnimationSet(false);
     *      false:分享速率器 如果子动画有不同的速率,那么子动画的速率不合并 
     *      true:以set的速率为准
     *2.创建不同的子动画
     *3.将所有的子动画添加进set
     * set.addAnimation(ani);
     * */
    public void set(View v){
        AnimationSet set = new AnimationSet(false);
        Animation ani = new AlphaAnimation(1.0f, 0);
        Animation ani2 = new RotateAnimation(
                0, 3600, 
                Animation.RELATIVE_TO_SELF, 0.5f, 
                Animation.RELATIVE_TO_SELF, 0.5f);
        Animation ani3 = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0, 
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0, 
                Animation.RELATIVE_TO_PARENT, 0.8f);
        set.addAnimation(ani);
        set.addAnimation(ani2);
        set.addAnimation(ani3);

        set.setDuration(3000);

        set.setFillAfter(true);

        mIv.startAnimation(set);
    }

补间动画的xml实现

1.在res下创建一个anim文件夹
2.创建一个xml文件
透明度:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0" 
    android:toAlpha="0"
    android:duration="3000">
</alpha>

缩放:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.5"
    android:toYScale="0.5" 
    android:pivotX="50%"
    android:pivotY="50%"/>

旋转:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0" 
    android:toDegrees="720"
    android:duration="3000"
    android:pivotX="50%"
    android:pivotY="50%"
    />

位移:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="300%"
    android:toYDelta="300%" 
    android:duration="3000"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator"
    />

补间动画集合
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:shareInterpolator="false" >

    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0" >
    </alpha>

    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" />

</set>

3.在代码中开启动画
        Animation ani = AnimationUtils.loadAnimation(this, R.anim.my_translate);
        mIv.startAnimation(ani);

属性动画的代码实现

    /*使用ObjectAnimator实现平移
     * 1.创建一个objectAnimator对象 
     *      ObjectAnimator.ofFloat(target, property, values);
     *      1.target  操纵的控件     
     *      2.property 字符串/属性名
     * 2.设置启动时间
     * 3.设置重复次数
     *      anmi.setRepeat(int);
     * ps.设置重复方式
     *      anmi.setRepeatMode(ObjectAnmitor.XX)
     * 4.启动
     *      anmi.start();
     * */
    public void translate(View v){
        ObjectAnimator anm = ObjectAnimator.ofFloat(mIv, "x", 500f,0f,500f);
        ObjectAnimator anm2 = ObjectAnimator.ofFloat(mIv, "y", 0f,200f,0f,200f,0f);
        anm.setRepeatCount(5);
        anm.setDuration(3000);
        anm.start();
        anm2.setRepeatCount(5);
        anm2.setDuration(3000);
        anm2.start();
    }

    public void rotate(View v) {
        ObjectAnimator anmi = ObjectAnimator.ofFloat(mIv, "rotation", 0, 360,
                0, -360, 0);
        anmi.setDuration(3000);
        anmi.setRepeatCount(5);
        anmi.setRepeatMode(ValueAnimator.RESTART);
        anmi.start();

        ObjectAnimator anmi2 = ObjectAnimator.ofFloat(mIv, "rotationX", 0, 360,
                0, -360, 0);
        anmi2.setDuration(3000);
        anmi2.setRepeatCount(5);
        anmi2.setRepeatMode(ValueAnimator.RESTART);
        anmi2.start();
    }
    public void beSmall(View v){
        ObjectAnimator anmi = ObjectAnimator.ofFloat(mIv, "scaleX", 1.0f,0.5f,2.0f);
        anmi.setDuration(3000);
        anmi.start();
    }
    public void transparent(View v){
        ObjectAnimator anmi = ObjectAnimator.ofFloat(mIv, "alpha", 1.0f,0.5f,1.0f,0.3f);
        anmi.setRepeatCount(ValueAnimator.INFINITE);
        anmi.setRepeatMode(ObjectAnimator.REVERSE);
        anmi.setDuration(3000);
        anmi.start();
    }

属性动画的xml实现

1.res目录下创建一个animtor文件夹

2.创建一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="0"
    android:valueTo="500"
    android:duration="3000"
    android:repeatCount="5">

</objectAnimator>

3.代码实现
     * 代码添加到activity中
     *  1.创建objectValue类型
     *      AnimatorInflater.loadAnimator(context,animator);
     *  2.关联图片控件
     *      anim.setTarget(mIv);
     *  3.anim.start();
     * */
    public void translate(View v){
        Animator anim = AnimatorInflater.loadAnimator(this, R.animator.my_object_animator);
        anim.setTarget(mIv);
        anim.start();
    }

设置属性动画的监听器

    /*动画监听器
     * 1.anim.addListener();
     *      1.new AnimatorListener()
     *      2.new AnimatorListenerAdapter()
     * */
    public void translate2(View v) {
        ObjectAnimator anmi = ObjectAnimator.ofFloat(mIv, "translationX", 0f,300f);
        anmi.setDuration(4000);
        anmi.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                Log.v("meeeeeee", "开始");
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                Log.v("meeeeeee", "重复");

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Log.v("meeeeeee", "结束");

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                Log.v("meeeeeee", "取消");

            }
        });
        anmi.addListener(new AnimatorListenerAdapter() {
        });
        anmi.start();
    }

属性动画集合的代码实现

    /*属性动画集
     * 1.创建属性动画集的类
     *      AnimatorSet/ new Animatorset();
     * 2.创建多个动画集子类
     *      ObjectAnimator/ObjectAnimator.ofFloat();
     *      miv.setX  在父容器的具体位置
     *      miv.settranslationX 在当前位置做一个偏移
     * 3.将子类动画添加进去
     *      1.set.playSequentially(xA,yA,rA);
     *      2.set.playTogether(xA,yA,rA);
     * 4.设置动画时间;
     * 5.启动
     * */
    public void set(View v){
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator xA = ObjectAnimator.ofFloat(mIv, "x", 0f,100f,300f);
        ObjectAnimator yA = ObjectAnimator.ofFloat(mIv, "y", 0f,200f,600f);
        ObjectAnimator rA = ObjectAnimator.ofFloat(mIv, "rotationY", 0f,360f,1080f);
        set.playSequentially(xA,yA,rA);
//      set.playTogether(xA,yA,rA);
        set.setDuration(3000);
        set.start();
    }

属性动画的xml实现

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together" >

    <objectAnimator
        android:duration="3000"
        android:propertyName="translationX"
        android:valueFrom="0"
        android:valueTo="300"
        android:valueType="floatType" >
    </objectAnimator>
    <objectAnimator
        android:duration="3000"
        android:propertyName="translationY"
        android:valueFrom="0"
        android:valueTo="200"
        android:valueType="floatType" >
    </objectAnimator>
    <objectAnimator
        android:duration="3000"
        android:propertyName="rotationY"
        android:valueFrom="0"
        android:valueTo="720"
        android:valueType="floatType" >
    </objectAnimator>

</set>

    public void set(View v){
        Animator anim = AnimatorInflater.loadAnimator(this, R.animator.my_set);
        anim.setTarget(mIv);
        anim.start();
    }