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

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo