Android 动画集与自定义动画
来源:互联网 发布:倩女幽魂辅助软件什么 编辑:程序博客网 时间:2024/04/29 10:44
Android系统自定义的动画效果有
- AlphaAnimation 透明度动画
- RotateAnimation 旋转动画
- TranslateAnimation 移位动画
- ScaleAnimation 缩放动画
- AnimationSet 动画效果集合
前四种动画的使用方法大同小异,第五种是将前四种动画任意组合而成的动画集合,也较为简单。这里我来记录下各种动画的使用方法并实现自定义动画。
首先新建个工程,修改默认布局,增添六个按钮
<?xml version="1.0" encoding="utf-8"?><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.zy.myapplication.MainActivity"> <Button android:id="@+id/btnAlpha" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="透明度动画" /> <Button android:id="@+id/btnRotate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="旋转动画" /> <Button android:id="@+id/btnTranslate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="移位动画" /> <Button android:id="@+id/btnScale" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="缩放动画" /> <Button android:id="@+id/btnAnimationSet" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="动画集合" /> <Button android:id="@+id/btnCustomAnimation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义动画" /></LinearLayout>
在MainActivity中声明并引用各个Button对象
private Button alphaButton; private Button rotateButton; private Button translateButton; private Button scaleButton; private Button animSetButton; private Button customAnimButton;
一、AlphaAnimation 的使用方法最为简单,构造函数的两个参数意味着透明度从零(完全透明)到一(完全不透明)。setDuration(long)设置动画的持续时间,以毫秒为单位。
AlphaAnimation alpha = new AlphaAnimation(0, 1);alpha.setDuration(2000);alphaButton.startAnimation(alpha);
此外,可以通过以下函数设置保持动画最终效果不变,即如果透明度是从一变为零,那动画结束后就可以保持透明度始终为零。
alpha.setFillAfter(true);
二、RotateAnimation 有多个构造函数
以下四个参数的构造函数意为从角度0转到360度,以相对组件左上角X轴偏右六百个像素,Y轴偏零个像素为中心点旋转。
RotateAnimation rotate=new RotateAnimation(0,360,600,0);
常用的为以下构造函数,Animation.RELATIVE_TO _SELF意为旋转中心类型,意思是相对组件自身,偏移量0.5f,意为50%,在这里即表示让组件以自身中心点为中心轴旋转。
RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotate.setDuration(1000);rotateButton.startAnimation(rotate);
旋转中心类型还有相对父级容器(Animation.RELATIVE_TO_PARENT),偏移量以百分比表示、绝对指定类型(Animation.ABSOLUTE),偏移量以绝对偏移量表示。
三、TranslateAnimation的使用方法与RotateAnimation 类似,重点是指定相对哪个组件来移动
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); translate.setDuration(1000); translateButton.startAnimation(translate);
四、ScaleAnimation
下列函数意为让组件X轴从一缩小到零,Y轴从一放大到二,缩放中心为相对自身的中心点
ScaleAnimation scale = new ScaleAnimation(1, 0, 1, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scale.setDuration(1000); scaleButton.startAnimation(scale);
五、 AnimationSet意为动画的集合,可以向之添加多个动画,然后一起赋给某个组件。
AnimationSet animationSet = new AnimationSet(true); animationSet.setDuration(2000); AlphaAnimation another_alpha = new AlphaAnimation(0, 1); RotateAnimation another_rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.addAnimation(another_alpha); animationSet.addAnimation(another_rotate); animSetButton.startAnimation(animationSet);
以上函数向animationSet 添加了透明度动画和旋转动画,可以看到按钮在旋转的同时透明度也在变化
六、以上动画类都是继承于父类Animation,因此我们想要自定义动画,一般也是继承于Animation类。
自定义动画效果如下,按钮呈现“摇头”动作,因为转为了gif动态图,动作看起来不是多流畅,实际上是不会的。
新建一个类继承于Animation,命名为CustomAnimation
Animation类含有一个applyTransformation方法,会在动画执行过程中不断执行该函数。参数interpolatedTime会在该过程中不断自增,从零增大到一,Transformation 表示组件对象,自定义的动画效果就是实施到该对象上。
protected void applyTransformation(float interpolatedTime, Transformation t) { }
我们只要重写applyTransformation函数,就可以实现按钮的“摇头”效果了。
package com.example.zy.myapplication;import android.view.animation.Animation;import android.view.animation.Transformation;/** * Created by ZY on 2016/5/18. */public class CustomAnimation extends Animation { //偏移距离 private float mDistance; //移动速度 private float mVelocity; public CustomAnimation(float distance, float velocity) { this.mDistance = distance; this.mVelocity = velocity; } protected void applyTransformation(float interpolatedTime, Transformation t) { float dis= (float) (Math.sin(interpolatedTime * mVelocity) * mDistance); t.getMatrix().setTranslate(dis, 0); }}
摇头效果是通过setTranslate()函数让组件移动的,X轴变化而Y轴不变。
通过Math.sin()函数让dis呈现周期性变化,这样就可以让按钮来回移动了。
CustomAnimation customAnimation = new CustomAnimation(30,40); customAnimation.setDuration(1000); customAnimButton.startAnimation(customAnimation);
七、为动画添加事件监听
分别在动画开始和结束时弹出一个Toast
customAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(MainActivity.this, "动画开始", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } });
代码可以点这里下载:Android动画集与自定义动画
- Android 动画集与自定义动画
- 【Android动画九章】-属性动画集
- 一行代码实现自定义转场动画--iOS自定义转场动画集
- 一行代码实现自定义转场动画--iOS自定义转场动画集
- Android帧动画实现,防OOM,比原生动画集节约超过十倍的资源
- Android Property Animation属性动画集:AnimatorSet(5)
- x文件的动画集
- 圣诞快乐动画集设计
- android画集合图形
- 安卓动画全解:补间动画(视图动画)、布局动画、属性动画、逐帧动画。动画Animation属性、Alpha属性、Scale属性、Translate属性、Rotate属性,动画集AnimationSet
- Android动画案例(五)布局动画与自定义动画
- 多动画集在D3D下的渲染
- 多动画集在D3D下的渲染
- 多动画集在D3D下的渲染
- Android 逐帧动画与自定义Dialog
- 浅析 Android 动画:自定义 Interpolator 与 TypeEvaluator
- Android Matrix的使用与自定义动画
- Android Matrix的使用与自定义动画
- Android更改语言模式
- ocp—051试题答案解析
- 图解net use 命令使用示例
- JQ中将事件绑定在动态添加的标签上(live/on/bind/delegate)
- snort: error while loading shared libraries
- Android 动画集与自定义动画
- java calender 获取本周一与周日的代码
- iOS代码调试(一)
- apache-tomcat非安装版配置
- Ubuntu 16.04 配置ipv6源
- maven目录结构解析
- get和post的区别
- L1-002. 打印沙漏
- 设计模式(一)-- 工厂模式