Android动画(二)——补间动画

2023-12-17 14:15
文章标签 android 动画 补间

本文主要是介绍Android动画(二)——补间动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

介绍

 Xml文件定义View动画

补充

alpha_animation.xml(透明度)

rotate_animation.xml(旋转)

scale_animation.xml(伸缩)

translate_animation.xml (平移)

 group_animation.xml(组合)

主活动代码

效果图

Java代码中使用补间动画

透明度定义:

缩放定义:

平移定义:

旋转定义

 集合定义

完整代码

效果仍如上图


介绍

        补间动画(Tween Animation)是一种常见的动画技术,用于在动画序列中创建平滑的过渡效果。它通过定义起始状态和结束状态之间的差异来自动生成中间帧,从而实现连续的动画效果。

        补间动画通常涉及两个关键帧(Keyframe):起始关键帧和结束关键帧。起始关键帧定义了动画的初始状态,而结束关键帧定义了动画的最终状态。通过在这两个关键帧之间进行插值计算,可以生成一系列中间帧,形成平滑的动画过渡。

        在补间动画中,可以对多个属性进行动画化,如位置、大小、旋转、透明度等。通过指定每个属性的起始值和结束值,并设置动画的持续时间和缓动函数,可以控制动画的速度、加速度和变化方式。

        补间动画具有简单易用、流畅自然的特点,常用于用户界面设计、游戏开发和电影制作等领域。现在许多动画软件和库都提供了方便的补间动画功能,使得创建和控制补间动画变得更加高效和灵活。

 Xml文件定义View动画

 通过xml来定义View动画涉及到一些公有的属性(在AndroidStudio上不能提示):

android:duration     动画持续时间
android:fillAfter    为true动画结束时,View将保持动画结束时的状态
android:fillBefore   为true动画结束时,View将还原到开始开始时的状态
android:repeatCount  动画重复执行的次数
android:repeatMode   动画重复模式 ,重复播放时restart重头开始,reverse重复播放时倒叙回放,该属性需要和android:repeatCount一起使用
android:interpolator 插值器,相当于变速器,改变动画的不同阶段的执行速度复制代码

        这些属性是从Animation中继承下来的,在alpha、rotate、scale、translate标签中都可以直接使用。

        利用xml文件定义View动画需要在工程的res目录下创建anim文件夹,所有的xml定义的View动画都要放在anim目录下。

补充

        在动画中,以数值、百分数、百分数p,比如:50、50%、50%p,他们取值的代表的意义各不相同:

        50表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移50px的位置;

        50%表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移View宽度或高度的50%处的位置;

        50%p表示以View左上角为原点沿坐标轴正方向(x轴向右,y轴向下)偏移父控件宽度或高度的50%处的位置(p表示相对于ParentView的位置)。

alpha_animation.xml(透明度)

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:fillAfter="false"android:fromAlpha="1"android:toAlpha="0">
</alpha>

rotate_animation.xml(旋转)

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:fillAfter="false"android:fromDegrees="0"android:toDegrees="360"android:pivotX="50%"android:pivotY="50%">
</rotate>

scale_animation.xml(伸缩)

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:fillAfter="false"android:fromXScale="1.0"android:fromYScale="1.0"android:pivotX="50%"android:pivotY="50%"android:toXScale="0.5"android:toYScale="0.5"></scale>

translate_animation.xml (平移)

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="2000"android:fillAfter="false"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="100%"android:toYDelta="100%">
</translate>

 group_animation.xml(组合)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:duration="2000"android:fillAfter="false"android:fromAlpha="1"android:toAlpha="0"/><scaleandroid:duration="2000"android:fillAfter="false"android:fromXScale="1.0"android:fromYScale="1.0"android:pivotX="50%"android:pivotY="50%"android:toXScale="0.5"android:toYScale="0.5"/><rotateandroid:duration="2000"android:fillAfter="false"android:fromDegrees="0"android:toDegrees="360"android:pivotX="50%"android:pivotY="50%"/><translateandroid:duration="2000"android:fillAfter="false"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="100%"android:toYDelta="100%"/>
</set>

主活动代码

