第二十六讲:Android之Animation

懒惰象生锈一样,比操劳更能消耗身体;经常用的钥匙,总是亮闪闪的。 —— 富兰克林

本讲内容:Animation 动画

一、Android中动画的实现分两种方式,一种方式是补间动画 Tween Animation,就是说你定义一个开始和结束,中间的部分由程序运算得到。另一种叫逐帧动画 Frame Animation,就是说一帧一帧的连起来播放就变成了动画。和放电影的机制很相似,下面我们逐个学习。

下面引用官方文档

从图我们可以知道Animation的直接子类有AlphaAnimation、AnimationSet、RotateAnimation、ScaleAnimation、TranslateAnimation。其中AnimationSet包含一系列的Animation。

二、Tween动画是操作某个控件让其展现出旋转()、渐变、移动、缩放的这么一种转换过程。我们可以以XML形式定义动画,也可以编码实现。

三、Tween Animation共同的节点属性

属性[类型]

功能  备注
Duration[long] 动画持续时间 毫秒为单位
fillAfter [boolean] 当设置为true 动画执行后,控件将停留在执行结束的状态
fillBefore[boolean] 当设置为true 动画执行后,控件将停留在执行之前的状态
repeatCount[int] 动画的重复次数  
RepeatMode[int] 定义重复的行为 1:重新开始  2:plays backward
startOffset[long] 动画执行之前的等待时间 毫秒为单位
     
     

四、alpha  渐变透明度动画效果(范围在0.0和1.0之间,分别代表透明和完全不透明)

android:fromAlpha="1.0" 代表起始alpha值 浮点值,范围在0.0和1.0之间
android:toAlpha="0.0" 代表结尾alpha值 浮点值,范围也在0.0和1.0之间。
android:duration="500" 动画持续时间 毫秒为单位
     

五、scale
渐变尺寸伸缩动画效果(浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍)

android:fromXScale 起始的X方向上相对自身的缩放比例 浮点值,范围在0.0和1.0之间
android:toXScale 结尾的X方向上相对自身的缩放比例  
android:fromYScale 起始的Y方向上相对自身的缩放比例  
android:toYScale 结尾的Y方向上相对自身的缩放比例  
android:pivotX 缩放的中轴点X坐标 如果想表示中轴点为图像的中心,
android:pivotY 缩放的中轴点Y坐标 可以把两个属性值定义成0.5或者50%。

android:pivotX属性代表旋转中心的X坐标值,android:pivotY属性代表旋转中心的Y坐标值,

这两个属性也有三种表示方式,

数字方式代表相对于自身左边缘的像素值,(绝对位置定位)Ainmation.ABSOLUTE

num%方式代表相对于自身左边缘或顶边缘的百分比,(相对于控件本身定位)Ainmation.RELATIVE_TO_SELF

num%p方式代表相对于父容器的左边缘或顶边缘的百分比。(相对于父控件) Ainmation.RELATIVE_TO_PARENF

六、translate
 画面转换位置移动动画效果(代表一个水平、垂直的位移)

android:fromXDelta 起始X方向的位置 浮点数、num%、num%p
android:toXDelta 结尾X方向上的位置  
android:fromYScale 起始Y方向上的位置  
android:toYDelta 结尾Y方向上的位置  
     

以上四个属性都支持三种表示方式:浮点数、num%、num%p

数字方式代表相对于自身左边缘的像素值,(绝对位置定位)

num%方式代表相对于自身左边缘或顶边缘的百分比,(相对于控件本身定位)100%表示移动自己的1倍距离

num%p方式代表相对于父容器的左边缘或顶边缘的百分比。(相对于父控件)

七、rotate  画面转移旋转动画效果

android:fromDegrees 代表起始角度 浮点值,单位:度
android:toDegrees 代表结尾角度 浮点值,单位:度
android:pivotX 旋转中心的X坐标  
android:pivotY 表旋转中心的Y坐标值  
     

我们通过一个例子感受一下,代码的讲解都写在注释里了

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.text.MainActivity$PlaceholderFragment" >
 <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_weight="1"/>
   <Button
       android:id="@+id/translate"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="测试translate动画效果" />
   <Button
       android:id="@+id/alpha"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="测试alpha动画效果" />
   <Button
       android:id="@+id/rotate"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="测试rotate动画效果" />
   <Button
       android:id="@+id/scale"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="测试scale动画效果" />

</LinearLayout>

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener {
	private Button translate;
	private Button alpha;
	private Button rotate;
	private Button scale;
	private ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		image = (ImageView) findViewById(R.id.image);
		translate = (Button) findViewById(R.id.translate);
		alpha = (Button) findViewById(R.id.alpha);
		rotate = (Button) findViewById(R.id.rotate);
		scale = (Button) findViewById(R.id.scale);
		translate.setOnClickListener(this);
		alpha.setOnClickListener(this);
		rotate.setOnClickListener(this);
		scale.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 创建一个AnimationSet对象
		AnimationSet animationSet = new AnimationSet(true);
		switch (v.getId()) {
		case R.id.alpha:
			// 创建一个AlphaAnimation对象
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			// 设置动画执行的时间(单位:毫秒)
			alphaAnimation.setDuration(1000);
			// 将AlphaAnimation对象添加到AnimationSet当中
			animationSet.addAnimation(alphaAnimation);
			// 使用ImageView的startAnimation方法开始执行动画
			image.startAnimation(animationSet);
			break;

		case R.id.translate:
			TranslateAnimation translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
					0.5f, Animation.RELATIVE_TO_SELF, 0f,
					Animation.RELATIVE_TO_SELF, 1.0f);
			translateAnimation.setDuration(1000);
			animationSet.addAnimation(translateAnimation);
			image.startAnimation(animationSet);
			break;

		case R.id.rotate:
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_PARENT, 1f,
					Animation.RELATIVE_TO_PARENT, 0f);
			rotateAnimation.setDuration(5000);
			animationSet.addAnimation(rotateAnimation);
			image.startAnimation(animationSet);
			break;

		case R.id.scale:
			ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
					0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animationSet.addAnimation(scaleAnimation);
			animationSet.setStartOffset(1000);
			animationSet.setDuration(2000);
			image.startAnimation(animationSet);
			break;
		}
	}
}

