Android动画介绍——属性动画(2)

本文介绍了如何在Android中使用PropertyValuesHolder实现多种动画效果的叠加,以及如何使用AnimatorSet来控制动画顺序和执行。着重讲解了透明度、缩放、平移和旋转等属性的动画设置。

在Android动画介绍——属性动画(1)中我们介绍了单一效果的属性动画的实现,在这里我们继续介绍多种效果叠加的属性动画实现。

多种效果叠加的属性动画实现方法有两种。

1、利用PropertyValuesHolder:可以将多种动画效果同时执行

2、利用AnimatorSet(组合动画):AnimatorSet 不仅可以同时执行多个动画效果,也可以让多个动画效果按照一定的顺序执行。

PropertyValuesHolder这个类的意义就是,它其中保存了动画过程中所需要操作的属性和对应的值。其中属性包括如下内容:

alpha(透明度动画)

scaleX/scaleY(缩放动画)

translationX/translationY(平移动画)

rotation(旋转动画)

PropertyValuesHolder的常用方法如下:

方法描述

static PropertValuesHolder ofInt(String propertyName, int... values)

设置整型属性

static PropertValuesHolder ofFloat(String propertyName, float... values)

设置浮点型属性

static PropertValuesHolder ofObject(String propertyName, TypeEvaluator evaluator, Object... values)

设置Object型属性

AnimatorSet的常用方法如下:

方法描述

static ObjectAnimator ofInt(Object target, String propertyName, int... values)

设置整型属性

static ObjectAnimator offFloat(Object target, String propertyName, float... values)

设置浮点型属性

static ObjectAnimator ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values)

设置Object型属性

static ObjectAnimator ofArgb(Object target, String propertyName, int... values)

设置RGB型属性

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:ignore="MissingConstraints"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv_animator_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈,我是AnimatorSet"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.439"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.188" />


    <TextView
        android:id="@+id/tv_property_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈,我是PropertyValuesHolder"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.737" />


</androidx.constraintlayout.widget.ConstraintLayout>

Activity文件:

package com.example.animatordomo;


import androidx.appcompat.app.AppCompatActivity;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


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


        TextView tv_property_values = findViewById(R.id.tv_property_values);
        PropertyValuesHolder alphaProper = PropertyValuesHolder.ofFloat("alpha",0.5f,1f);
        PropertyValuesHolder scaleXProper = PropertyValuesHolder.ofFloat("scaleX",0f,1f);
        PropertyValuesHolder scaleYProper = PropertyValuesHolder.ofFloat("scaleY",0f,1f);
        PropertyValuesHolder translationXProper = PropertyValuesHolder.ofFloat("translationX",-100,100);
        PropertyValuesHolder translationYProper = PropertyValuesHolder.ofFloat("translationY",-100,100);
        PropertyValuesHolder rotationProper = PropertyValuesHolder.ofFloat("rotation",0,360);
        ValueAnimator animator = ObjectAnimator.ofPropertyValuesHolder(tv_property_values,alphaProper,
                scaleXProper,scaleYProper,translationXProper,translationYProper,rotationProper);
        animator.setDuration(5000).start();


        TextView tv_animator_set = findViewById(R.id.tv_animator_set);
        ObjectAnimator rotate = ObjectAnimator.ofFloat(tv_animator_set,"rotation",0f,360f);
        ObjectAnimator translationX = ObjectAnimator.ofFloat(tv_animator_set,"translationX",-100,100f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(tv_animator_set,"translationY",-100,100f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(tv_animator_set,"scaleX",0,1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(tv_animator_set,"scaleY",0,1f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(tv_animator_set,"alpha",0f,1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(rotate)
                .with(alpha)
                .with(scaleY)
                .after(scaleX)
                .after(500)
                .before(translationX)
                .before(translationY);
        animatorSet.setDuration(2000).start();
    }
}

Domo代码:

Android动画介绍-属性动画(2)资源-CSDN文库 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值