實現加入購物車動畫
阿新 • • 發佈:2019-01-09
最近一直在總結之前寫過的一些功能,今年就記錄一下之前實現的加入購物車有動畫
1.點選的位置是開始位置
2.購物車圖片是結束位置
3.點選加入的時候獲取開始位置座標,並獲取動畫圖片
4.加入購物車成功後,開始傳送handle 執行動畫
下面直接上程式碼:
private int[] startLocation; private ImageView ball;// 小圓點 private ViewGroup anim_mask_layout;//動畫層 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: setAnim(ball, startLocation);// 開始執行動畫 break; case 4: int page1 = (Integer) msg.obj; viewPager.setCurrentItem(page1); break; } } };
//彈出動畫 startLocation = new int[2];// 一個整型陣列,用來儲存按鈕的在螢幕的X、Y座標 view.getLocationInWindow(startLocation);// 這是獲取購買按鈕的在螢幕的X、Y座標(這也是動畫開始的座標) ball = new ImageView(MakerApplyProductActivity.this);// buyImg是動畫的圖片,我的是一個小球(R.drawable.sign) ball.setLayoutParams(new ViewGroup.LayoutParams(80, 80)); // ball.setImageResource(R.drawable.mall_cart);// 設定buyImg的圖片 GlideUtils.loadPic(MakerApplyProductActivity.this, item.get商品主圖(), ball);
new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); msg.arg1 = 0; msg.obj = tab; // handler.sendEmptyMessage(0); handler.sendMessage(msg); } }).start(); }
private void setAnim(final View v, int[] startLocation) {
anim_mask_layout = null;
anim_mask_layout = createAnimLayout();
anim_mask_layout.addView(v);//把動畫小球新增到動畫層
final View view = addViewToAnimLayout(anim_mask_layout, v,
startLocation);
int[] endLocation = new int[2];// 儲存動畫結束位置的X、Y座標
// tab.getTabAt(tab.getSelectedTabPosition() + 1).getLocationInWindow(endLocation);// re_zhongcai_tanchu是那個拋物線最後掉落的控制元件
tab.getLocationInWindow(endLocation);
// 計算位移
int endX = 0 - startLocation[0] + 250;// 動畫位移的X座標
int endY = endLocation[1] - startLocation[1];// 動畫位移的y座標
TranslateAnimation translateAnimationX = new TranslateAnimation(0,
endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());
translateAnimationX.setRepeatCount(0);// 動畫重複執行的次數
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);// 動畫重複執行的次數
translateAnimationX.setFillAfter(true);
final AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
set.addAnimation(translateAnimationY);
set.addAnimation(translateAnimationX);
set.setDuration(1000);// 動畫的執行時間
view.startAnimation(set);
// 動畫監聽事件
set.setAnimationListener(new Animation.AnimationListener() {
// 動畫的開始
@Override
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
// Log.e("動畫","asdasdasdasd");
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
// 動畫的結束
@Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
set.cancel();
animation.cancel();
// anim_mask_layout.removeView(v);
}
});
}
/**
* @param
* @return void
* @throws
* @Description: 建立動畫層
*/
private ViewGroup createAnimLayout() {
// final Dialog dialog = new Dialog(context, R.style.dialogStyle);
// Window window = dialog.getWindow();
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
// if(rootView != null) rootView.removeAllViews();
LinearLayout animLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
animLayout.setId(Integer.MAX_VALUE);
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}
private View addViewToAnimLayout(final ViewGroup parent, final View view,
int[] location) {
int x = location[0];
int y = location[1];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin = x;
lp.topMargin = y;
view.setLayoutParams(lp);
return view;
}