画应该是很基础的东西了,但是由于自己的记性不太好,总是记不住那些参数和方法,只是知道怎么用而已。所以每次用的时候都去百度(一般能出来)或者Google(基本出来了)。然而周末断网~你懂的....所以用周末时间重新看了一遍动画相关的文章,写下了笔记,温故而知新。
前提:
如果你想学习Android动画,知道它原理,实现什么的。如果你想掌握Android动画,熟练运用它什么的。那么可以看下面的几个我推荐的链接出门左转了~
如果你想和我一样偷懒,或者你想知道这个坑货到底写了什么笔记,或者你想花几分钟重新复习一下Android动画基础,那么下面的内容应该很适合你(我)。
那么开始~
View动画
View动画的属性
-set
|-android:interpolator -> 插值器,影响动画的速度
|-默认值 -> @android:anim/accelerate_decelerate_interpolator
|-android:shareInterpolator -> 集合所有动画是否使用同一插值器
|-android:fillAfter -> 动画结束后View是否停留在结束的位置
|-android:startOffset -> 动画多少秒之后执行
|-android:repeatMode -> 重复的模式,默认为restart,即重头开始重新运行,reverse即从结束开始向前重新运行
-TranslateAnimation -> 移动View
|-<translate>
|-android:fillAfter ->
|-android:duration -> 表示动画持续的时间
|-android:fromXDelta -> 表示 x 的起始值
|-android:toXDelta -> 表示 x 的结束值
|-android:fromYDelta -> 表示 y 的起始值
|-android:toYDelta -> 表示 y 的结束值
-scaleAnimation -> 放大或者缩小View
|-<scale>
|-android:duration -> 表示动画持续的时间
|-android:fromXScale -> 表示水平方向缩放的起始值
|-android:fromYScale -> 表示竖直方向缩放的起始值
|-android:pivotX -> 表示缩放中心点的 X 坐标
|-android:pivotY -> 表示缩放中心点的 Y 坐标
|-android:toXScale -> 表示水平方向缩放的结束值
|-android:toYScale -> 表示竖直方向缩放的结束值
-RotateAnimation -> 旋转View
|-<rotate>
|-android:duration -> 表示动画持续的时间
|-android:fromDegrees -> 旋转开始的角度
|-android:toDegrees -> 旋转结束的角度
|-android:pivotX -> 旋转中心点的 X 坐标
|-android:pivotY -> 旋转中心点的 Y 坐标
-AlphaAnimation -> 改变View的透明度
|-<alpha>
|-android:duration -> 表示动画持续的时间
|-android:fromAlpha -> 透明度的起始值
|-android:toAlpha -> 透明度的结束值
-自定义View动画 -> (不会,待实践中学习)
View动画在XML中设置
xml文件地址
res/anim/filename.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true"
android:fillAfter="true">
<translate
android:duration="100"
android:fromXDelta="0.0f"
android:fromYDelta="0.0f"
android:toXDelta="100.0f"
android:toYDelta="100.0f" />
<scale
android:fromXScale="0.0f"
android:fromYScale="0.0f"
android:pivotX="0.0f"
android:pivotY="0.0f"
android:toXScale="1.0f"
android:toYScale="1.0f" />
<rotate
android:fromDegrees="0.0f"
android:pivotX="0.0f"
android:pivotY="0.0f"
android:toDegrees="30.0f" />
<alpha
android:fromAlpha="0.0f"
android:toAlpha="1.0f" />
</set>
View动画的使用
View mView = findViewById(R.id.mView);
Animation mAnimation = AnimationUtils.loadAnimation(this,R.anim.slide_bottom_in);
mView.startAnimation(mAnimation);
View动画的在Java中设置并使用
AlphaAnimation mAlphaAnimation = new AlphaAnimation(0 , 1);
mAlphaAnimation.setDuration(300);
mView.startAnimation(mAlphaAnimation);
mAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重新运行
}
});
Frame动画
Frame动画的属性
animation-list -> 帧动画列表
|-android:oneshot -> true表示动画只播放一次停止在最后一帧上,false表示动画循环播放
|-item -> 帧
|-android:drawable -> 表示每一帧的值
|-android:duration -> 表示每一帧停留的时间
Frame动画的XML设置
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/image1" android:duration="500" />
<item android:drawable="@drawable/image2" android:duration="500" />
<item android:drawable="@drawable/image3" android:duration="500" />
</animation-list>
Frame动画的使用
mView.setBackgroundResource(R.drawable.frame);
AnimationDrawable mAnimationDrawable = (AnimationDrawable) mView.getBackground();
mAnimationDrawable.start();
Layout动画
Layout动画的属性
LayoutAnimation -> 布局动画
|-android:animation -> 孩子执行的动画资源
|-android:animationOrder -> 子元素执行的顺序
|-normal -> 顺序执行
|-reverse -> 逆序执行
|-
|-android:delay -> 子元素开始动画的延迟(每一个动画在上一个动画结束后延迟ms后执行)
Layout动画的XML设置
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/slide_right"
android:animationOrder="normal"
android:delay="30%" />
Layout动画的使用
<ListView
android:layoutAnimation="@anim/list_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
//通过加载XML动画设置文件来创建一个Animation对象;
Animation animation=AnimationUtils.loadAnimation(this, R.anim.list_anim);
//得到一个LayoutAnimationController对象;
LayoutAnimationController mLayoutAnimationController = new LayoutAnimationController(animation);
//设置控件显示的顺序;
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_REVERSE);
//设置控件显示间隔时间;
mLayoutAnimationController.setDelay(1);
//为ListView设置LayoutAnimationController属性;
mList.setLayoutAnimation(mLayoutAnimationController);
Activity动画
Activity动画的属性
overridePendingTransition(enterAnim, exitAnim);
|-enterAnim -> 进入动画资源id, 0表示不使用
|-exitAnim -> 退出动画资源id, 0表示不使用
Activity动画的使用方法
Intent mIntent = new Intent(this, TestActivity.class);
startActivity(mIntent);
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
}
PS:一定要在startActivity或者finishd的后面调用
Property动画
ObjectAnimator.ofFloat(mView, "translationY", 100).start();//默认时间内让mView在Y轴上平移100个像素
//3秒中内改变mView的背景颜色从0xffffffff到0xff000000
ValueAnimator colorAnim = ObjectAnimator.ofInt(mView, "backgroundColor", 0xffffffff, 0xff000000);
colorAnim.setDuration(3000);//动画执行时间
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);//无限循环
colorAnim.setRepeatMode(ValueAnimator.REVERSE);//翻转执行
colorAnim.start();
//--------
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(mView, "rotationX", 0, 360),
ObjectAnimator.ofFloat(mView, "rotationY", 0, 180),
ObjectAnimator.ofFloat(mView, "rotation", 0, 90),
ObjectAnimator.ofFloat(mView, "translationX", 0, 90),
ObjectAnimator.ofFloat(mView, "translationY", 0, 90),
ObjectAnimator.ofFloat(mView, "scaleX", 1, 1.5f),
ObjectAnimator.ofFloat(mView, "scaleY", 1, 1.5f),
ObjectAnimator.ofFloat(mView, "alpha", 1, 1.5f, 1)
);
set.setDuration(5000).start();
PS:除了"rotationX","rotationY",上面出现的这些可以使用,还有哪些可以用呢?
只要你的View有setXXX()方法就可以用。
比如View中有setAlpha(float value)
和getAlpha()
,所以你就可以传入alpha
了。
XML定义属性动画
res/animator/property.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<objectAnimator
android:duration="3000"
android:propertyName="rotationX"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:startOffset="0"
android:valueFrom="0"
android:valueTo="360"
android:valueType="intType" />
<animator
android:duration="3000"
android:repeatCount="1"
android:repeatMode="restart"
android:startOffset="0"
android:valueFrom="0"
android:valueTo="100"
android:valueType="intType" />
</set>
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.property_animator);
set.setTarget(mView);
set.start();
<set> = AnimatorSet
<animator> = ValueAnimator
<objectAnimator>= ObjectAnimator
objectAnimator
|-android:propertyName -> 表示属性动画作用的对象的属性的名称
|-android:duration -> 表示动画执行的时间
|-android:repeatCount -> 表示动画执行次数
|-android:repeatMode -> 表示动画执行类型
|-android:startOffset -> 表示动画执行延迟时间
|-android:valueFrom -> 表示属性执行初始值
|-android:valueTo -> 表示属性执行结束值
|-android:valueType -> 表示动画执行数值的类型
PS:属性动画设置生效的锚点是对View设置,例如修改锚点位置为View右上角:
mView.setPivotX(0f);
mView.setPivotY(mView.getWidth());
属性动画生效的前提:
1.对象必须提供 setXXX 方法(XXX为属性名称),还要提供有 getXXX 方法(如果你的动画没有传递初始值的话)。
2.对象的 setXXX 方法会对属性 XXX 进行某种UI改变,比如背景颜色。
问:属性动画生效怎么调用getXXX方法的?
答:反射
插值器和估值器
TimeInterpolator
|-LinearInterpolator -> 线性插值器,匀速运动
|-AccelerateDecelerateInterpolator -> 加速减速插值器,动画两头慢,中间快
|-DecelerateInterpolator -> 减速插值器,动画越来越慢
|-more
TypeEvaluator
|-IntEvaluator
|-FloatEvaluator
|-ArgbEvaluator -> 针对Color属性
Android L Animation
有待实践中学习(http://www.androiddesignpatterns.com/2014/12/activity-fragment-transitions-in-android-lollipop-part1.html)
Android L Animation
|-Touch feedback -> 触摸反馈
|-Ripple -> 波纹效果
|-Reveal effect -> 揭露效果
|-ViewAnimationUtils.createCircularReveal() -> 创建方法
|-centerX -> 动画执行中心 X 轴
|-centerY -> 动画执行中心 Y 轴
|-startRadius -> 动画开始的半径值
|-endRadius -> 动画结束的半径值
|-Transition -> 转换效果
|-Activity transitions -> Activity转换效果
|-TransitionManager
|-(方法)beginDelayedTransition
|-(参数)ViewGroup sceneRoot -> 根布局
|-(参数)Transition transition -> 默认动画
|-Visibility -> View显示或者隐藏时候的动画效果
|-Explode -> 爆炸展开效果
|-Fade -> 渐变效果
|-Slide -> 滑动效果
|-Curved motion -> 曲线运动 (待补充)
|-View state changes -> 视图状态改变 (待补充)
|-Animate Vector Drawables -> 可绘矢量动画 (待补充)
Ripple (波纹效果)
android:colorControlHighlight="#ff00ff" //波纹颜色
android:colorAccent="#ffff00" //设置checkbox等控件的选中颜色
android:background="?android:attr/selectableItemBackground" //波纹有边界
android:background="?android:attr/selectableItemBackgroundBorderless" //波纹超出边界
Circular Reveal (揭露效果)
mButton = findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(
mButton,
0,
0,
0,
(float) Math.hypot(mButton.getWidth(), mButton.getHeight()));
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(2000);
animator.start();
}
});
使用注意问题
1.OOM问题
帧动画使用图片过大容易OOM。
2.内存泄漏
当有些属性动画是无限运行的,比如转圈~,这类动画要在Activity的onPause()中及时暂停!
3.是用View动画后无法隐藏
setVisibility(View.GONE)失效,使用clearAnimation()消除View动画。
4.点击问题
View动画新位置无法触发点击事件,属性动画旧位置无法触发点击事件。(版本也有些区别,需要注意)
Whitelaning
It's very easy to be different but very difficult to be better
原文链接:http://www.jianshu.com/p/b7aa2a4a9787
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。