当前位置: 首页 > news >正文

Android——动画

帧动画

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

frame.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/diqiu"android:duration="120" /><itemandroid:drawable="@drawable/huoxing"android:duration="120" /><itemandroid:drawable="@drawable/tuxing"android:duration="120" /><itemandroid:drawable="@drawable/shuixing"android:duration="120" />
</animation-list>

将其设置为background

<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>

执行动画

    @Overrideprotected 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() {@Overridepublic void onClick(View v) {if (flag) {anim.start();flag = false;} else {anim.stop();flag = true;}}});}

案例代码

补间动画

补间动画包括alpha(透明度)rotate(旋转)scale(缩放)translate(平移)

  • alpha.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:duration="2000"android:fromAlpha="0"android:toAlpha="1" />
</set>
  • rotate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"><rotateandroid:duration="2000"android:fromDegrees="0"android:pivotX="50%"android:pivotY="50%"android:toDegrees="360" />
</set>
  • scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"><scaleandroid: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
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="2000"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="400"android:toYDelta="400" />
</set>

启动动画

        ImageView iv = findViewById(R.id.iv);iv.setOnClickListener(new View.OnClickListener() {@Overridepublic 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
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);valueAnimator.setDuration(2000);valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(@NonNull ValueAnimator animation) {float value = (float) animation.getAnimatedValue();Log.e("leo", "value:" + value);}});valueAnimator.start();
  • ObjectAnimator

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

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

监听

        objectAnimator.addListener(new Animator.AnimatorListener() {@Overridepublic void onAnimationStart(@NonNull Animator animation) {//开始}@Overridepublic void onAnimationEnd(@NonNull Animator animation) {//结束}@Overridepublic void onAnimationCancel(@NonNull Animator animation) {//取消}@Overridepublic void onAnimationRepeat(@NonNull Animator animation) {//重复}});

案例代码

http://www.xdnf.cn/news/12817.html

相关文章:

  • IPTV电视信息发布直播点播系统:营造数字化个性化融合化多媒体IPTV电视信息发布平台
  • 预训练与微调:大模型如何“学习知识”?
  • Python 网络爬虫基础理论与实战指南
  • 【每日八股】复习计算机网络 Day1:TCP 的头部结构 + TCP 确保可靠传输 + TCP 的三次握手
  • 【漫话机器学习系列】209.均值的标准误差(Standard Error of the Mean)
  • 完整的 .NET 6 分布式定时任务实现(Hangfire + Redis 分布式锁)
  • 故障诊断常用算法
  • 2025妈妈杯数学建模D题完整分析论文
  • Kubernetes Pod 调度策略:从基础到进阶
  • java面向对象09:方法的重写
  • PyTorch入门------卷积神经网络
  • TCP/IP和UDP协议的发展历程
  • POSIX 信号量(Semaphore)
  • MacOS怎么显示隐藏文件
  • Vue3 实战:打造多功能旅游攻略选项卡页面
  • 记录学习的第二十九天
  • unity TEngine学习记录3
  • 精准计量+AI管控——安科瑞助力高校水电管理数字化转型
  • C#插件与可扩展性
  • 闲来无事,用HTML+CSS+JS打造一个84键机械键盘模拟器
  • 优化自旋锁的实现
  • pdfjs库使用3
  • Linux内核机制——内存管理
  • C++ 迭代器失效详解:如何避免 vector 操作中的陷阱
  • 数控铣床自动上下料机械手控制装置设计
  • IDEA 2025.1更新-AI助手试用和第三方模型集成方案
  • C++类和对象上
  • 00.IDEA 插件推荐清单(2025)
  • Jenkins 简易使用记录
  • 从零到一:管理系统设计新手如何快速上手?