最近在学习View动画这一块,在网上也找了一些资料,看了一点源码,下面就写一些自己在学习过程中的一点笔记。
首先,view动画有4种,AlphaAnimation(透明度)、ScaleAnimation(缩放)、TranslateAnimation(位移)、RotateAnimation(旋转)
还有一种是这四个动画的集合AnimationSet
他们可以在java代码中实现,也可以在XML中实现,下面附上代码
android:interpolator 插值器,影响动画播放的速度,如@android:anim/accelerate_interpolator表示动画播放的速度是加速的
android:shareInterpolator 集合中的动画是否与集合共享一个插值器,如果集合没有指定插值器,那么子动画就要自己指定自己的插值器或者使用默认值
android:fromAlpha 透明度的起始值, android:toAlpha 透明的的结束值 值类型为float,比如0.5
droid:fromDegrees 旋转的起始值 、android:toDegrees 旋转的结束值 值类型为float
droid:pivotX 、android:pivotY分别对应轴点的X、Y值 ,值为float表示相对于ABSOLUTE的值,fraction是分数、小数、百分比 ,比如50%表示相对于自己RELATIVE_OF_SELF,50%p(这个p是小写的)表示相对于父窗体RELATIVE_TO_PARENT
droid:fromXDelta、android:toXDelta、android:fromYDelta、android:toYDelta,比如把他们值分为100、0、100、0,表示把坐标从(x+100,y+100)移动到(x+0,y+0).
droid:duration 表示动画持续的事件
android:fillAfter 表示动画结束后是否停留在结束的位置
android:repeatCount 重复的次数 ,-1表示无线循环
android:repeatMode 重复的模式 restart:每次重复从起始开始 reverse:比如有两个状态A、B A->B、B->A、A->B ... .
android:startOffset 动画开启后推迟多少ms才显示该动画
动画位置在res/anim/anim_name.xml
<pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package]:anim/res_interpolator"
android:shareInterpolator="boolean" >
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<rotate
android:fromDegrees="float"
android:pivotX="float|fraction"
android:pivotY="float|fraction"
android:toDegrees="float" />
<translate
android:fromXDelta="float|fraction"
android:fromYDelta="float|fraction"
android:toXDelta="float|fraction"
android:toYDelta="float|fraction" />
<scale
android:duration="int"<!-- 单位ms -->
android:fromXScale="float|fraction"
android:fromYScale="float|fraction"
android:pivotX="float|fraction"
android:pivotY="float|fraction"
android:toXScale="float|fraction"
android:toYScale="float|fraction" />
</set>
在java代码中调用XML:
Animation animation01 = AnimationUtils.loadAnimation(this,
R.anim.scale02);
bt.startAnimation(animation01);
在java代码中定义view动画:
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE,
100, Animation.ABSOLUTE, 0);
translateAnimation.setDuration(500);
translateAnimation.setFillAfter(true);
bt.startAnimation(translateAnimation);
注意:
在view动画中设置FillAfter为ture后,动画会停留在结尾的位置,如果控件设置有点击事件,那么此时点击控件事件不能触发,因为控件动画变化的是控件的影子,但老位置仍然可以触发点击事件,但是在属性动画中,3.0以后的版本,属性动画是可以在控件动画执行后新的位置点击触发事件的,3.0以前的版本也是不可以的
接着说Animation动画,Animation的setAnimationListener方法可以给View动画添加过程监听的:
public static interface AnimationListener {
/**
* <p>Notifies the start of the animation.</p>
*
* @param animation The started animation.
*/
void onAnimationStart(Animation animation);
/**
* <p>Notifies the end of the animation. This callback is not invoked
* for animations with repeat count set to INFINITE.</p>
*
* @param animation The animation which reached its end.
*/
void onAnimationEnd(Animation animation);
/**
* <p>Notifies the repetition of the animation.</p>
*
* @param animation The animation which was repeated.
*/
void onAnimationRepeat(Animation animation);
}
通过接口可以看出每个方法的用法
最后,我们不仅可以使用google官方给我们的view动画,我们也可以自定义动画
public class MyAnimation extends Animation {
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
}
}
继承Animation然后重写里面的initialize()和applyTransformation()方法即可
initialize()进行一些初始化的工作
applyTransformation()中进行相应的矩阵变化,结合Camera和Matrix来简化矩阵的变化,可以参考android的Camera和Matrix
到此,应该说读者对View动画有一定的了解啦,如有不足的地方咱可以交流交流,相互学习!
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE,
100, Animation.ABSOLUTE, 0);
translateAnimation.setDuration(500);
translateAnimation.setFillAfter(true);
bt.startAnimation(translateAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE,
100, Animation.ABSOLUTE, 0);
translateAnimation.setDuration(500);
translateAnimation.setFillAfter(true);
bt.startAnimation(translateAnimation);