Android:属性动画详解

package com.example.a51044.myapplicationli;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button Start_Anim;
    private Button Object_Anim;
    private Button XML_Anim;

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

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.Start_Anim:
                valueAnimStart();
                Toast.makeText(this,"李小====强",Toast.LENGTH_SHORT).show();
                break;
            case R.id.Object_Anim:
                ObjectAnimator translation = ObjectAnimator.ofFloat(Start_Anim, "translationX", 0, 300, 500);
                // 平移动画
                ObjectAnimator rotate = ObjectAnimator.ofFloat(Start_Anim, "rotation", 0f, 360f);
                // 旋转动画
                ObjectAnimator alpha = ObjectAnimator.ofFloat(Start_Anim, "alpha", 1f, 0f, 1f);
                // 透明度动画
                // 步骤2:创建组合动画的对象
                AnimatorSet animSet = new AnimatorSet();
			
			//动画的先后顺序
                animSet.play(translation).with(rotate).before(alpha);
                animSet.setDuration(5000);
                // 步骤4:启动动画
                animSet.start();
                break;
            case R.id.XML_Anim:
            //找到XML文件
                AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, 		R.animator.set_animotor);
                //参数是控件
                set.setTarget(Object_Anim);
                set.start();
                break;

        }
    }

    private void valueAnimStart() {
    //初始化控件,第一个参数是要改变的控件的宽度,第二个参数是控件宽度改变为500
        ValueAnimator valueAnimator = ValueAnimator.ofInt(Start_Anim.getLayoutParams().width, 500);
        valueAnimator.setDuration(3000);
        //动画的延迟
        valueAnimator.setStartDelay(500);
        //动画的改变监听
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //移动的单位  得值是跟你赋值对象的时候那个方法要一致(执行完后的控件宽度)
                int currentValue = (Integer) animation.getAnimatedValue();
                Start_Anim.getLayoutParams().width = currentValue;
                Log.e("currentValue", currentValue + "单位");
                Start_Anim.requestLayout();
            }
        });
        valueAnimator.start();

    }

    private void initView() {
        Start_Anim = (Button) findViewById(R.id.Start_Anim);
        Object_Anim = (Button) findViewById(R.id.Object_Anim);
        XML_Anim = (Button) findViewById(R.id.XML_Anim);

        Start_Anim.setOnClickListener(this);
        Object_Anim.setOnClickListener(this);
        XML_Anim.setOnClickListener(this);
    }
}

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

    <Button
        android:id="@+id/Start_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="ValueAnim开始吧" />

    <Button
        android:id="@+id/Object_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="ObjectAnimator开始吧" />

    <Button
        android:id="@+id/XML_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="XMl开始吧" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <set android:ordering="together">
        <!-- 下面的动画同时进行 -->
        <objectAnimator
            android:duration="2000"
            android:propertyName="translationX"
            android:valueFrom="0"
            android:valueTo="300"
            android:valueType="floatType"></objectAnimator>

        <objectAnimator
            android:duration="3000"
            android:propertyName="rotation"
            android:valueFrom="0"
            android:valueTo="360"
            android:valueType="floatType"></objectAnimator>
    </set>

    <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>
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

猜你喜欢

转载自blog.csdn.net/weixin_43747497/article/details/85005217