本文實(shí)例為大家分享了Android添加商品進(jìn)購(gòu)物車的具體代碼,供大家參考,具體內(nèi)容如下
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、松溪網(wǎng)站維護(hù)、網(wǎng)站推廣。
1、首先展示下效果圖
2、講一下思路,小球由加號(hào)位置運(yùn)動(dòng)到購(gòu)物車位置,首先得獲得這兩個(gè)點(diǎn)在整個(gè)屏幕中的坐標(biāo),然后分別計(jì)算這兩個(gè)點(diǎn)的橫縱坐標(biāo)的差值,再通過TranslateAnimation這個(gè)類設(shè)置小球在X、Y方向上的偏移量,最后通過AnimationSet這個(gè)類將這兩個(gè)動(dòng)畫放在一起執(zhí)行。這是小球運(yùn)動(dòng)的動(dòng)畫,還有就是購(gòu)物車變大縮小的動(dòng)畫。這個(gè)動(dòng)畫通過ObjectAnimator的ofFloat的方法設(shè)置縮放,要注意的是當(dāng)小球落下的時(shí)候,購(gòu)物車才開始動(dòng)畫,所以要設(shè)置一下setStartDelay這個(gè)方法。
3、具體的代碼我就貼一下動(dòng)畫部分的代碼,如果想要這個(gè)Demo看下我最后貼出的Github的地址
@Override public void setAnim(View view) { // TODO Auto-generated method stub int[] start_location = new int[2];// 一個(gè)整型數(shù)組用來存儲(chǔ)按鈕在屏幕的X,Y坐標(biāo) view.getLocationInWindow(start_location);// 購(gòu)買按鈕在屏幕中的坐標(biāo) buyImg = new ImageView(this);// 動(dòng)畫的小圓圈 buyImg.setImageResource(R.drawable.sign);// 設(shè)置buyImg的圖片 setAnim(buyImg, start_location); } /** * hdh: 創(chuàng)建動(dòng)畫層 * * @return */ private ViewGroup createAnimLayout() { ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();// 獲得Window界面的最頂層 LinearLayout animLayout = new LinearLayout(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); animLayout.setLayoutParams(lp); //animLayout.setId(); animLayout.setBackgroundResource(android.R.color.transparent); rootView.addView(animLayout); return animLayout; } /** * hdh: * * @param vp * @param view * @param location * @return */ private View addViewToAnimLayout(final ViewGroup vp, 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; } /** * hdh:動(dòng)畫 * * @param v * @param start_location */ private void setAnim(final View v, int[] start_location) { anim_mask_layout = null; anim_mask_layout = createAnimLayout(); anim_mask_layout.addView(v); View view = addViewToAnimLayout(anim_mask_layout, v, start_location); int[] end_location = new int[2];// 存儲(chǔ)動(dòng)畫結(jié)束位置的X,Y坐標(biāo) text_chart_num.getLocationInWindow(end_location);// 將購(gòu)物車的位置存儲(chǔ)起來 // 計(jì)算位移 int endX = end_location[0] - start_location[0];// 動(dòng)畫位移的X坐標(biāo) int endY = end_location[1] - start_location[1];// 動(dòng)畫位移的y坐標(biāo) TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator());// 設(shè)置此動(dòng)畫的加速曲線。默認(rèn)為一個(gè)線性插值。 translateAnimationX.setRepeatCount(0);// 動(dòng)畫重復(fù)的次數(shù) translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0);// 動(dòng)畫重復(fù)次數(shù) translateAnimationY.setFillAfter(true); AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationX); set.addAnimation(translateAnimationY); set.setDuration(1000); view.startAnimation(set); set.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub v.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub v.setVisibility(View.GONE); } }); ObjectAnimator anim = ObjectAnimator// .ofFloat(view, "scale", 1.0F, 1.5F, 1.0f)// .setDuration(500);// anim.setStartDelay(1000); anim.start(); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float cVal = (Float) animation.getAnimatedValue(); image_chart.setScaleX(cVal); image_chart.setScaleY(cVal); text_chart_num.setScaleX(cVal); text_chart_num.setScaleY(cVal); } }); }
4、GitHub地址:點(diǎn)擊打開鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。