属性动画

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="bwie.myamimation.MainActivity"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/tab_shopping_pressed" />

    <Button
        android:id="@+id/chuizhi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="垂直" />

    <Button
        android:id="@+id/paowuxian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="抛物线 动画结束后移除" />

    <Button
        android:id="@+id/togetherRun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="简单的多动画" />

    <Button
        android:id="@+id/playWithAfter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按次序执行" />
</LinearLayout>
package bwie.myamimation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private ImageView ima;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button togetherRun = (Button) findViewById(R.id.togetherRun);
        Button playWithAfter = (Button) findViewById(R.id.playWithAfter);
        Button chuizhi = (Button) findViewById(R.id.chuizhi);
        Button paowuxian = (Button) findViewById(R.id.paowuxian);
        ima = (ImageView) findViewById(R.id.imageview);
        /* ObjectAnimator 透明+缩放
        ima.setOnClickListener(new View.OnClickListener() {
            public ObjectAnimator anim;
            @Override
            public void onClick(final View view) {
                anim = ObjectAnimator.ofFloat(view, "zhy", 1.0F, 0.0F).setDuration(500);
                anim.start();
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float cVal = (float) valueAnimator.getAnimatedValue();
                        view.setAlpha(cVal);
                        view.setScaleX(cVal);
                        view.setScaleY(cVal);
                    }
                });
            }
        });
        */
        /**
         * 透明度+缩放  PropertyValuesHolder
         */
        ima.setOnClickListener(listener);
        chuizhi.setOnClickListener(listenerchuizhi);
        paowuxian.setOnClickListener(listenerpaowuxian);
        togetherRun.setOnClickListener(listenertogetherRun);
        playWithAfter.setOnClickListener(listenerplayWithAfter);
    }

    /**
     * 多动画按次序执行
     */
    View.OnClickListener listenerplayWithAfter = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float cx = ima.getX();

            ObjectAnimator anim1 = ObjectAnimator.ofFloat(ima, "scaleX",
                    1.0f, 2f);
            ObjectAnimator anim2 = ObjectAnimator.ofFloat(ima, "scaleY",
                    1.0f, 2f);
            ObjectAnimator anim3 = ObjectAnimator.ofFloat(ima,
                    "x", cx, 0f);
            ObjectAnimator anim4 = ObjectAnimator.ofFloat(ima,
                    "x", cx);

            /**
             * anim1,anim2,anim3同时执行
             * anim4接着执行
             */
            AnimatorSet animSet = new AnimatorSet();
            animSet.play(anim1).with(anim2);
            animSet.play(anim2).with(anim3);
            animSet.play(anim4).after(anim3);
            animSet.setDuration(1000);
            animSet.start();
        }
    };
    /**
     * 简单的多动画
     */
    View.OnClickListener listenertogetherRun = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ObjectAnimator anim1 = ObjectAnimator.ofFloat(ima, "scaleX",
                    1.0f, 2f);
            ObjectAnimator anim2 = ObjectAnimator.ofFloat(ima, "scaleY",
                    1.0f, 2f);
            AnimatorSet animSet = new AnimatorSet();
            animSet.setDuration(2000);
            animSet.setInterpolator(new LinearInterpolator());
            //两个动画同时执行
            animSet.playTogether(anim1, anim2);
            animSet.start();
        }
    };
    /**
     * 抛物线  ValueAnimator
     */
    View.OnClickListener listenerpaowuxian = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ValueAnimator valueAnimator = new ValueAnimator();
            valueAnimator.setDuration(3000);
            valueAnimator.setObjectValues(new PointF(0, 0));
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
                // fraction = t / duration
                @Override
                public PointF evaluate(float fraction, PointF startValue,
                                       PointF endValue) {

                    // x方向200px/s ,则y方向0.5 * 10 * t
                    PointF point = new PointF();
                    point.x = 200 * fraction * 3;
                    point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
                    return point;
                }
            });
            /**
             * 监听动画结束 图片移除不再占用位置
             */
            valueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {

                    ViewGroup parent = (ViewGroup) ima.getParent();
                    if (parent != null)
                        parent.removeView(ima);
                }
            });
            valueAnimator.start();
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    PointF point = (PointF) valueAnimator.getAnimatedValue();
                    ima.setX(point.x);
                    ima.setY(point.y);
                }
            });
        }
    };

    /**
     * 点击垂直按钮自由落体  ValueAnimator
     */
    View.OnClickListener listenerchuizhi = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 600
                    - ima.getHeight());
            animator.setTarget(ima);

            animator.setDuration(1000).start();
            //animator.setInterpolator(value)
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    ima.setTranslationY((Float) valueAnimator.getAnimatedValue());
                }
            });
        }
    };
    /**
     * 透明度+缩放
     */
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
                    0f, 1f);
            PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
                    0, 1f);
            PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
                    0, 1f);
            ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
        }
    };
}

    文章来源:

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

    http://blog.csdn.net/lmj623565791/article/details/38067475

相关推荐