Android 动画之属性动画

本文介绍了一个简单的Android属性动画实现案例,通过ValueAnimator改变TextView的颜色从透明渐变为红色。

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

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tvTitle;
    Button btnStart,btnStop;
    ValueAnimator animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获得组件
        tvTitle=(TextView) findViewById(R.id.tv_title_main);
        btnStart=(Button) findViewById(R.id.start_anim_main);
        btnStop=(Button) findViewById(R.id.stop_anim_main);
        //监听事件
        btnStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                //一。构造并返回0-255之间的一个变化的ValueAnimator
                animation=ValueAnimator.ofInt(0,255);
                //二。设置动画 的时常
                animation.setDuration(3000);
                //三。Repeat重复,设置重复的次数
                animation.setRepeatCount(ValueAnimator.INFINITE);
                //四。设置重复的模板(当这个动画到结尾时,应该如何做)
                animation.setRepeatMode(ValueAnimator.RESTART);
                //设置动画更新的监听事件
                animation.addUpdateListener(new AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //五。获取当前时刻animation的值
                        Log.i("Tag", ""+animation.getAnimatedValue());
                        int red=Color.rgb((Integer) animation.getAnimatedValue(), 0, 0);
                        //六 。把获得的值设置为TextView的颜色值
                        tvTitle.setTextColor(red);
                    }
                });
                //开始动画
                animation.start();
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(animation.isRunning()){//如果animation动画正在运行
                    animation.end();//结束
                }
            }
        });

    }
}
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_title_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="35sp" />

    <Button
        android:id="@+id/start_anim_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title_main"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="开始属性动画" />

    <Button
        android:id="@+id/stop_anim_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/start_anim_main"
        android:layout_centerVertical="true"
        android:text="停止属性动画" />
</RelativeLayout>

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值