Android Studio 实现动画 -- 补间动画

效果图

在这里插入图片描述

动画

  • 点击处理
 //平移
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation translation= AnimationUtils.loadAnimation(getApplication(),R.anim.translate);
                fk.startAnimation(translation);
            }
        });
        //缩放
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation scale= AnimationUtils.loadAnimation(getApplication(),R.anim.scale);
                fk.startAnimation(scale);
            }
        });
        //旋转
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation rotate= AnimationUtils.loadAnimation(getApplication(),R.anim.rotate);
                fk.startAnimation(rotate);
            }
        });
        //透明度
        but3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation alpha= AnimationUtils.loadAnimation(getApplication(),R.anim.alpha);
                fk.startAnimation(alpha);
            }
        });
        //上下循环
       but4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation alpha= AnimationUtils.loadAnimation(getApplication(),R.anim.shangxia);
                fk.startAnimation(alpha);
            }
        });
        //缩放循环
        but5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation alpha= AnimationUtils.loadAnimation(getApplication(),R.anim.scale2);
                fk.startAnimation(alpha);
            }
        });
        //旋转动画
        but6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                fk.setImageResource(R.mipmap.dong);
                Animation rotate= AnimationUtils.loadAnimation(getApplication(),R.anim.rotate2);
                //让旋转动画一直转,不停顿的重点(实际上是添加动画插值器)
                rotate.setInterpolator(new LinearInterpolator());//加上这一句,解决旋转停顿问题
                fk.startAnimation(rotate);
            }
        });
        //停止动画
        but7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fk.clearAnimation();
            }
        });

注意:让旋转动画一直转,不停顿的重点(实际上是添加动画插值器)

rotate.setInterpolator(new LinearInterpolator());//加上这一句,解决旋转停顿问题
  • anim
    在这里插入图片描述
  • translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--平移-->
    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="100"
        android:toYDelta="0.0"
        android:repeatMode="reverse"
        android:repeatCount="1"
        android:duration="500"
        />
    <!--
        android:repeatCount="infinite" 循环
        android:duration="500" 期间/速度
        android:fromXDelta="0.0" //开始 x 位置
        android:fromYDelta="0.0" //开始 y 位置
        android:toXDelta="100" //到达 x 位置
        android:toYDelta="0.0" //到达 y 位置
    -->
</set>
  • scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--缩放-->
    <scale
        android:repeatMode="reverse"
        android:repeatCount="1"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.5"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"/>
</set>
  • rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--旋转-->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="0"
        android:duration="1000"
        />
    <!--
        android:repeatMode="reverse" //是模式 ,反方向也会走一遍
        android:fromDegrees="0" //开始是0 结束是360 就顺时针 - 开始是360 结束是 0 就是逆时针
        android:toDegrees="360"
        -->
</set>
  • alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--改变透明度-->
    <alpha
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatMode="reverse"
        android:repeatCount="1"
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>
</set>
  • shangxia.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--上下-->
    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="100"
        android:toXDelta="0.0"
        android:toYDelta="0.0"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:duration="500"
        />

</set>
  • scale2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--缩放-->
    <scale
        android:repeatMode="reverse"
        android:repeatCount="-1"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.5"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"/>
</set>
  • rotate2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--旋转-->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:duration="1000"
        />
    <!--
        android:repeatMode="reverse" //是模式 ,反方向也会走一遍
        android:fromDegrees="0" //开始是0 结束是360 就顺时针 - 开始是360 结束是 0 就是逆时针
        android:toDegrees="360"
        -->
</set>
  • 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.AnimationMessageActivity">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:id="@+id/fk"
        android:src="@mipmap/huangguan"/>

    <LinearLayout
        android:layout_above="@+id/ll_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="平移"
            android:textSize="20sp"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:id="@+id/but"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放"
            android:textSize="20sp"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/but1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:id="@+id/but2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="透明度"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:id="@+id/but3"/>
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/ll_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上下"
            android:textSize="20sp"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:id="@+id/but4"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放"
            android:textSize="20sp"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/but5"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:id="@+id/but6"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:id="@+id/but7"/>
    </LinearLayout>

