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

案例代码

相关推荐
花花鱼7 小时前
android studio 设置让开发更加的方便,比如可以查看变量的类型,参数的名称等等
android·ide·android studio
alexhilton9 小时前
为什么你的App总是忘记所有事情
android·kotlin·android jetpack
AirDroid_cn12 小时前
OPPO手机怎样被其他手机远程控制?两台OPPO手机如何相互远程控制?
android·windows·ios·智能手机·iphone·远程工作·远程控制
尊治12 小时前
手机电工仿真软件更新了
android
xiangzhihong815 小时前
使用Universal Links与Android App Links实现网页无缝跳转至应用
android·ios
车载应用猿16 小时前
基于Android14的CarService 启动流程分析
android
没有了遇见16 小时前
Android 渐变色实现总结
android
雨白19 小时前
Jetpack系列(四):精通WorkManager,让后台任务不再失控
android·android jetpack
mmoyula21 小时前
【RK3568 驱动开发:实现一个最基础的网络设备】
android·linux·驱动开发
sam.li21 小时前
WebView安全实现(一)
android·安全·webview