Android开发分数滚动动画效果

79 阅读1分钟
Android开发分数滚动动画效果

有时候分数或者金额的变动,会用滚动一下动画,来表现数字的变化。

一、思路:

用MotionLayout动画做的

二、效果图:

在这里插入图片描述在这里插入图片描述

三、关键代码:
<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/ml_main"
    app:layoutDescription="@xml/activity_main_scene2">

        <androidx.constraintlayout.motion.widget.MotionLayout
            android:id="@+id/cl_profit_num"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            app:layoutDescription="@xml/activity_profit_xml_cl_profit_scene"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/tv_profit_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:includeFontPadding="false"
                android:textColor="#FFD596"
                android:textSize="40sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:text="0" />
        </androidx.constraintlayout.motion.widget.MotionLayout>



<!--    <TextView-->
<!--        android:id="@+id/tv_profit_num"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:includeFontPadding="false"-->
<!--        android:textColor="#FFD596"-->
<!--        android:textSize="40sp"-->
<!--        android:textStyle="bold"-->
<!--        app:layout_constraintBottom_toBottomOf="parent"-->
<!--        app:layout_constraintLeft_toLeftOf="parent"-->
<!--        app:layout_constraintRight_toRightOf="parent"-->
<!--        app:layout_constraintTop_toTopOf="parent"-->
<!--        android:text="0" />-->

    <ImageView
        android:id="@+id/ml_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/head3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.motion.widget.MotionLayout>

kotlin代码:

class MainActivity : AppCompatActivity() {

    private lateinit var clProfitNum: MotionLayout
    private lateinit var tvProfitNum:TextView

    var newlyAddedScore = 0f  // 新增数
    var scoreNum = 0f  // 积分数
    var increaseNum = 0   // 每轮动画积分增减率

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        clProfitNum = findViewById(R.id.cl_profit_num)
        tvProfitNum = findViewById(R.id.tv_profit_num)

        newlyAddedScore = 1416f
        scoreNum = 4519.68f
        tvProfitNum.text = "$scoreNum"
四、项目demo源码结构图:

在这里插入图片描述

有问题或者需要完整源码demo的可以看简介联系我,也可以私信我,我每天都看私信的