通过xml文件来设置动画
@android:anim/accelerate_interpolator: 越来越快
@android:anim/decelerate_interpolator:越来越慢
@android:anim/accelerate_decelerate_interpolator:先快后慢
@android:anim/anticipate_interpolator: 先后退一小步然后向前加速
@android:anim/overshoot_interpolator:快速到达终点超出一小步然后回到终点
@android:anim/anticipate_overshoot_interpolator:到达终点超出一小步然后回到终点
@android:anim/bounce_interpolator:到达终点产生弹球效果,弹几下回到终点
@android:anim/linear_interpolator:均匀速度。
——AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
——DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程。
——CycleInterpolator:动画从开始到结束,变化率是循环给定次数的正弦曲线。
——AccelerateDecelerateInterpolator:动画从开始到结束,变化率是先加速后减速的过程。
——LinearInterpolator:动画从开始到结束,变化率是线性变化。
设定动画延迟时间:android:startOffset="50"
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" android:startOffset="50"> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:startOffset="200" android:duration="1000"/> </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:pivotX="50%p" android:pivotY="50%" android:duration="1000"/> </set>
scale.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:toXScale="0" android:fromYScale="1.0" android:toYScale="0" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/> </set>
translate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="50%" android:toYDelta="200%" android:duration="2000"/> </set>
MainActivity.java
package com.kale.anim; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { ImageView iV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iV = (ImageView)findViewById(R.id.imageView); } public void buttonListener(View v) { Animation anim = null; switch (v.getId()) { case R.id.alpha_button: anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha); break; case R.id.rotate_button: anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate); break; case R.id.scale_button: anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale); break; case R.id.translate_button: anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate); break; default: break; } //设置开始动画 iV.startAnimation(anim); } }
通过工具类就能加载xml文件,这样就能使用xml文件了。最后贴上xml的界面文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="28dp" android:orientation="vertical" > <Button android:id="@+id/alpha_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="透明度改变" /> <Button android:id="@+id/rotate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="旋转动画" /> <Button android:id="@+id/scale_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="缩放动画" /> <Button android:id="@+id/translate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="移动动画" /> </LinearLayout> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout1" android:layout_centerHorizontal="true" android:layout_marginTop="56dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
关于动画速率设置。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
……
</set>
example:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:startOffset="200" android:duration="1000"/> </set>
代码中可以这样设置:
AnimationSet set = new AnimationSet(true); set.setInterpolator(new DecelerateInterpolator());
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?