国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用動畫動態添加商品進購物車

2019-10-21 21:48:54
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android添加商品進購物車的具體代碼,供大家參考,具體內容如下

1、首先展示下效果圖

Android,動畫,購物車

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武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌县| 平原县| 罗甸县| 龙岩市| 鄂伦春自治旗| 嘉善县| 阿巴嘎旗| 平武县| 扶余县| 改则县| 中西区| 江永县| 绵阳市| 诸城市| 江山市| 呼图壁县| 密云县| 塔城市| 论坛| 灵川县| 黄浦区| 曲阜市| 曲水县| 清镇市| 五大连池市| 且末县| 鄢陵县| 湟源县| 宁安市| 海丰县| 甘南县| 凤庆县| 平凉市| 同江市| 富民县| 灵台县| 略阳县| 阳新县| 武宁县| 花莲县| 博爱县|