分析数字自动翻滚的动画---加深对属性动画的理解

package com.garea.plugin.measurer;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

public class CountView extends TextView {
    int duration = 2000;
    float number;

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

    public void showNumberWithAnimation(float number) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "number",
                0, number);

        objectAnimator.setDuration(duration);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
    }

    public CountView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CountView(Context context) {
        super(context);
    }

    public void setNumber(float number) {
        this.number = number;
        DecimalFormat fnum = new DecimalFormat("##0");
        String str = fnum.format(number);
        setText(str+"");
    }

}


想要的效果 要个textView 设置数字100,让它减速的从0到100,而不是直接写上100,这就需要动画

Object动画

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "number", 0, 100);这行代码

的真实意思是 随着时间的变化依次产生0-100之间的数,然后去调用this的 setNumber(float number)方法。

它使用的是反射的机制  所以需要传一个 对象给他  还需要传一个方法的名称给他   这里的“number”就是方法名称 

那他怎么会调用setNumber 这是他底层的给他加上的“set”+"Number"

不仅如此  View的 缩放、旋转、平移、透明度等等都是这样的

view.setScaleX(scaleX);

view.setAlpha(alpha);.

.

.

.

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0, 100);

所以只要View有的setXXX的方法缩放、旋转、平移、透明度都可以这么调用。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值