属性动画合集

属性动画代码

package com.wj.learnmvi.anim.animator

import android.animation.*
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AnticipateOvershootInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams
import com.wj.learnmvi.R
import kotlinx.android.synthetic.main.activity_animator.*

/**
 *  Author:WJ
 *  Date:2023/10/8 9:48
 *  Describe:
 */
class AnimatorActivity : AppCompatActivity(), View.OnClickListener {
    private val measuredHeight by lazy { tvBtn.measuredHeight.toFloat() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animator)
        tvTranslation.setOnClickListener(this)
        tvAlpha.setOnClickListener(this)
        tvScale.setOnClickListener(this)
        tvViewProperty.setOnClickListener(this)
        tvBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvTranslation -> {// 平移动画
                startTranslationAnimator()
            }
            R.id.tvAlpha -> {// 透明度动画
                startAlphaAnimator()
            }
            R.id.tvScale -> {// 缩放动画
                startScaleAnimator()
            }
            R.id.tvViewProperty -> {// ViewProperty
                startViewPropertyAnimator()
            }
        }
    }

    /**
     * ViewPropertyAnimator
     */
    private fun startViewPropertyAnimator() {
//        tvBtn.animate().x(100f).y(100f).apply {
//            duration =1000
//            interpolator = DecelerateInterpolator()
//        }
//        tvBtn.animate().translationX(100f).translationY(100f).apply {
//            duration =1000
//            interpolator = DecelerateInterpolator()
//        }
//        tvBtn.animate().alpha(0.3f).apply {
//            duration =1000
//            interpolator = DecelerateInterpolator()
//        }
        tvBtn.animate().rotation(90f).apply {
            duration =1000
            interpolator = DecelerateInterpolator()
        }
    }

    /**
     * 缩放动画
     */
    private fun startScaleAnimator() {
//        ObjectAnimator.ofFloat(tvBtn, "scaleY", 1f,0.5f).apply {
//            duration = 1000
//            start()
//        }

        ValueAnimator.ofFloat(measuredHeight, 0.5f* measuredHeight).apply {
            //时间
            duration = 1000
            interpolator = AnticipateOvershootInterpolator()
            //开始执行
            start()
            //监听值的变化 如果你打印 updatedAnimation.animatedValue 那么控制台会输出0-100
            addUpdateListener { updatedAnimation ->
                //修改组件的位置
//                tvBtn.translationX = updatedAnimation.animatedValue as Float
//                tvBtn.scaleY = updatedAnimation.animatedValue as Float
                val layoutParams = (tvBtn.layoutParams as LayoutParams).apply {
                    height = (updatedAnimation.animatedValue as Float).toInt()
                    Log.i("wj==>", height.toString())
                }
                tvBtn.layoutParams = layoutParams
            }
            //监听开始结束
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationStart(animation: Animator?) {
                    super.onAnimationStart(animation)
                    Log.i("动画开始", "")
                }

                override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)
                    Log.i("动画结束", "")
                }
            })
        }
    }

    /**
     * 透明度动画
     */
    private fun startAlphaAnimator() {
        ObjectAnimator.ofFloat(tvBtn, "alpha", 0f, 1f).apply {
            duration = 1000
            start()
        }
    }

    /**
     * 开始平移动画
     */
    private fun startTranslationAnimator() {
        val animator1 = ObjectAnimator.ofFloat(tvBtn, "translationX", 0f, 100f).apply {
            duration = 1000
//            start()
        }
        val animator2 = ObjectAnimator.ofFloat(tvBtn, "translationY", 0f, 100f).apply {
            duration = 1000
//            start()
        }
        AnimatorSet().apply {
            // 先执行1后执行2
//            play(animator1).before(animator2)
            // 先执行2后执行1
//            play(animator1).after(animator2)
            // 一起执行
            playTogether(animator1, animator2)
            start()
        }
    }
}

整体布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:stateListAnimator="@drawable/animate_scale"
        android:text="执行按钮"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvTranslation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="平移动画"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tvAlpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="透明度动画"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvTranslation" />

    <TextView
        android:id="@+id/tvScale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="缩放动画"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvAlpha" />

    <TextView
        android:id="@+id/tvViewProperty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="view动画"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvScale" />

</androidx.constraintlayout.widget.ConstraintLayout>

页面布局图

在这里插入图片描述

使用 StateListAnimator 为视图状态更改添加动画效果

需要准备 res/drawable/animate_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- the pressed state; increase x and y size to 150% -->
    <item android:state_pressed="true">
        <set>
            <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleX" android:valueTo="1.5" android:valueType="floatType" />
            <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleY" android:valueTo="1.5" android:valueType="floatType" />
        </set>
    </item>
    <!-- the default, non-pressed state; set x and y size to 100% -->
    <item android:state_pressed="false">
        <set>
            <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleX" android:valueTo="1" android:valueType="floatType" />
            <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleY" android:valueTo="1" android:valueType="floatType" />
        </set>
    </item>
</selector>


使用动画 时需要注意view要有点击事件 ,在XML 得View节点下添加 android:stateListAnimator=“@drawable/animate_scale”

在这里插入图片描述

动画插值器

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值