动画
补间动画
//透明度动画
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1)
//平移动画
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 0);
//缩放动画
ScaleAnimation rotateAnimation = new ScaleAnimation(1, 2, 1, 2,50,50);
//旋转动画
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, 0, 0);
方法
常用的几个
//动画持续时间
alphaAnimation.setDuration(2000);
/是否保存最后状态
alphaAnimation.setFillAfter(true);
//重复次数
alphaAnimation.setRepeatCount(2);
//是否回访
alphaAnimation.setRepeatMode(Animation.REVERSE);
//插值器
alphaAnimation.setInterpolator(new BounceInterpolator());
//控件开始播放
im.startAnimation(alphaAnimation);
插值器
//集合动画
AnimationSet animationSet = new AnimationSet(true)
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.scale);
animationSet.addAnimation(animation);
animationSet.addAnimation(animation2);
img.startAnimation(animationSet);
res资源文件 创建anim set.xm
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fillAfter="true"
android:fillBefore="true"
>
<rotate android:pivotX="0.5" android:pivotY="0.5" android:fromDegrees="0" android:toDegrees="180"></rotate>
<translate android:fromYDelta="0" android:toYDelta="200" android:fromXDelta="0" android:toXDelta="0"></translate>
</set>
帧动画(动图)
现将需要的图片存入drawable里然后创建动画集合animation-list
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/anim1" android:duration="100"></item>
<item android:drawable="@drawable/anim2" android:duration="100"></item>
<item android:drawable="@drawable/anim3" android:duration="100"></item>
<item android:drawable="@drawable/anim4" android:duration="100"></item>
<item android:drawable="@drawable/anim5" android:duration="100"></item>
<item android:drawable="@drawable/anim6" android:duration="100"></item>
<item android:drawable="@drawable/anim7" android:duration="100"></item>
<item android:drawable="@drawable/anim8" android:duration="100"></item>
<item android:drawable="@drawable/anim9" android:duration="100"></item>
<item android:drawable="@drawable/anim10" android:duration="100"></item>
<item android:drawable="@drawable/anim11" android:duration="100"></item>
<item android:drawable="@drawable/anim12" android:duration="100"></item>
</animation-list>
im.setImageResource(R.drawable.list);
AnimationDrawable drawable = (AnimationDrawable) im.getDrawable();
drawable.start();
属性动画(ValueAnimator)
//ofInt() 变化值的方法 不是固定的
//第一个参数btn.getWidth()当前控件的宽度
//第二个参数 你要估算变化的值
ValueAnimator valueAnimator = ValueAnimator.ofInt(btn.getWidth(), 500);
//动画时间
valueAnimator.setDuration(5000);
//监听器
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//获取改变的值
int animatedValue = (int) animation.getAnimatedValue();
//把值赋给控件
btn.getLayoutParams().width=animatedValue;
//刷新控件
btn.requestLayout();
}
});
//开始动画
valueAnimator.start();
自定义动画
View a;
//看源码ctrl+g转三万 缩放 后两个参数本身放大两倍
ObjectAnimator scaleX = ObjectAnimator.ofFloat(btn, "scaleX", 1, 2);
scaleX.setDuration(2000);
scaleX.start();
ObjectAnimator scaleY = ObjectAnimator.ofFloat(btn, "scaleY", 1, 2);
scaleY.setDuration(2000);
scaleY.start();
//旋转
ObjectAnimator rotation = ObjectAnimator.ofFloat(btn, "rotation", 0, 360, 0, 0);
rotation.setDuration(2000);
rotation.start();
//平移
ObjectAnimator translationX = ObjectAnimator.ofFloat(btn, "translationX", 0, 200);
ObjectAnimator translationY = ObjectAnimator.ofFloat(btn, "translationY", 0, 200);
translationX.setDuration(2000);
translationY.setDuration(2000);
translationX.start();
translationY.start();
//透明度
ObjectAnimator alpha = ObjectAnimator.ofFloat(btn, "alpha", 1, 0);
alpha.setDuration(2000);
//监听器
alpha.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//开始
}
@Override
public void onAnimationEnd(Animator animation) {
//结束
}
@Override
public void onAnimationCancel(Animator animation) {
//取消
}
@Override
public void onAnimationRepeat(Animator animation) {
//重复
}
});
alpha.start();
自定义组合动画
AnimatorSet animatorSet = new AnimatorSet();
/* ObjectAnimator scaleX = ObjectAnimator.ofFloat(btn, "scaleX", 1, 2);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(btn, "scaleY", 1, 2);*/
ObjectAnimator translationX = ObjectAnimator.ofFloat(btn, "translationX", 0, 200);
ObjectAnimator translationY = ObjectAnimator.ofFloat(btn, "translationY", 0, 200);
animatorSet.setDuration(2000);
//animatorSet.playSequentially(translationX,translationY);//先播放前面的后播放后面的
animatorSet.playTogether(translationX,translationY);//一起播放
animatorSet.start();