美文网首页Android
Android高仿(余额宝)数字动画效果

Android高仿(余额宝)数字动画效果

作者: 遇见编程 | 来源:发表于2020-11-26 10:57 被阅读0次

直接上工具类代码:

import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * 高仿余额宝数字动画
 */
public class CountNumberView extends androidx.appcompat.widget.AppCompatTextView {
    //动画时长
    private int duration = 1500;
    //显示数字
    private float number;
    //显示表达式
    private String regex;

    //显示表示式
    public static final String INTREGEX = "%1$01.0f";//不保留小数,整数
    public static final String FLOATREGEX = "%1$01.2f";//保留2位小数

    public CountNumberView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 显示带有动画效果的数字
     * @param number
     * @param regex
     */
    public void showNumberWithAnimation(float number, String regex) {
        if (TextUtils.isEmpty(regex)) {
            //默认为整数
            this.regex = INTREGEX;
        } else {
            this.regex = regex;
        }
        //修改number属性,会调用setNumber方法
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "number", 0, number);
        objectAnimator.setDuration(duration);
        //加速器,从慢到快到再到慢
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
    }

    /**
     * 获取当前数字
     * @return
     */
    public float getNumber() {
        return number;
    }

    /**
     * 根据正则表达式,显示对应数字样式
     * @param number
     */
    public void setNumber(float number) {
        this.number = number;
        setText(String.format(regex, number));
    }
}

xml中使用

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

    <com.smt.saveviewtocream.utils.CountNumberView
        android:id="@+id/mTvCountNum1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        tools:text="11111111" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/white" />

    <com.smt.saveviewtocream.utils.CountNumberView
        android:id="@+id/mTvCountNum2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        tools:text="11111111" />
</LinearLayout>

使用(kotlin):

class CountNumActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_count_num)

        mTvCountNum1.showNumberWithAnimation(3201.23f, CountNumberView.FLOATREGEX)
        mTvCountNum2.showNumberWithAnimation(65535f, CountNumberView.INTREGEX)
    }
}

常规类型、字符类型和数值类型的格式说明符的语法如下:
%[argument_index][flags][width][.precision]conversion 可选的 argument_index 是一个十进制整数,用于表明参数在参数列表中的位置。第一个参数由 "1" 引用,第二个参数由 "2$" 引用,依此类推。
可选 flags 是修改输出格式的字符集。有效标志集取决于转换类型。
可选 width 是一个非负十进制整数,表明要向输出中写入的最少字符数。
可选 precision 是一个非负十进制整数,通常用来限制字符数。特定行为取决于转换类型。

相关文章

网友评论

    本文标题:Android高仿(余额宝)数字动画效果

    本文链接:https://www.haomeiwen.com/subject/nowiiktx.html