欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē)

 更新時(shí)間:2018年06月30日 15:09:04   作者:android_hdh  
這篇文章主要為大家詳細(xì)介紹了Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android添加商品進(jìn)購(gòu)物車(chē)的具體代碼,供大家參考,具體內(nèi)容如下

1、首先展示下效果圖

2、講一下思路,小球由加號(hào)位置運(yùn)動(dòng)到購(gòu)物車(chē)位置,首先得獲得這兩個(gè)點(diǎn)在整個(gè)屏幕中的坐標(biāo),然后分別計(jì)算這兩個(gè)點(diǎn)的橫縱坐標(biāo)的差值,再通過(guò)TranslateAnimation這個(gè)類(lèi)設(shè)置小球在X、Y方向上的偏移量,最后通過(guò)AnimationSet這個(gè)類(lèi)將這兩個(gè)動(dòng)畫(huà)放在一起執(zhí)行。這是小球運(yùn)動(dòng)的動(dòng)畫(huà),還有就是購(gòu)物車(chē)變大縮小的動(dòng)畫(huà)。這個(gè)動(dòng)畫(huà)通過(guò)ObjectAnimator的ofFloat的方法設(shè)置縮放,要注意的是當(dāng)小球落下的時(shí)候,購(gòu)物車(chē)才開(kāi)始動(dòng)畫(huà),所以要設(shè)置一下setStartDelay這個(gè)方法。

3、具體的代碼我就貼一下動(dòng)畫(huà)部分的代碼,如果想要這個(gè)Demo看下我最后貼出的Github的地址

@Override
  public void setAnim(View view) {
    // TODO Auto-generated method stub
    int[] start_location = new int[2];// 一個(gè)整型數(shù)組用來(lái)存儲(chǔ)按鈕在屏幕的X,Y坐標(biāo)
    view.getLocationInWindow(start_location);// 購(gòu)買(mǎi)按鈕在屏幕中的坐標(biāo)
    buyImg = new ImageView(this);// 動(dòng)畫(huà)的小圓圈
    buyImg.setImageResource(R.drawable.sign);// 設(shè)置buyImg的圖片
    setAnim(buyImg, start_location);
  }
 
  /**
   * hdh: 創(chuàng)建動(dòng)畫(huà)層
   *
   * @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)畫(huà)
   *
   * @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)畫(huà)結(jié)束位置的X,Y坐標(biāo)
    text_chart_num.getLocationInWindow(end_location);// 將購(gòu)物車(chē)的位置存儲(chǔ)起來(lái)
    // 計(jì)算位移
    int endX = end_location[0] - start_location[0];// 動(dòng)畫(huà)位移的X坐標(biāo)
    int endY = end_location[1] - start_location[1];// 動(dòng)畫(huà)位移的y坐標(biāo)
    TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
    translateAnimationX.setInterpolator(new LinearInterpolator());// 設(shè)置此動(dòng)畫(huà)的加速曲線。默認(rèn)為一個(gè)線性插值。
    translateAnimationX.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)的次數(shù)
    translateAnimationX.setFillAfter(true);
 
    TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
    translateAnimationY.setInterpolator(new AccelerateInterpolator());
    translateAnimationY.setRepeatCount(0);// 動(dòng)畫(huà)重復(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)擊打開(kāi)鏈接

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論