补间动画
Animation提供了AlphaAnimation,RotateAnimatio,TranslateAnimation,ScaleAnimation四种动画方式并提供Animationset动画集合。
AlphaAnimation
AlphaAnimation al = new AlphaAnimation(0,1);
al.setDuration(2000);
alpha.startAnimation(al);
RotateAnimation ro = new RotateAnimation(0,300,100,100);
ro.setDuration(2000);
rotate.setAnimation(ro);
TranslateAnimation tr = new TranslateAnimation(0,200,0,300);
tr.setDuration(2000);
translate.setAnimation(tr);
ScaleAnimation sc = new ScaleAnimation(0,2,0,2);
sc.setDuration(2000);
scale.setAnimation(sc);
AnimationSet setAnimation = new AnimationSet(true);
setAnimation.setDuration(2000);
AlphaAnimation als = new AlphaAnimation(0,1);
als.setDuration(2000);
setAnimation.addAnimation(als);
RotateAnimation ros = new RotateAnimation(0,300,100,100);
ros.setDuration(2000);
setAnimation.addAnimation(ros);
set.startAnimation(setAnimation);
xml 中定义动画
在 res中新建文件夹anim 定义文件名如my_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:repeatCount="0"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="2000"
/>
</set>
使用只需要
Animation anim = AnimationUtils.loadAnimation(this,R.anim.my_anim);
test_anim.startAnimation(anim);
- 列表内容translationX和 translationY:这两个属性作为一种增量来控制着View对象从它布局容器的左上角坐标便宜的位置。
- rotation、rotationX和rotationY:这三个属性控制View对象围绕支点进行2D和3D旋转
- scaleX和scaleY. 这两个属性控制着View对象围绕它的支点进行2D缩放。
- pivotX和pivotY:这两个属性控制着view对象的支点位置,围绕这个支点进行旋转和缩放变换处理,默认情况下,该支点的位置就是View对象的中心点。
- alpha:它表示View对象的alpha透明度,默认值是1(不透明),0代表完全透明(不可见)
- x和y这是两个简单实用的属性,它描述了View对象在它的容器中的最终位置,它是最初的左上角坐标和 translationX和 translationY值的累计和。
属性动画
ObjectAnimator
TextView tv_value_anim = (TextView) findViewById(R.id.tv_value_anim);
ObjectAnimator objAnimator = ObjectAnimator.ofFloat(tv_value_anim,"rotationY",360);
objAnimator.setDuration(2000);
objAnimator.start();
PropertyValuesHolder
这个类与补间动画中的AnimationSet类似,就是把动画给组合起来,在属性动画中,如果针对一个对象的多个属性,就同时需要多个动画了,可以使用PropertyValuesHolder,来实现,比如需要在平移的过程中,同时改变x,y的缩放,可以这样实现
PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("translationX",300f);
PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleX",1f,0,1f);
PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("scaleY",1f,0,1f);
ObjectAnimator.ofPropertyValuesHolder(tv_value_anim,pvh1,pvh2,pvh3).setDuration(2000).start();
ValueAnimator
ValueAnimator va = ValueAnimator.ofInt(0,100);
va.setTarget(tv_value_anim);
va.setDuration(2000).start();
va.setInterpolator(new DecelerateInterpolator()); // 插值器
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int values = (int) animation.getAnimatedValue();
tv_value_anim.setText(values+"");
}
});
xml中定义属性动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="5000"
android:propertyName="translationX"
android:valueFrom="100dp"
android:valueTo="200dp"/>
<objectAnimator
android:duration="5000"
android:propertyName="translationY"
android:valueFrom="100dp"
android:valueTo="200dp"/>
</set>
使用方法
animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.translation);
animator.setTarget(image);
animator.start();