动画
Android动画主要包括以下两种类型
- View Animation(View动画) :在View动画框架中包括两种类型的动画: (1) Tween animation(补间动画) 可以对View进行一系列的动画操作,包括淡入淡出、缩放、平移、旋转四种 (2).Frame animation(帧动画) 其实就是将一个完整的动画拆分成一张张单独的图片,然后再将它们连贯起来进行播放,类似于动画片的工作原理
2.Property Animation(属性动画):它主要是通过修改一个对象的属性值来创建动画
view 动画
view动画-----补间动画
- 1:xml中
-
alph:渐变透明度动画效果
scale:渐变尺寸伸缩动画效果
translate:画面转换位置移动动画效果
rootate:画面转移旋转动画效果
- javacode中
-
AlphaAnimation:渐变透明度动画效果
ScaleAnimation:渐变尺寸伸缩动画效果
TranslateAnimation:画面转换位置移动动画效果
RotateAnimation:画面转移旋转动画效果
-
2:如何在xml文件中定义动画
步骤如下:
①新建 Android 项目
②在res目录中新建anim文件夹
③在anim目录中新建一个my_anim.xml(注意文件名小写)
④在 my_anim.xml 加入动画代码
5.引用方式:.在XML文件中:@[package:]anim/filenam
在Java代码中:R.anim.filename
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha /> <scale /> <translate /> <rotate /> </set>
3.代码中定义动画
splash = (RelativeLayout)findViewById(R.id.splash);
//以旋转动画为例
RotateAnimation rotateAnimation =
new
RotateAnimation(
0
,
360
,
Animation.RELATIVE_TO_SELF,
0
.5f,Animation.RELATIVE_TO_SELF,
0
.5f);
rotateAnimation.setDuration(
2000
);
rotateAnimation.setFillAfter(
true
); (播放完毕停留在那个位置)
view动画-----帧动画
帧动画是最容易实现的一种动画,这种动画更多的依赖于完善的UI资源,他的原理就是将一张张单独的图片连贯的进行播放,
从而在视觉上产生一种动画的效果;有点类似于某些软件制作gif动画的方式
步骤如下:
①新建 Android 项目
②在res目录中新建anim文件夹
③在anim目录中新建一个my_anim.xml(注意文件名小写)
④在 my_anim.xml 加入动画代码
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/a_0"
android:duration="100" />
<item
android:drawable="@drawable/a_1"
android:duration="100" />
<item
android:drawable="@drawable/a_2"
android:duration="100" />
</animation-list>
layoutAnimation
前几天遇到一个需求,将动画顺序改为左上到右下角展开,对某一组控件播放一样的动画,layoutAnimation可以实现
layout-animation可由xml和代码两种方式配置
-
XML配置
由于layout-animation是对于某一组控件的操作,就需要一个基本的动画来定义单个控件的动画。另外还可以定义动画的显示顺序和延迟:
在res目录中新建anim文件夹 ,在anim目录中新建一个my_anim.xm
- <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay="30%"
- android:animationOrder="reverse"
- android:animation="@anim/slide_right"/>
其中
-
android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。
-
android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。
-
android:animation指向了子控件所要播放的动画。
-
如果在xml中文件已经写好LayoutAnimation,可以使用AnimationUtils直接加载:AnimationUtils.loadLayoutAnimation(context, R.anim.my_anim)
但是LayoutAnimation默认只有三种顺序,即顺序逆序和随机,不能满足需求。去翻翻源码看它是怎么实现的,有没有提供方法自定义顺序?结果翻到了一个LayoutAnimationController#getTransformedIndex(AnimationParameters params)方法,返回值就是播放动画的顺序。并且这个方法是protected的,明显就是可由子类来扩展。既然找到了方法,那么就去实现它
- * custom LayoutAnimationController for playing child animation
- * in any order.
- *
- */
- public class CustomLayoutAnimationController extends LayoutAnimationController {
- // 7 just lucky number
- public static final int ORDER_CUSTOM = 7;
- private Callback onIndexListener;
- public void setOnIndexListener(OnIndexListener onIndexListener) {
- this.onIndexListener = onIndexListener;
- }
- public CustomLayoutAnimationController(Animation anim) {
- super(anim);
- }
- public CustomLayoutAnimationController(Animation anim, float delay) {
- super(anim, delay);
- }
- public CustomLayoutAnimationController(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- /**
- * override method for custom play child view animation order
- */
- protected int getTransformedIndex(AnimationParameters params) {
- if(getOrder() == ORDER_CUSTOM && onIndexListener != null) {
- return onIndexListener.onIndex(this, params.count, params.index);
- } else {
- return super.getTransformedIndex(params);
- }
- }
- /**
- * callback for get play animation order
- *
- */
- public static interface Callback{
- public int onIndex(CustomLayoutAnimationController controller, int count, int index);
- }
- }
通过复写getTransformedIndex方法,添加自定义顺序ORDER_CUSTOM,让callback自定义控件播放动画的顺序,即可以达到任何想要的效果
属性动画
- ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
- anim.setDuration(300);
- anim.start();