官方文档
属性动画的一. 属性动画
1. ObjectAnimation
// 绕x轴旋转的度数。
ObjectAnimator.ofFloat(iv_ainm,"rotationX",0F,180F)
// 绕y轴旋转
ObjectAnimator.ofFloat(iv_ainm,"rotationY",0f,90f);
//绕着中心点旋转
ObjectAnimator.ofFloat(iv_ainm, "rotation", 0F, 180F);
// 沿着x轴位移
ObjectAnimator.ofFloat(iv_ainm,"translationX",200f);
// 沿着y轴位移
ObjectAnimator.ofFloat(iv_ainm,"translationY",800f);
//x轴缩放
ObjectAnimator.ofFloat(iv_ainm,"scaleX",1f,2f); // 放大两倍
// y轴缩放
ObjectAnimator.ofFloat(iv_ainm,"scaleY",1f,2f);
// alpha
ObjectAnimator.ofFloat(iv_ainm,"alpha",1f,0.1f);
//开启属性动画:
objectAnimator.setDuration(2000);
// 设置插值器。
objectAnimator.setInterpolator(new DecelerateInterpolator())
objectAnimator.start();
2. AnimatorSet
AnimatorSet set = new AnimatorSet();
// 一起播放
set.play(objectAnimator1).with(objectAnimator2);
// 在动画2之后动画1
set.play(objectAnimator1).after(objectAnimator2);
// 在动画2之前动画1
set.play(objectAnimator1).before(objectAnimator2);
3. ValueAnimator
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,200);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) iv_ainm.getLayoutParams();
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float fraction = animation.getAnimatedFraction();
Log.e(TAG,"fraction = " + fraction);
float value = (float) animation.getAnimatedValue();
Log.e(TAG,"value = " + value);
Log.e(TAG,"fraction * value = " + fraction*value);
layoutParams.width = (int) value; // 修改宽高。
layoutParams.topMargin = (int) value; // 修改外边距。
iv_ainm.setPadding(left,top,right,bottom); // 改变内边距。
iv_ainm.setLayoutParams(layoutParams);
}});
valueAnimator.setDuration(2000).start();
4. 抛物线的实现:
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,500);
valueAnimator.setDuration(2000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override
public void onAnimationUpdate(ValueAnimator animation) { iv_ainm.setTranslationX((float)animation.getAnimatedValue());
long time = animation.getCurrentPlayTime();
float g = 0.000125f;//500/2000/2000;
Log.e(TAG,"g = " + g);
int y = (int) ((g*time*time)/2);
Log.e(TAG,"time = " + time);
Log.e(TAG,"y = " + y);
iv_ainm.setTranslationY(y);
}});
valueAnimator.start();
5. TypeEvaluator
public class PointSinEvaluator implements TypeEvaluator {
@Override
public Pointevaluate(float fraction,Point startValue,Point endValue) {
floatx = startValue.x+ fraction * (endValue.x- startValue.x);
floaty = (float) (Math.sin(x * Math.PI/180) *100) + endValue.y/2;
Point point =newPoint((int) x,(int) y);
return point;
}
}
PointstartPoint =newPoint(0,0);
PointendPoint =newPoint(300,300);
ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointSinEvaluator(),startPoint,endPoint);
//valueAnimator.setEvaluator(newPointSinEvaluator());
valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Point point = (Point) animation.getAnimatedValue();
Log.e(TAG,"point.x = "+ point.x+" point.y = "+ point.y);
}
});
valueAnimator.start();
网友评论