Android——动画

发布于:2025-04-19 ⋅ 阅读:(15) ⋅ 点赞:(0)

帧动画

帧动画就是很多张图片,一帧一帧的播放,形成的一个动画效果。

frame.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/diqiu"
        android:duration="120" />
    <item
        android:drawable="@drawable/huoxing"
        android:duration="120" />
    <item
        android:drawable="@drawable/tuxing"
        android:duration="120" />
    <item
        android:drawable="@drawable/shuixing"
        android:duration="120" />
</animation-list>

将其设置为background

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/frame"
    tools:context=".FrameAnimationActivity">

</RelativeLayout>

执行动画

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

        RelativeLayout relativeLayout = findViewById(R.id.main);
        AnimationDrawable anim = (AnimationDrawable) relativeLayout.getBackground();
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    anim.start();
                    flag = false;
                } else {
                    anim.stop();
                    flag = true;
                }
            }
        });
    }

案例代码

补间动画

补间动画包括alpha(透明度)rotate(旋转)scale(缩放)translate(平移)

  • alpha.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>
  • rotate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</set>
  • scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />
</set>
  • translate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="400"
        android:toYDelta="400" />
</set>

启动动画

        ImageView iv = findViewById(R.id.iv);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Animation animation = AnimationUtils.loadAnimation(AlphaActivity.this, R.anim.alpha);
//                Animation animation = AnimationUtils.loadAnimation(AlphaActivity.this, R.anim.rotate);
//                Animation animation = AnimationUtils.loadAnimation(AlphaActivity.this, R.anim.scale);
                Animation animation = AnimationUtils.loadAnimation(AlphaActivity.this, R.anim.translate);
                iv.startAnimation(animation);
            }
        });

案例代码

属性动画

声明一个属性值,通过属性值的改变,达到动画的效果。

  • ValueAnimator
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("leo", "value:" + value);
            }
        });
        valueAnimator.start();
  • ObjectAnimator

可以直接指定要控制的对象

        TextView tv = findViewById(R.id.tv);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);
        objectAnimator.setDuration(4000);
        objectAnimator.start();

监听

        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(@NonNull Animator animation) {
                //开始
            }

            @Override
            public void onAnimationEnd(@NonNull Animator animation) {
                //结束
            }

            @Override
            public void onAnimationCancel(@NonNull Animator animation) {
                //取消
            }

            @Override
            public void onAnimationRepeat(@NonNull Animator animation) {
                //重复
            }
        });

案例代码


网站公告

今日签到

点亮在社区的每一天
去签到