</RelativeLayout>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: Android Studio 是一款专门用于开发 Android 应用程序的集成开发环境(IDE)。在 Android Studio 中创建的项目可以包含许多不同的元素,例如布局文件、Java 代码、XML 文件等等。 补间动画(Tween Animation)是一种 Android Studio 中的动画类型,可以通过一系列的关键帧(Keyframes)来定义动画效果。补间动画可以用于平移、旋转、缩放或透明度动画等。在 Android Studio 中,可以通过 XML 文件来创建补间动画。以下是创建补间动画的步骤: 1. 在 Android Studio 中创建一个新的 XML 文件。可以选择 File -> New -> Android Resource File。 2. 在弹出的对话框中,选择 Animation 作为资源类型,然后设置文件名和其他选项。 3. 打开新创建的 XML 文件,并定义动画的关键帧。 4. 保存并使用动画,可以在布局文件或代码中引用动画资源 ID。 例如,以下是一个简单的补间动画 XML 文件,它会将一个图像从左上角平移到右下角: ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="100%" android:toYDelta="100%" android:duration="1000" /> </set> ``` 可以在布局文件中使用如下代码来应用这个动画: ``` <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_image" android:animation="@anim/my_animation" /> ``` 这个例子中,@anim/my_animation 是一个指向动画资源文件的引用。当这个布局文件被加载时,图像会按照定义的动画效果进行移动。 ### 回答2: Android Studio补间动画是一种基于时间轴的运动动画,其运动轨迹是由起点、终点、路径以及持续时间等多个属性组成的。补间动画适用于控件的渐变、透明度、位置、大小、旋转等动画效果的实现。 首先,我们需要在XML文件中定义我们的动画效果。这可以通过使用内置XML属性实现。例如,对于alpha(透明度)动画,我们可以在XML文件中使用透明度属性,如下所示: ``` <alpha android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="1000"/> ``` 这将从一个完全不透明的控件开始,逐渐到50%的透明度,持续时间为1秒钟(1000毫秒)。 除了透明度,我们还可以使用其他属性来定义不同的动画效果。 例如,用于旋转的属性是rotation,用于平移的属性是translationX和translationY,而用于缩放的属性是scaleX和scaleY等。 一旦我们定义了动画的XML文件,我们就可以在我们的代码中引用它并应用它。使用AnimatorInflater. inflateResource()或AnimationUtils. loadAnimation()方法可以将动画文件加载到我们的代码中。 ``` // 加载一个XML文件中的动画资源 Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_in); // 将所加载的动画应用于控件 textview.startAnimation(animation); ``` 最后,启动和停止动画非常简单,只需调用startAnimation()和clearAnimation()方法。 ``` textview.startAnimation(animation); //启动动画 textview.clearAnimation(); //停止动画 ``` 总的来说,Android Studio补间动画是一种非常有用的技术,可以使我们的应用程序更有吸引力和更有趣。我们只需要定义我们的动画效果,然后在我们的代码中引用它们,就可以在我们的应用程序中快速应用它们。 ### 回答3: Android Studio是一款非常流行的Android开发工具,它允许开发者使用各种技术和方法来开发Android应用程序。其中一种最常用的技术就是补间动画,通过它可以实现许多流行的动效,比如渐隐渐现、移动、旋转等。 补间动画是一种类型的动画,用于在指定时间内平滑地改变视图对象的属性。一般来说,补间动画由属性动画器、动画资源和XML布局文件三部分组成。属性动画器是动画资源的核心,它定义了需要动画的视图对象以及如何对它进行动画动画资源是指定义了动画的各种属性和效果,比如动画的持续时间、移动方向和加速度等。XML布局文件则用于指定需要动画的控件对象和动画资源。 通过使用Android Studio补间动画,您可以为Android应用程序添加一些非常酷的效果。这些效果可以让您的应用程序更加吸引人,将用户与其信任,同时提高应用程序的可见性和意识度。此外,补间动画还可以改进应用程序的功能性,比如将状态切换、转场动画和滚动效果等集成到您的应用程序中,以提高用户体验。 总之,补间动画是一种非常有用的技术,用于开发Android应用程序。通过使用Android Studio提供的补间动画工具,开发者可以轻松地创造出各种各样的动画效果,从而为其应用程序添加一些非常酷的新功能。因此,如果您是一位Android开发者,就应该学习并应用补间动画来改进您的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值