下面是运行结果:

本讲到这里,谢谢大家!

时间: 2024-07-30 16:12:46

第二十六讲:Android之Animation的相关文章

第二十六讲:基础一开放封闭原则

做出来的软件具有很强的扩展性,扩展并不是修改以前的源代码,而是在以前的代码之外添加新的功能. 重点是对扩展开放,对修改关闭. 银行业务员下有很多子类:负责存款的银行业务员,负责取款的银行业务员,负责转账的银行业务员.

Unity3D教程宝典之Shader篇:第二十六讲ImageEffects_Twirl

转载自风宇冲Unity3D教程学院 Twirl是一个全屏画面扭曲的效果,新仙剑的战斗切换有用到这个效果. 主要有三个设置: center  扭曲的中心点 radius 扭曲的范围 angle 扭曲的角度 用到了如下函数 Matrix4x4.TRS(Vector3 pos, Quaternion rotate,Vector3 scale) 创建一个包括位移,旋转,缩放的矩阵 Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, Quatern

十六、Android 滑动效果汇总

Android 滑动效果入门篇(一)-- ViewFlipper Android 滑动效果入门篇(二)-- Gallery Android 滑动效果基础篇(三)-- Gallery仿图像集浏览 Android 滑动效果基础篇(四)-- Gallery + GridView Android 滑动效果进阶篇(五)-- 3D旋转 Android 滑动效果进阶篇(六)-- 倒影效果 ViewFilpper 是Android官方提供的一个View容器类,继承于ViewAnimator类,用于实现页面切换,

四十六、android中的Bitmap

四十六.android中的Bitmap: http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html 四十七.实现调用Android手机的拍照功能: http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304970.html

第二十六篇:USB3.0高带宽ISO(48KBytes/125us)实战

USB3.1技术已经推出, 10Gbps的速率足以满足数据, HD视频传输的要求. 要步入USB3.1的研发, 还得将USB3.0的基础打扎实. 微软提供的SUPER MUTT只包含一个接口0, 其下有两个ALT, ALT 1与ALT 2, 分别包含了两对ISO IN/OUT端点, 不过, 只有ALT 2下的ISO OUT EP的bMaxBurst为1, 而其它三个ISO EP的bMaxBurst均为0, 而所有的ISO EP的Mult均为0. 即只有一个ISO EP支持2KBytes/125u

“全栈2019”Java多线程第二十六章:同步方法生产者与消费者线程

难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多线程第二十六章:同步方法生产者与消费者线程 下一章 "全栈2019"Java多线程第二十七章:Lock获取lock/释放unlock锁 学习小组 加入同步学习小组,共同交流与进步. 方式一:关注头条号Gorhaf,私信"Java学习小组". 方式二:关注公众号Gorha

第二十六个知识点:描述NAF标量乘法算法

第二十六个知识点:描述NAF标量乘法算法 NAF标量乘法算法是标量乘法算法的一种增强,该算法使用了非邻接形式(Non-Adjacent Form)表达,减少了算法的期望运行时间.下面是具体细节: 让\(k\)是一个正整数,\(P\)是一个在域\(F_q\)上椭圆曲线\(E\)上的点.这个计算乘法操作\(Q = k * P\)就是圆曲线上的标量乘法操作(点乘).一个最简单计算的方法就是基于双倍-加法的霍纳规则的变体.顾名思义,该方法最突出的两个构建块是点加倍和点添加原语.就像名字那样,算法也十分简

【WPF学习】第二十六章 Application类——应用程序的生命周期

原文:[WPF学习]第二十六章 Application类--应用程序的生命周期 在WPF中,应用程序会经历简单的生命周期.在应用程序启动后,将立即创建应用程序对象,在应用程序运行时触发各种应用程序事件,你可以选择监视其中的某些事件.最后,当释放应用程序对象时,应用程序将结束. 一.创建Application对象 使用Application类的最简单方式是手动创建它.下面的示例演示了最小的程序:在应用程序入口(Main()方法)处创建名为MainWindow的窗口,并启动一个新的应用程序: 在本质

【Android的从零单排开发日记】之入门篇(十六)——Android的动画效果

      什么是动画,动画的本质是通过连续不断地显示若干图像来产生“动”起来的效果.比如说一个移动的动画,就是在一定的时间段内,以恰当的速率(起码要12帧/秒以上,才会让人产生动起来的错觉)每隔若干时间在屏幕上更新一次位置.游戏中的动画效果也是由此而来.同样还有其他属性变更所引起的动画效果,从数学的角度来看,包括:(1)平移(2)旋转(3)缩放(4)透明度.当然这些属性可以组合起来使用,来达到更绚丽的画面.但是不论什么样的组合方式,我们都可以统一用Matirx运算来实现,从技术实现的角度来讲,