/** * * @param itemView * @param targetView */ public void animationItem(ImageView itemView, View targetView) { // 一,将imageview加入到根view中 final ImageView imageView = new ImageView(mContext); imageView.setImageDrawable(itemView.getDrawable()); imageWH = UiUtil.dip2px(mContext, 50); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWH, imageWH); // 开始和结束View的父View mRootRl.addView(imageView, layoutParams); // 计算动画开始和结束的点的坐标 // 获取父布局的起始点坐标 int[] parentLoc = new int[2]; mRootRl.getLocationInWindow(parentLoc); // 获取选中图片的坐标 int[] startLoc = new int[2]; itemView.getLocationInWindow(startLoc); // 获取预览区的RecyclerView的坐标 int[] endLoc = new int[2]; targetView.getLocationInWindow(endLoc); float startX = startLoc[0] - parentLoc[0] + itemView.getWidth() / 2; float startY = startLoc[1] - parentLoc[1] + itemView.getWidth() / 2; float toX = endLoc[0] - parentLoc[0]; float toY = endLoc[1] - parentLoc[1]; // 开始绘制贝塞尔曲线 Path path = new Path(); path.moveTo(startX, startY); // 使用二次贝塞尔曲线 path.quadTo((startX + toX) / 2, startY, toX, toY); pathMeasure = new PathMeasure(path, false); // 属性动画 ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength()); valueAnimator.setDuration(500); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); // 平移 pathMeasure.getPosTan(value, mCurrentPosition, null); imageView.setTranslationX(mCurrentPosition[0]); imageView.setTranslationY(mCurrentPosition[1]); // 缩放 // float fraction = animation.getAnimatedFraction(); // imageView.setScaleX(1 - fraction); // imageView.setScaleY(1 - fraction); } }); valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { mRootRl.removeView(imageView); // TODO other thing } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); valueAnimator.start(); }
加入购物车动画实现
最新推荐文章于 2024-08-07 16:12:37 发布