属性动画

package com.bwie.propertyanimationdemo;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button btnStart;
    private Button btnStartTween;
    private Button btnStop;
    private TextView txtHello;
    private ObjectAnimator animatr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = (Button) findViewById(R.id.btn_start);
        btnStartTween = (Button) findViewById(R.id.btn_start_tween);
        btnStop = (Button) findViewById(R.id.btn_stop);
        txtHello = (TextView) findViewById(R.id.txt_hello);


        txtHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: " + v.getX());
            }
        });


        // 补间动画的属性并没有发生变化,属性动画是真正改变了视图的值
        btnStartTween.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TranslateAnimation animation = new TranslateAnimation(0, 500, 0, 0);
                animation.setDuration(3000);
                txtHello.startAnimation(animation);
            }
        });


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 可以对一个值进行平滑的改变
                // ValueAnimator只是对一个值做的改变
//                ValueAnimator animator = ValueAnimator.ofInt(1, 9);
//                ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
//                animator.setDuration(3000);
//                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                    @Override
//                    public void onAnimationUpdate(ValueAnimator animation) {
//                        Log.i(TAG, "onAnimationUpdate: " + animation.getAnimatedValue());
//                    }
//                });
//                animator.start();


//                txtHello.setTranslationX(500);
//                txtHello.setTextColor();


                // 属性动画可以完成补间动画的所有效果,也可以实现其他补间动画没有的效果,
                // 属性动画本质上是改变了视图的属性,所以只要视图有公开的set方法,就可以进行变换
//                ObjectAnimator animatr = ObjectAnimator.ofInt(
//                        txtHello,
//                        "textColor",
//                        Color.RED, Color.GREEN
//                );

//                txtHello.setAlpha();

//                ObjectAnimator animatr = ObjectAnimator.ofFloat(
//                        txtHello,
//                        "alpha",
//                        1, 0
//                );

//                txtHello.setRotation();

//                animatr = ObjectAnimator.ofFloat(
//                        txtHello,
//                        "rotation",
//                        90, 360
//                );

//                animatr = ObjectAnimator.ofFloat(
//                        txtHello,
//                        "translationX",
//                        0, 500
//                ).ofFloat(
//                        txtHello,
//                        "rotation",
//                        0, 360
//                );


                Animator animatr01 = ObjectAnimator.ofFloat(
                        txtHello,
                        "translationX",
                        0, 500
                );
                Animator animatr02 = ObjectAnimator.ofFloat(
                        txtHello,
                        "rotation",
                        0, 360
                );
                Animator animatr03 = ObjectAnimator.ofFloat(
                        txtHello,
                        "alpha",
                        0, 1
                );

                Animator animatr04 = ObjectAnimator.ofFloat(
                        txtHello,
                        "scaleX",
                        0, 1
                );

//                animatr01.setDuration(3000);
//                animatr02.setDuration(3000);
//                animatr03.setDuration(3000);
//                animatr04.setDuration(3000);

                // 动画的集合
                AnimatorSet set = new AnimatorSet();
                set.setDuration(3000);
                set.play(animatr01) // 平移
                        // 在。。。之后
                        .after(animatr02) // 旋转
                        // 和....一起
                        .with(animatr03) // 透明度
                        // 在。。。。之前
                        .before(animatr04); // 缩放
                set.start();

                // 旋转-透明度/平移-缩放

                // 动画自定义差值器
                // 先加速后减速
//                animatr.setInterpolator(new AccelerateDecelerateInterpolator());
                // 加速
//                animatr.setInterpolator(new AccelerateInterpolator());
                // 减速
//                animatr.setInterpolator(new DecelerateInterpolator());
                // 匀速
//                animatr.setInterpolator(new LinearInterpolator());
//
//                animatr.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                    @Override
//                    public void onAnimationUpdate(ValueAnimator animation) {
//                        Log.i(TAG, "onAnimationUpdate:" + animation.getAnimatedValue());
//                    }
//                });

//                ObjectAnimator animatr = ObjectAnimator.ofFloat(
//                        txtHello,
//                        "translationX",
//                        0, 500


//                );

//                animatr.setDuration(3000);
                animatr.setRepeatCount(3);
//                animatr.start();


            }
        });


        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(animatr.isStarted()){
                    animatr.cancel();
                }
            }
        });


    }
}




XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn_start_tween"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="补间动画开始" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="动画开始" />
    <Button
        android:id="@+id/btn_stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="动画结束" />


</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值