您的位置:首页 > 移动开发 > Android开发

Android 属性动画备忘 nineold

2015-11-25 17:11 651 查看
项目开发偶尔会用到动画,每次都得找以前的代码复制,太麻烦了,但又记不住,只好写在这.
ObjectAnimator.ofFloat(view, "translationX", 0, 50, -50, 0).setDuration(duration).start();//位移
ObjectAnimator.ofFloat(view, "translationY", 0, 50, -50, 0).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "scaleX", 1, 2, 1).setDuration(duration).start();//缩放
ObjectAnimator.ofFloat(view, "scaleY", 1, 2, 1).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "alpha", 1, 0, 1).setDuration(duration).start();//透明度
ObjectAnimator.ofFloat(view, "rotationX", 0, 180, 0).setDuration(duration).start();//翻转
ObjectAnimator.ofFloat(view, "rotationY", 0, 180, 0).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "rotation", 0, 180, 0).setDuration(duration).start();//旋转
ViewHelper.setPivotX(view, view.getWidth() / 2f);//设置动画基点
ViewHelper.setPivotY(view, view.getHeight() / 2f);
以上就是比较常用的nineold,但是偶尔遇到组合的,这样就比较省事
AnimatorSet animator = new AnimatorSet();
animator.playTogether(
ObjectAnimator.ofFloat(image, "rotation", 0, 1080),
ObjectAnimator.ofFloat(image, "translationX", 0, 180),
ObjectAnimator.ofFloat(image, "translationY", 0, -180),
ObjectAnimator.ofFloat(image, "scaleX", 1, 0),
ObjectAnimator.ofFloat(image, "scaleY", 1, 0)
ObjectAnimator.ofFloat(image, "alpha", 1, 0.25f, 1)
);
animator.setDuration(5 * 1000).start();
今天做一个添加商品到购物车时 商品图片旋转 缩小 位移 到指定的购物车图标位置,用的属性动画,很麻烦,动画执行顺序如果弄乱了,位移轨迹会出问题的.大概记录一下,以免忘了

int[] endLocation = new int[2];
topSpcart.getLocationInWindow(endLocation);// shopCart是那个购物车

// 计算位移
int endX = endLocation[0] - startLocation[0] ;// 动画位移的X坐标
int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标

RotateAnimation rotateAnimation = new RotateAnimation(0, 1080,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ScaleAnimation scaleAnimation =new ScaleAnimation(1, 0, 1, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
TranslateAnimation translateAnimation = new TranslateAnimation(0,endX, 0, endY);
//		translateAnimation.setInterpolator(new LinearInterpolator());//动画加速器
//		translateAnimation.setInterpolator(new AccelerateInterpolator());
//		translateAnimation.setRepeatCount(0);// 动画重复执行的次数
//		translateAnimation.setFillAfter(true);//动画结束留在原位置

AnimationSet set = new AnimationSet(false);
set.setFillAfter(true);
set.addAnimation(rotateAnimation);//旋转
set.addAnimation(scaleAnimation);//缩放
set.addAnimation(translateAnimation);//位移
set.setDuration(1000);// 动画的执行时间
view.startAnimation(set);
// 动画监听事件
set.setAnimationListener(new AnimationListener() {
// 动画的开始
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
// 动画的结束
@Override
public void onAnimationEnd(Animation animation) {
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画 Android nineold