ValueAnimator的使用

本文介绍如何使用 Android 的 ValueAnimator 实现字符变化动画及视图尺寸动态调整动画。通过 XML 和 Java 代码两种方式创建 ValueAnimator,并展示了如何监听动画更新以实时改变 TextView 的显示内容及大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ValueAnimator的使用

activity_main.xml文件中

<RelativeLayout 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"
tools:context="www.weshared.anim.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:background="@android:color/holo_blue_bright"
        android:padding="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:text="执行动画"
        android:layout_height="wrap_content" />
</RelativeLayout>

在res/animator目录下的value_animator.xml文件中(ValueAnimator的xml动画)

<?xml version="1.0" encoding="utf-8"?>
<animator  xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:interpolator="@android:interpolator/bounce"
    android:valueFrom="0px"
    android:valueTo="720px"
    android:valueType="intType" />

在MainActicity中

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ValueAnimator.AnimatorUpdateListener {

    private TextView mTextView;
    private Button mButton;
    private int widthPixels;

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

        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);
        mButton.setOnClickListener(this);

        //获取屏幕的宽度
        widthPixels = getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public void onClick(View v) {
        //initAnim();
        initOfObject();
        //initXmlAnim();
    }

    private void initOfObject() {
        ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('Z'));
        animator.setDuration(2000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
            char animatedValue = (char) animation.getAnimatedValue();
            mTextView.setText(String.valueOf(animatedValue));
            mTextView.requestLayout();
            }
        });
    }


    private void initAnim() {
        ValueAnimator animator = ValueAnimator.ofInt(0, widthPixels);
        animator.setDuration(2000);
        animator.setInterpolator(new BounceInterpolator());
        animator.start();
        animator.addUpdateListener(this);
     }
    private void initXmlAnim() {
        ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.value_animator);
        animator.start();
        animator.addUpdateListener(this);
    }


    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int animatedValue = (int) animation.getAnimatedValue();
        mTextView.setWidth(animatedValue);
        mTextView.setHeight((int) (animatedValue * 0.6));
        mButton.setHeight((int) (animatedValue * 0.6));
        mTextView.requestLayout();
        mButton.requestLayout();
     }

    public class CharEvaluator implements TypeEvaluator<Character> {
        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {
        int startInt = (int) startValue;
        int endInt = (int) endValue;
        int curInt = (int) (startInt + fraction * (endInt - startInt));
        char result = (char) curInt;
        return result;
       }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值