Android 属性动画使用总结

本文介绍如何使用ObjectAnimator实现Android中的四种常见动画效果:淡入淡出、旋转、平移及缩放,并通过AnimatorSet进行组合动画的创建。文章提供了一个完整的示例代码,包括Activity布局文件及动画设置。

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

把四大常用效果加动画组合的代码整理了一下

下面是一个演示图:



简单强调一下:

获取ObjectAnimator

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  

第三个参数是 ...无限多的形式 因此可以根据自己的需求传递参数

最后上一下代码

Acitivity中点击按钮开始动画:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button start_btn;
    private TextView alpha_tv;
    private TextView rotation_tv;
    private TextView translationX_tv;
    private TextView scaleY_tv;
    private TextView animset_tv;

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

    private void initView() {
        start_btn = (Button) findViewById(R.id.start_btn);
        alpha_tv = (TextView) findViewById(R.id.alpha_tv);
        rotation_tv = (TextView) findViewById(R.id.rotation_tv);
        translationX_tv = (TextView) findViewById(R.id.translationX_tv);
        scaleY_tv = (TextView) findViewById(R.id.scaleY_tv);
        animset_tv = (TextView) findViewById(R.id.animset_tv);

        start_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_btn:
                                //淡入淡出属性动画
                                ObjectAnimator alpha = ObjectAnimator.ofFloat(alpha_tv, "alpha", 1f, 0.1f, 1f);
                                alpha.setDuration(5000);
                                alpha.start();

                                //旋转
                                ObjectAnimator rotation = ObjectAnimator.ofFloat(rotation_tv, "rotation", 0f, 360f);
                                rotation.setDuration(5000);
                                rotation.start();

                                //平移
                                float curTranslationX = translationX_tv.getTranslationX();
                                ObjectAnimator translationX = ObjectAnimator.ofFloat(translationX_tv, "translationX", curTranslationX, -500f, curTranslationX);
                                translationX.setDuration(5000);
                                translationX.start();

                                //缩放
                                ObjectAnimator scaleY = ObjectAnimator.ofFloat(scaleY_tv, "scaleY", 1f, 3f, 1f);
                                scaleY.setDuration(3000);
                                scaleY.start();

                                //组合
                                ObjectAnimator moveIn = ObjectAnimator.ofFloat(animset_tv, "translationX", -500f, 0f);
                                ObjectAnimator rotate = ObjectAnimator.ofFloat(animset_tv, "rotation", 0f, 360f);
                                ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(animset_tv, "alpha", 1f, 0f, 1f);
                                ObjectAnimator end = ObjectAnimator.ofFloat(animset_tv, "scaleY", 1f, 3f, 1f);
                                AnimatorSet animSet = new AnimatorSet();
                                //after(Animator anim)   将现有动画插入到传入的动画之后执行
                                //after(long delay)   将现有动画延迟指定毫秒后执行
                                //before(Animator anim)   将现有动画插入到传入的动画之前执行
                                //with(Animator anim)   将现有动画和传入的动画同时执行
                                animSet.play(rotate).with(fadeInOut).after(moveIn).before(end);
                                animSet.setDuration(5000);
                                animSet.start();


                break;
        }
    }
}
Activity的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.lyl.objectanimator.MainActivity">

    <Button
        android:id="@+id/start_btn"
        android:text="开始动画"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/alpha_tv"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个字符串,淡入淡出" />

    <TextView
        android:id="@+id/rotation_tv"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个字符串,旋转" />

    <TextView
        android:id="@+id/translationX_tv"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个字符串,平移" />

    <TextView
        android:id="@+id/scaleY_tv"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个字符串,缩放" />

    <TextView
        android:id="@+id/animset_tv"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个字符串,组合" />

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值