Android 动画总结(补间动画,属性动画)

本文介绍了如何在开发中使用补间动画和属性动画提升用户体验,包括透明、旋转、位移及缩放等基本动画类型,并通过一个具体的菜单动画实例展示了属性动画在用户交互中的应用。

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

在开发中,为了提升用户体验,我们会添加很多动画效果,以前一直在使用,但是从来没有总结一下,最近工作轻松,所以总结一下这个方面:

这里主要是补间动画(Animation),属性动画(Animator)两类。

  • 补间动画(Animation)

  • 属性动画(Animator)

  • 动画实例


补间动画(Animation)

透明动画
    AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
    alphaAnimation.setDuration(2000);
    iv.startAnimation(alphaAnimation);
旋转动画

注意: RotateAnimation.RELATIVE_TO_SELF, 0.5f相对控件本身中点。

  RotateAnimation rotateAnimation = new RotateAnimation(0, 360,            
     RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
     RotateAnimation.RELATIVE_TO_SELF, 0.5f);
 rotateAnimation.setDuration(2000);
 iv.startAnimation(rotateAnimation);
位移动画
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 300);
translateAnimation.setDuration(2000);
iv.startAnimation(translateAnimation);
缩放动画
  ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2);
  scaleAnimation.setDuration(2000);
  iv.startAnimation(scaleAnimation);
动画合集
 AnimationSet set = new AnimationSet(true);
                set.setDuration(2000);

                AlphaAnimation alphaAnimation2 = new AlphaAnimation(0, 1);
                alphaAnimation2.setDuration(2000);

                RotateAnimation rotateAnimation2 = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation2.setDuration(2000);

                TranslateAnimation translateAnimation2 = new TranslateAnimation(0, 200, 0, 300);
                translateAnimation2.setDuration(2000);

                ScaleAnimation scaleAnimation2 = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation2.setDuration(2000);

                set.addAnimation(alphaAnimation2);
                set.addAnimation(rotateAnimation2);
                set.addAnimation(scaleAnimation2);
                set.addAnimation(translateAnimation2);

                iv.startAnimation(set);

属性动画(Animator)

代码实现

注意:ObjectAnimator.ofFloat(iv, “translationX”, 300),初始化方法与补间动画不同,第一参数为实现动画的控件,第一个参数为动画类型,第三个为移动的数据,有时候这里是两个数据,一个开始,一个结束

位移动画

 ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", 300);
 animator.setDuration(2000);
 animator.start();

动画合集

 ObjectAnimator tran = ObjectAnimator.ofFloat(iv, "translationX", 300);
 tran.setDuration(2000);

 ObjectAnimator scale = ObjectAnimator.ofFloat(iv, "scaleX", 0.1f, 1f);

 ObjectAnimator rotate = ObjectAnimator.ofFloat(iv, "rotation", 0, 360);
 rotate.setDuration(2000);

 AnimatorSet set = new AnimatorSet();
 set.setDuration(2000);
 set.playTogether(tran, scale, rotate);
 set.start();
xml实现

这里粘贴guoling大神的博客,讲的很详细。
http://blog.youkuaiyun.com/guolin_blog/article/details/43536355

动画实例--灵动菜单

最后通过使用例子来加强对动画的理解。

对于这个动画,我们怎么实现呢?它具有用户交互性,肯定是不能使用补间动画。其次只要针对不同的按钮设置不同的动画,设置相应的差值器来实现打开,合拢效果。
打开动画如下:

    //展开动画
    private void startAnim() {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(btn_menu, "alpha", 1f, 0.5f);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(buttons[0], "translationY", 300f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(buttons[1], "translationX", 300f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(buttons[2], "translationY", -300f);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(buttons[3], "translationX", -300f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new BounceInterpolator());
        set.playTogether(animator0, animator1, animator2, animator3, animator4);
        set.start();
        isOpen = true;
    }

xml布局如下;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



    <Button
        android:id="@+id/btn_view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="onClick"
        android:text="button1" />

    <Button
        android:id="@+id/btn_view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="onClick"
        android:text="button2" />

    <Button
        android:id="@+id/btn_view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="onClick"
        android:text="button3" />

    <Button
        android:id="@+id/btn_view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="onClick"
        android:text="button4" />
    <Button
        android:id="@+id/btn_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="onClick"
        android:text="菜单" />
</RelativeLayout>

整体代码如下:

/**
 * 主要是实现点击后弹出子菜单的动画
 * Created by kenway on 17/3/16 14:29
 * Email : xiaokai090704@126.com
 */

public class MenuAnimActivity extends AppCompatActivity {
    private Button btn_menu;
    private Button[] buttons = new Button[4];
    //判断菜单是否打开
    private boolean isOpen;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menuanim);
        btn_menu = (Button) findViewById(R.id.btn_menu);
        buttons[0] = (Button) findViewById(R.id.btn_view1);
        buttons[1] = (Button) findViewById(R.id.btn_view2);
        buttons[2] = (Button) findViewById(R.id.btn_view3);
        buttons[3] = (Button) findViewById(R.id.btn_view4);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_menu:
                if (!isOpen) {
                    startAnim();
                } else {
                    closeAnim();
                }

                break;
            case R.id.btn_view1:
//
                break;
            case R.id.btn_view2:

                break;
            case R.id.btn_view3:

                break;
            case R.id.btn_view4:

                break;

        }
    }

    //展开动画
    private void startAnim() {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(btn_menu, "alpha", 1f, 0.5f);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(buttons[0], "translationY", 300f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(buttons[1], "translationX", 300f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(buttons[2], "translationY", -300f);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(buttons[3], "translationX", -300f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new BounceInterpolator());
        set.playTogether(animator0, animator1, animator2, animator3, animator4);
        set.start();
        isOpen = true;
    }

    //关闭菜单动画
    private void closeAnim() {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(btn_menu, "alpha", 0.5f, 1f);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(buttons[0], "translationY", 0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(buttons[1], "translationX", 0);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(buttons[2], "translationY", 0);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(buttons[3], "translationX", 0);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new BounceInterpolator());
        set.playTogether(animator0, animator1, animator2, animator3, animator4);
        set.start();
        isOpen = false;
    }
}

其实这些代码都特别简单,通过这些代码,你是否发现一些感觉起来不好实现的功能,使用动画便可以很容易的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值