Android——动画

帧动画

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

frame.xml

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

xml 复制代码
<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>

执行动画

java 复制代码
    @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
xml 复制代码
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>
  • rotate.xml
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
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
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>

启动动画

java 复制代码
        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
java 复制代码
        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

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

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

监听

java 复制代码
        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) {
                //重复
            }
        });

监听适配器

java 复制代码
        // 监听适配方式,只监听某一个都可以
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });

案例代码

相关推荐
hqk9 分钟前
鸿蒙ArkUI:状态管理、应用结构、路由全解析
android·前端·harmonyos
消失的旧时光-194343 分钟前
从 C 链表到 Android Looper:MessageQueue 的底层原理一条线讲透
android·数据结构·链表
方白羽44 分钟前
Android 中Flags从源码到实践
android·app·客户端
深蓝电商API44 分钟前
从数据采集到商业变现:网络爬虫技术的实战与边界
android·爬虫
恋猫de小郭3 小时前
再次紧急修复,Flutter 针对 WebView 无法点击问题增加新的快速修复
android·前端·flutter
李慕婉学姐3 小时前
【开题答辩过程】以《基于Android的健康助手APP的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
android·java·mysql
似霰5 小时前
传统 Hal 开发笔记6----App 访问硬件服务
android·framework·hal
爱装代码的小瓶子6 小时前
【c++知识铺子】封装map和set(详细版)
android·java·c++
私人珍藏库6 小时前
AutoGLM无需豆包手机,让AI自动帮你点外卖-刷视频
android·ai·智能手机·工具·软件·辅助·autoglm