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

首頁 > 系統 > Android > 正文

Android把商品添加到購物車的動畫效果(貝塞爾曲線)

2019-10-23 20:08:50
字體:
來源:轉載
供稿:網友

當我們寫商城類的項目的時候,一般都會有加入購物車的功能,加入購物車的時候會有一些拋物線動畫,具體代碼如下:

實現效果如圖:

android,仿購物車動畫,android購物車動畫,添加購物車動畫效果

思路:

  1. 確定動畫的起終點
  2. 在起終點之間使用二次貝塞爾曲線填充起終點之間的點的軌跡
  3. 設置屬性動畫,ValueAnimator插值器,獲取中間點的坐標
  4. 將執行動畫的控件的x、y坐標設為上面得到的中間點坐標
  5. 開啟屬性動畫
  6. 當動畫結束時的操作

難點:

PathMeasure的使用
- getLength()
- boolean getPosTan(float distance, float[] pos, float[] tan) 的理解

涉及到的知識點:

如何獲取控件在屏幕中的絕對坐標

//得到父布局的起始點坐標(用于輔助計算動畫開始/結束時的點的坐標)  int[] parentLocation = new int[2];  rl.getLocationInWindow(parentLocation);

如何使用貝塞爾曲線以及屬性動畫插值器ValueAnimator
  

 //    四、計算中間動畫的插值坐標(貝塞爾曲線)(其實就是用貝塞爾曲線來完成起終點的過程)    //開始繪制貝塞爾曲線    Path path = new Path();    //移動到起始點(貝塞爾曲線的起點)    path.moveTo(startX, startY);    //使用二次薩貝爾曲線:注意第一個起始坐標越大,貝塞爾曲線的橫向距離就會越大,一般按照下面的式子取即可    path.quadTo((startX + toX) / 2, startY, toX, toY);    //mPathMeasure用來計算貝塞爾曲線的曲線長度和貝塞爾曲線中間插值的坐標,    // 如果是true,path會形成一個閉環    mPathMeasure = new PathMeasure(path, false);    //★★★屬性動畫實現(從0到貝塞爾曲線的長度之間進行插值計算,獲取中間過程的距離值)    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());    valueAnimator.setDuration(1000);    // 勻速線性插值器    valueAnimator.setInterpolator(new LinearInterpolator());    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {      @Override      public void onAnimationUpdate(ValueAnimator animation) {        // 當插值計算進行時,獲取中間的每個值,        // 這里這個值是中間過程中的曲線長度(下面根據這個值來得出中間點的坐標值)        float value = (Float) animation.getAnimatedValue();        // ★★★★★獲取當前點坐標封裝到mCurrentPosition        // boolean getPosTan(float distance, float[] pos, float[] tan) :        // 傳入一個距離distance(0<=distance<=getLength()),然后會計算當前距        // 離的坐標點和切線,pos會自動填充上坐標,這個方法很重要。        mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此時就是中間距離點的坐標值        // 移動的商品圖片(動畫圖片)的坐標設置為該中間點的坐標        goods.setTranslationX(mCurrentPosition[0]);        goods.setTranslationY(mCurrentPosition[1]);      }    });//   五、 開始執行動畫    valueAnimator.start();

所有代碼:

