平移动画是通过为动画指定开始位置、结束位置以及动画持续时长来创建动画,在XML文件中定义平移动画,具体代码如下:
<?xml version="1.0" encoding="utf-8"?><translate
android:fromXDelta=“0.0”
android:fromYDelta=“0.0”
android:toXDelta=“100”
android:toYDelta=“0.0”
android:repeatCount=“infinite”
android:repeatMode=“reverse”
android:duration=“4000”/>
上述代码定义了一个让View从起始x(0.0)位置平移到结束x(100)位置,持续时间为4s的平移动画。
注意:这里的坐标不是指屏幕像素的坐标,而是指相对于View的所在位置的坐标。
例如:开始位置0.0表示在View最开始的地方平移(即布局文件定义View所在的位置)。
===================================================================
布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_marginBottom=“5dp”>
<ImageView
android:id=“@+id/iv_bean”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“false”
android:layout_alignParentEnd=“true”
android:layout_alignParentRight=“true”
android:layout_alignParentBottom=“true”
android:layout_marginEnd=“145dp”
android:layout_marginRight=“145dp”
android:layout_marginBottom=“320dp”
android:src=“@drawable/icon” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentBottom=“true”>
<Button
android:id=“@+id/btn_one”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/btn_1”/>
<Button
android:id=“@+id/btn_two”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/btn_2”/>
<Button
android:id=“@+id/btn_three”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/btn_3”/>
<Button
android:id=“@+id/btn_four”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/btn_4”/>
其中,按钮名称在(res/values/strings.xml)文件中说明,方便程序管理:
Animation
渐变
旋转
缩放
移动
在res目录下创建一个anim文件夹,并新建4个XML文件,分别命名为:
-
“alpha_animation.xml”(透明度渐变动画)
-
“rotate_animation.xml” (旋转动画)
-
“scale_animation.xml” (缩放动画)
-
“translate_animation.xml”(平移动画)。
分别将前面讲解的四个动画的XML代码放入创建的四个文件中。
在XML文件中定义好补间动画资源后,需要将动画资源设置到控件上。要实现该功能,需要在MainActivity中调用AnimationUtils类的loadAnimation()方法加载动画资源,并为图片(@drawable/icon)设置指定动画,具体代码如下:
文件MainActivity.java编写逻辑代码:
package com.example.animation;
import androidx.appcompat.app.AppCompatActivity;
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 {
private ImageView ivBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化组件并添加点击事件
Button buttonOne = findViewById(R.id.btn_one);
Button buttonTwo = findViewById(R.id.btn_two);
Button buttonThree = findViewById(R.id.btn_three);
Button buttonFour = findViewById(R.id.btn_four);
ivBean = findViewById(R.id.iv_bean);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
buttonThree.setOnClickListener(this);
buttonFour.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.btn_one:
//调用AnimationUtils的loadAnimation()方法加载动画
//单击按钮使用图片渐变
Animation alpha = AnimationUtils.loadAnimation(this,R.anim.alpha_animation);
ivBean.startAnimation(alpha);
break;
case R.id.btn_two:
//单击按钮使图片旋转
Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate_animation);
ivBean.startAnimation(rotate);
break;
case R.id.btn_three:
//单击按钮使图像缩放
Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale_animation);
ivBean.startAnimation(scale);
break;
case R.id.btn_four:
//单击按钮使图片移动
Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate_animation);
ivBean.startAnimation(translate);
break;
}
}
}
可以看出,XML中定义的动画是通过AnimationUtils.loadAnimation()方法加载的,最后通过startAnimation()方法将动画设置到ImageView中。
video1.mp4
=================================================================
-
逐帧动画(Frame Animation)是按照事先准备好的静态图像顺序播放的,利用人眼的“视觉暂留”原理,给用户造成动画的错觉。逐帧动画的原理与放胶片看电影的原理是一样的,它们都是一张一张地播放事先准备好的静态图像。
-
在使用逐帧动画时,需要在res/drawable目录下创建好帧动画的XML文件,并在< animation-list >节点的< item >子节点中,指定图片帧出现的顺序以及每帧的持续时间。
===================================================================
布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><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”
android:background=“@android:color/white”
tools:context=“.MainActivity”>
<ImageView
android:id=“@+id/iv_flower”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“false”
android:layout_alignParentEnd=“true”
android:layout_alignParentRight=“true”
android:layout_alignParentBottom=“true”
android:layout_marginEnd=“145dp”
android:layout_marginRight=“145dp”
android:layout_marginBottom=“320dp”
android:background=“@drawable/frame” />
<Button
android:id=“@+id/btn_play”
android:layout_width=“70dp”
android:layout_height=“70dp”
android:layout_centerInParent=“true”
android:background=“@android:drawable/ic_media_play”/>
注意:ImageView的背景是Frame动画资源。
将需要制作为动画的图片统一存入drawable目录下,同时在drawable目录下创建Frame动画资源文件frame.xml,具体代码如下。
<?xml version="1.0" encoding="utf-8"?><item
android:drawable=“@drawable/rose_one”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_two”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_three”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_four”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_five”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_six”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_seven”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_eight”
android:duration=“200” />
<item
android:drawable=“@drawable/rose_nine”
android:duration=“200” />
上述代码是定义Frame动画的基本语法格式,< animation-list >为帧动画的根节点,其中属性drawable表示当前帧要播放的图片,duration表示当前帧的播放时长,单位为ms,和前面补间动画中的属性介绍一致。
尾声
以薪资待遇为基础,以发展为最终目标,要在高薪资的地方,谋求最好的发展!
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ation=“200” />
上述代码是定义Frame动画的基本语法格式,< animation-list >为帧动画的根节点,其中属性drawable表示当前帧要播放的图片,duration表示当前帧的播放时长,单位为ms,和前面补间动画中的属性介绍一致。
尾声
以薪资待遇为基础,以发展为最终目标,要在高薪资的地方,谋求最好的发展!
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。
[外链图片转存中…(img-sKI9IHsi-1714365960930)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!