本文實例為大家分享了Android添加商品進購物車的具體代碼,供大家參考,具體內容如下
1、首先展示下效果圖
2、講一下思路,小球由加號位置運動到購物車位置,首先得獲得這兩個點在整個屏幕中的坐標,然后分別計算這兩個點的橫縱坐標的差值,再通過TranslateAnimation這個類設置小球在X、Y方向上的偏移量,最后通過AnimationSet這個類將這兩個動畫放在一起執行。這是小球運動的動畫,還有就是購物車變大縮小的動畫。這個動畫通過ObjectAnimator的ofFloat的方法設置縮放,要注意的是當小球落下的時候,購物車才開始動畫,所以要設置一下setStartDelay這個方法。
3、具體的代碼我就貼一下動畫部分的代碼,如果想要這個Demo看下我最后貼出的Github的地址
@Override public void setAnim(View view) { // TODO Auto-generated method stub int[] start_location = new int[2];// 一個整型數組用來存儲按鈕在屏幕的X,Y坐標 view.getLocationInWindow(start_location);// 購買按鈕在屏幕中的坐標 buyImg = new ImageView(this);// 動畫的小圓圈 buyImg.setImageResource(R.drawable.sign);// 設置buyImg的圖片 setAnim(buyImg, start_location); } /** * hdh: 創建動畫層 * * @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:動畫 * * @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];// 存儲動畫結束位置的X,Y坐標 text_chart_num.getLocationInWindow(end_location);// 將購物車的位置存儲起來 // 計算位移 int endX = end_location[0] - start_location[0];// 動畫位移的X坐標 int endY = end_location[1] - start_location[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);// 動畫重復次數 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地址:點擊打開鏈接
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答