View Animation 相对简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画
Drawable Animation 这个一帧一帧的替换,开发过程中几乎不用。不讲了
Property Animation 属性动画 android 3.0 以后有的。
View Animation
平移:
在res 下新建anim 文件
新建 animation resource file 然后文件中出现成对的set
添加如下代码
<translate android:fromXDelta="0" android:toXDelta="300" android:duration="1000"/>
其中duration 是动画持续的毫秒数
在java代码中使用
AnimationUtils.LoadAnimation(context , animId );
context 上下文 animId 是 动画xml资源id
view.setAnimation(AnimationUtils.loadAnimation(this , R.anim.translate));
实现
渐变操作
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
/>
旋转操作
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="520"
android:toDegrees="3600"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="1000"
android:fillAfter="true"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
/>
缩放图片
<scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:duration="1000"/>
也可以在代码中实现
// 渐变动画
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation .setRepeatCount(1); // 如果为0 表示不重复 大于0 重复 大于的次数
alphaAnimation .setRepeatMode(Animation.REVERSE); //**Defines what this animation should do when it reaches the end** 动画到尽头了应该做什么。
alphaAnimation .setFillAfter(true); //默认false if fillAfter is true, the transformation that this animation performed will persist when it is finishe
alphaAnimation .setDuration(1000); // 设置时间
//缩放动画
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation .setRepeatCount(1); // 重复复次数
scaleAnimation .setRepeatMode(Animation.REVERSE);// 同上
scaleAnimation .setFillAfter(true); // 同上
scaleAnimation .setDuration(1000); //同上
//位移动画
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation .setRepeatCount(1);
translateAnimation .setRepeatMode(Animation.REVERSE);
translateAnimation .setFillAfter(true);
translateAnimation .setDuration(1000);
//旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation .setRepeatCount(1);
rotateAnimation .setRepeatMode(Animation.REVERSE);
rotateAnimation .setFillAfter(true);
rotateAnimation .setDuration(1000);
帧动画 跳过
Android 3.0后 加入属性动画。
首先介绍
ObjectAnimator
这个用法相对简单
X轴旋转的方法
ObjectAnimator//
.ofFloat(view, "rotationX", 0.0F, 360.0F)//
.setDuration(500)//
.start();
Y轴旋转的方法
ObjectAnimator//
.ofFloat(view, "rotationY", 0.0F, 360.0F)//
.setDuration(500)//
.start();
注意源码中 ObjectAnimator 中有三个方法 ofFloat ofInt ofObject 三个方法。
源码中 ofFloat(Object target, String propertyName, float… values)
其中target 一般穿 view propertyName 是动画的tpyename float… 表示后边可以加多个valuse
现在看 ValueAnimator
源码中public static ValueAnimator ofFloat(float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
return anim;
}
意思说可以传很多变化的值
比如我传的
ValueAnimator animator = ValueAnimator.ofFloat(0, 1f ,0.5f , 0.9f, 0.0f);
animator.setTarget(button);
animator.setInterpolator(value) // *
animator.setDuration(1000).start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.d("ddd" , animation.getAnimatedValue()+"");
// button.setScaleX((Float) animation.getAnimatedValue());
// button.setScaleY((Float) animation.getAnimatedValue());
// button.setAlpha((Float) animation.getAnimatedValue());
// button.setTranslationX((Float) animation.getAnimatedValue()*100);
// button.setTranslationY((Float) animation.getAnimatedValue()*100);
button.setRotation((Float) animation.getAnimatedValue()*360);
}
});
上边写的不规范不用在意这些细节啊,
上边表示 先转360度 然后倒转 180度 再转 到324度 最后转回 初始位置
其他的 渐变 尺寸改变 位移 跟他是一样的道理。
上边 注释为* 的地方 是 下述对象。都有解释
AccelerateInterpolator ? ? 加速,开始时慢中间加速
DecelerateInterpolator ? 减速,开始时快然后减速
AccelerateDecelerateInterolator ? 先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator ?反向 ,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator ?跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator ?循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator ?线性,线性均匀改变
OvershottInterpolator ?超越,最后超出目的值然后缓慢改变到目的值
TimeInterpolator 一个接口,允许你自定义interpolator,以上几个都是实现了这个接口
ViewPropertyAnimator
private void propertyValueHolder(View view){
PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("alpha", 1f,
0f, 1f);
PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("scaleX", 1f,
0f, 1f);
PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleY", 1f,
0f, 1f);
ObjectAnimator.ofPropertyValuesHolder(view , propertyValuesHolder1 ,propertyValuesHolder2 , propertyValuesHolder3).setDuration(2000).start();
}
注意 里面的字符串scaleX scaleY 都不能错。错了不能识别报异常。
最后看
AnimatorSet
AnimatorSet animatorSet = new AnimatorSet();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(360);
valueAnimator.setDuration(2000);
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mImageView.setRotationY((Float) animation
.getAnimatedValue());
}
});
animatorSet.play(valueAnimator).with(
ObjectAnimator.ofFloat(v, RotationY, 360)
.setDuration(2000));
// 两种可以同时执行
animSet.playTogether(anim1, anim2);
animatorSet.start();
参考:http://www.2cto.com/kf/201411/353170.html
http://blog.csdn.net/lmj623565791/article/details/38067475/