package com.example.animationstudy;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {ImageView imageView2;Button alpha;Button scale;Button rotate;Button translate;Button group;Animation groupAnim;Animation alphaAnima;Animation scaleAnima;Animation rotateAnim;Animation translateAnim;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView2 = (ImageView) findViewById(R.id.imageView1);alpha = (Button) findViewById(R.id.alpha);scale = (Button) findViewById(R.id.scale);rotate = (Button) findViewById(R.id.rotate);translate = (Button) findViewById(R.id.translate);group = (Button) findViewById(R.id.group);alphaAnima = AnimationUtils.loadAnimation(this,R.anim.alpha_animation);scaleAnima = AnimationUtils.loadAnimation(this,R.anim.scale_animation);rotateAnim = AnimationUtils.loadAnimation(this,R.anim.rotate_animation);translateAnim = AnimationUtils.loadAnimation(this,R.anim.translate_animation);groupAnim = AnimationUtils.loadAnimation(this,R.anim.group_animation);alpha.setOnClickListener(this);scale.setOnClickListener(this);rotate.setOnClickListener(this);translate.setOnClickListener(this);group.setOnClickListener(this);}@Overridepublic void onClick(View view) {if ((view.getId() == R.id.alpha)){imageView2.startAnimation(alphaAnima);}if ((view.getId() == R.id.scale)){imageView2.startAnimation(scaleAnima);}if ((view.getId() == R.id.rotate)){imageView2.startAnimation(rotateAnim);}if ((view.getId() == R.id.translate)){imageView2.startAnimation(translateAnim);}if ((view.getId() == R.id.group)){imageView2.startAnimation(groupAnim);}}
}

效果图

Java代码中使用补间动画

透明度定义:

AlphaAnimation alphaAnima = new AlphaAnimation(1, 0);alphaAnima.setDuration(2000);

缩放定义:

ScaleAnimation scaleAnima = new ScaleAnimation(1, 0.5f,1, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);scaleAnima.setDuration(2000);

平移定义:

TranslateAnimation translateAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,Animation.RELATIVE_TO_SELF, 1,Animation.RELATIVE_TO_SELF, 0,Animation.RELATIVE_TO_SELF, 1);translateAnim.setDuration(2000);

旋转定义

RotateAnimation rotateAnim = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotateAnim.setDuration(2000);

 集合定义

AnimationSet animationSet = new AnimationSet(true);animationSet.addAnimation(alphaAnima);animationSet.addAnimation(rotateAnim);animationSet.addAnimation(scaleAnima);animationSet.addAnimation(translateAnim);

完整代码

package com.example.animationstudy;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;public class MainActivity2 extends AppCompatActivity implements View.OnClickListener{ImageView imageView;Button button;Button next;AnimationSet animationSet;AlphaAnimation alphaAnima;ScaleAnimation scaleAnima;RotateAnimation rotateAnim;TranslateAnimation translateAnim;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);imageView = (ImageView) findViewById(R.id.imageView2);button = (Button) findViewById(R.id.button2);next = (Button) findViewById(R.id.nextActivity2);button.setOnClickListener(this);next.setOnClickListener(this);init();}@Overridepublic void onClick(View view) {if (view.getId() == R.id.button2){imageView.startAnimation(animationSet);}if (view.getId() == R.id.nextActivity2){Intent intent = new Intent(MainActivity2.this, MainActivity3.class);startActivity(intent);}}public void init() {alphaAnima = new AlphaAnimation(1, 0);alphaAnima.setDuration(2000);rotateAnim = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotateAnim.setDuration(2000);scaleAnima = new ScaleAnimation(1, 0.5f,1, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);scaleAnima.setDuration(2000);translateAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,Animation.RELATIVE_TO_SELF, 1,Animation.RELATIVE_TO_SELF, 0,Animation.RELATIVE_TO_SELF, 1);translateAnim.setDuration(2000);animationSet = new AnimationSet(true);animationSet.addAnimation(alphaAnima);animationSet.addAnimation(rotateAnim);animationSet.addAnimation(scaleAnima);animationSet.addAnimation(translateAnim);}
}

效果仍如上图

上一篇:Android动画(一)——逐帧动画-CSDN博客

文章参考:Android 动画详解-CSDN博客 

这篇关于Android动画(二)——补间动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/504666

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I