package cn.c.com.beziercurveanimater;import android.animation.Animator;import android.animation.ValueAnimator;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Path;import android.graphics.PathMeasure;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.animation.LinearInterpolator;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {  private RecyclerView mRecyclerView;  private ImageView cart;  private ArrayList<Bitmap> bitmapList = new ArrayList<>();  private RelativeLayout rl;  private PathMeasure mPathMeasure;  /**   * 貝塞爾曲線中間過程的點的坐標   */  private float[] mCurrentPosition = new float[2];  private TextView count;  private int i = 0;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    initView();    initImg();    MyAdapter myAdapter = new MyAdapter(bitmapList);    mRecyclerView.setAdapter(myAdapter);    mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));  }  private void initImg() {    bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.coin));    bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.coin1));    bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.coin91));  }  private void initView() {    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);    cart = (ImageView) findViewById(R.id.cart);    rl = (RelativeLayout) findViewById(R.id.rl);    count = (TextView) findViewById(R.id.count);  }  class MyAdapter extends RecyclerView.Adapter<MyVH> {    private ArrayList<Bitmap> bitmapList;    public MyAdapter(ArrayList<Bitmap> bitmapList) {      this.bitmapList = bitmapList;    }    @Override    public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {      LayoutInflater inflater = LayoutInflater.from(MainActivity.this);      View itemView = inflater.inflate(R.layout.item, parent, false);      MyVH myVH = new MyVH(itemView);      return myVH;    }    @Override    public void onBindViewHolder(final MyVH holder, final int position) {      holder.iv.setImageBitmap(bitmapList.get(position));      holder.buy.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          addCart(holder.iv);        }      });    }    @Override    public int getItemCount() {      return bitmapList.size();    }  }  /**   * ★★★★★把商品添加到購物車的動畫效果★★★★★   * @param iv   */  private void addCart( ImageView iv) {//   一、創造出執行動畫的主題---imageview    //代碼new一個imageview,圖片資源是上面的imageview的圖片    // (這個圖片就是執行動畫的圖片,從開始位置出發,經過一個拋物線(貝塞爾曲線),移動到購物車里)    final ImageView goods = new ImageView(MainActivity.this);    goods.setImageDrawable(iv.getDrawable());    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);    rl.addView(goods, params);//    二、計算動畫開始/結束點的坐標的準備工作    //得到父布局的起始點坐標(用于輔助計算動畫開始/結束時的點的坐標)    int[] parentLocation = new int[2];    rl.getLocationInWindow(parentLocation);    //得到商品圖片的坐標(用于計算動畫開始的坐標)    int startLoc[] = new int[2];    iv.getLocationInWindow(startLoc);    //得到購物車圖片的坐標(用于計算動畫結束后的坐標)    int endLoc[] = new int[2];    cart.getLocationInWindow(endLoc);//    三、正式開始計算動畫開始/結束的坐標    //開始掉落的商品的起始點:商品起始點-父布局起始點+該商品圖片的一半    float startX = startLoc[0] - parentLocation[0] + iv.getWidth() / 2;    float startY = startLoc[1] - parentLocation[1] + iv.getHeight() / 2;    //商品掉落后的終點坐標:購物車起始點-父布局起始點+購物車圖片的1/5    float toX = endLoc[0] - parentLocation[0] + cart.getWidth() / 5;    float toY = endLoc[1] - parentLocation[1];//    四、計算中間動畫的插值坐標(貝塞爾曲線)(其實就是用貝塞爾曲線來完成起終點的過程)    //開始繪制貝塞爾曲線    Path path = new Path();    //移動到起始點(貝塞爾曲線的起點)    path.moveTo(startX, startY);    //使用二次薩貝爾曲線:注意第一個起始坐標越大,貝塞爾曲線的橫向距離就會越大,一般按照下面的式子取即可    path.quadTo((startX + toX) / 2, startY, toX, toY);    //mPathMeasure用來計算貝塞爾曲線的曲線長度和貝塞爾曲線中間插值的坐標,    // 如果是true,path會形成一個閉環    mPathMeasure = new PathMeasure(path, false);    //★★★屬性動畫實現(從0到貝塞爾曲線的長度之間進行插值計算,獲取中間過程的距離值)    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());    valueAnimator.setDuration(1000);    // 勻速線性插值器    valueAnimator.setInterpolator(new LinearInterpolator());    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {      @Override      public void onAnimationUpdate(ValueAnimator animation) {        // 當插值計算進行時,獲取中間的每個值,        // 這里這個值是中間過程中的曲線長度(下面根據這個值來得出中間點的坐標值)        float value = (Float) animation.getAnimatedValue();        // ★★★★★獲取當前點坐標封裝到mCurrentPosition        // boolean getPosTan(float distance, float[] pos, float[] tan) :        // 傳入一個距離distance(0<=distance<=getLength()),然后會計算當前距        // 離的坐標點和切線,pos會自動填充上坐標,這個方法很重要。        mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此時就是中間距離點的坐標值        // 移動的商品圖片(動畫圖片)的坐標設置為該中間點的坐標        goods.setTranslationX(mCurrentPosition[0]);        goods.setTranslationY(mCurrentPosition[1]);      }    });//   五、 開始執行動畫    valueAnimator.start();//   六、動畫結束后的處理    valueAnimator.addListener(new Animator.AnimatorListener() {      @Override      public void onAnimationStart(Animator animation) {      }      //當動畫結束后:      @Override      public void onAnimationEnd(Animator animation) {        // 購物車的數量加1        i++;        count.setText(String.valueOf(i));        // 把移動的圖片imageview從父布局里移除        rl.removeView(goods);      }      @Override      public void onAnimationCancel(Animator animation) {      }      @Override      public void onAnimationRepeat(Animator animation) {      }    });  }  class MyVH extends RecyclerView.ViewHolder {    private ImageView iv;    private TextView buy;    public MyVH(View itemView) {      super(itemView);      iv = (ImageView) itemView.findViewById(R.id.iv);      buy = (TextView) itemView.findViewById(R.id.buy);    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 额尔古纳市| 肇庆市| 重庆市| 杭州市| 淳安县| 渭南市| 四子王旗| 广东省| 保德县| 鄢陵县| 仪陇县| 吉木萨尔县| 高密市| 建湖县| 新安县| 多伦县| 西城区| 玉溪市| 十堰市| 阿城市| 榆中县| 故城县| 永泰县| 普定县| 平顶山市| 永平县| 修文县| 乌拉特后旗| 清新县| 洱源县| 改则县| 百色市| 桓台县| 东城区| 武鸣县| 金塔县| 房产| 东台市| 武城县| 江门市| 印江|