Skip to content

Instantly share code, notes, and snippets.

@zjbpku
Created February 20, 2016 12:33
Show Gist options
  • Save zjbpku/5c1c0e9b3a379e41bafc to your computer and use it in GitHub Desktop.
Save zjbpku/5c1c0e9b3a379e41bafc to your computer and use it in GitHub Desktop.
Add goods to shopping cart with animations
public class AddToCartHelper {
@Inject
Context mContext;
/**
* @param v 移动对象
* @param fromXDelta 开始位置X轴偏移
* @param fromYDelta 开始位置Y轴偏移
* @param fx 开始移动时 v 的x坐标
* @param fy 开始移动时 v 的y坐标
* @param tx 移动结束时 v 的x坐标
* @param ty 移动结束时 v 的y坐标
*/
public void startAnimation(final View v, int fromXDelta, int fromYDelta, int fx, int fy, int tx, int ty, final AnimationListener listener) {
AnimationSet set = new AnimationSet(false);
TranslateAnimation translateAnimation = new TranslateAnimation(fromXDelta, tx - fx, fromYDelta, ty - fy);
translateAnimation.setInterpolator(new DecelerateInterpolator());
translateAnimation.setRepeatCount(0);
translateAnimation.setFillAfter(false);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f);
scaleAnimation.setFillAfter(false);
scaleAnimation.setInterpolator(new DecelerateInterpolator());
scaleAnimation.setRepeatCount(0);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.3f);
scaleAnimation.setFillAfter(false);
scaleAnimation.setInterpolator(new DecelerateInterpolator());
scaleAnimation.setRepeatCount(0);
set.addAnimation(alphaAnimation);
set.addAnimation(scaleAnimation);
set.addAnimation(translateAnimation);
set.setDuration(1000);
set.setFillAfter(false);
// v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
// v.setLayerType(View.LAYER_TYPE_NONE, null);
v.setVisibility(View.GONE);
if (listener != null) {
listener.onAnimationEnd();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
v.startAnimation(set);
}
/**
* @param activity
* @return
*/
public ViewGroup createAnimLayout(Activity activity) {
ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView();
LinearLayout animLayout = new LinearLayout(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
animLayout.setId(Integer.MAX_VALUE - 1);
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}
/**
* @param parent
* @param view
* @param location
* @return
*/
public View addViewToAnimLayout(final ViewGroup parent, final View view, int[] location, boolean wrap_content) {
if (view == null) return null;
int x = location[0];
int y = location[1];
LinearLayout.LayoutParams params;
if (wrap_content) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable == null) {
final int wh = mContext.getResources().getDimensionPixelSize(R.dimen.db_goods_wh);
params = new LinearLayout.LayoutParams(wh, wh);
} else {
params = new LinearLayout.LayoutParams(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
} else {
final int wh = mContext.getResources().getDimensionPixelSize(R.dimen.db_goods_wh);
params = new LinearLayout.LayoutParams(wh, wh);
}
params.leftMargin = x;
params.topMargin = y;
view.setLayoutParams(params);
return view;
}
public interface AnimationListener {
/**
* 处理动画结束后的逻辑,不要涉及动画相关的View
*/
void onAnimationEnd();
}
}
Usage:
int[] startXY = new int[2];
goodsImageView.getLocationInWindow(startXY);
int fx = startXY[0];
int fy = startXY[1];
final ImageView animImg = new ImageView(this);
animImg.setImageDrawable(goodsImageView.getDrawable());
ViewGroup anim_mask_layout = mAddToCartHelper.createAnimLayout(this);
anim_mask_layout.addView(animImg);
final View v = mAddToCartHelper.addViewToAnimLayout(anim_mask_layout, animImg, startXY, true);
if (v == null) return;
View cartView = findViewById(R.id.lfCart);
int[] endXY = new int[2];
cartView.getLocationInWindow(endXY);
int tx = endXY[0] + cartView.getWidth() / 2;
int ty = endXY[1] + cartView.getHeight() / 2;
mAddToCartHelper.startAnimation(v, 0, -animImg.getHeight() / 2, fx, fy, tx, ty, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment