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);
            }
        });

案例代码

相关推荐
liang_jy15 分钟前
Android View Tag
android
liang_jy37 分钟前
Android 架构中的统一分发与策略路由
android·架构
scan7243 小时前
长期记忆存储在数据库里
android
xingpanvip3 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
儿歌八万首5 小时前
Jetpack Compose 实战:实现一个动态平滑折线图
android·折线图·compose
李艺为10 小时前
Fake Device Test作假屏幕分辨率分析
android·java
zh_xuan10 小时前
github远程library仓库升级
android·github
峥嵘life10 小时前
Android蓝牙停用绝对音量原理
android
czlczl2002092511 小时前
IN和BETWEEN在索引效能的区别
android·adb
Volunteer Technology11 小时前
ES高级搜索功能
android·大数据·elasticsearch