Android把商品添加到購(gòu)物車的動(dòng)畫(huà)效果(貝塞爾曲線)
當(dāng)我們寫(xiě)商城類的項(xiàng)目的時(shí)候,一般都會(huì)有加入購(gòu)物車的功能,加入購(gòu)物車的時(shí)候會(huì)有一些拋物線動(dòng)畫(huà),具體代碼如下:
實(shí)現(xiàn)效果如圖:
思路:
- 確定動(dòng)畫(huà)的起終點(diǎn)
- 在起終點(diǎn)之間使用二次貝塞爾曲線填充起終點(diǎn)之間的點(diǎn)的軌跡
- 設(shè)置屬性動(dòng)畫(huà),ValueAnimator插值器,獲取中間點(diǎn)的坐標(biāo)
- 將執(zhí)行動(dòng)畫(huà)的控件的x、y坐標(biāo)設(shè)為上面得到的中間點(diǎn)坐標(biāo)
- 開(kāi)啟屬性動(dòng)畫(huà)
- 當(dāng)動(dòng)畫(huà)結(jié)束時(shí)的操作
難點(diǎn):
PathMeasure的使用
- getLength()
- boolean getPosTan(float distance, float[] pos, float[] tan) 的理解
涉及到的知識(shí)點(diǎn):
如何獲取控件在屏幕中的絕對(duì)坐標(biāo)
//得到父布局的起始點(diǎn)坐標(biāo)(用于輔助計(jì)算動(dòng)畫(huà)開(kāi)始/結(jié)束時(shí)的點(diǎn)的坐標(biāo)) int[] parentLocation = new int[2]; rl.getLocationInWindow(parentLocation);
如何使用貝塞爾曲線以及屬性動(dòng)畫(huà)插值器ValueAnimator
// 四、計(jì)算中間動(dòng)畫(huà)的插值坐標(biāo)(貝塞爾曲線)(其實(shí)就是用貝塞爾曲線來(lái)完成起終點(diǎn)的過(guò)程) //開(kāi)始繪制貝塞爾曲線 Path path = new Path(); //移動(dòng)到起始點(diǎn)(貝塞爾曲線的起點(diǎn)) path.moveTo(startX, startY); //使用二次薩貝爾曲線:注意第一個(gè)起始坐標(biāo)越大,貝塞爾曲線的橫向距離就會(huì)越大,一般按照下面的式子取即可 path.quadTo((startX + toX) / 2, startY, toX, toY); //mPathMeasure用來(lái)計(jì)算貝塞爾曲線的曲線長(zhǎng)度和貝塞爾曲線中間插值的坐標(biāo), // 如果是true,path會(huì)形成一個(gè)閉環(huán) mPathMeasure = new PathMeasure(path, false); //★★★屬性動(dòng)畫(huà)實(shí)現(xiàn)(從0到貝塞爾曲線的長(zhǎng)度之間進(jìn)行插值計(jì)算,獲取中間過(guò)程的距離值) 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) { // 當(dāng)插值計(jì)算進(jìn)行時(shí),獲取中間的每個(gè)值, // 這里這個(gè)值是中間過(guò)程中的曲線長(zhǎng)度(下面根據(jù)這個(gè)值來(lái)得出中間點(diǎn)的坐標(biāo)值) float value = (Float) animation.getAnimatedValue(); // ★★★★★獲取當(dāng)前點(diǎn)坐標(biāo)封裝到mCurrentPosition // boolean getPosTan(float distance, float[] pos, float[] tan) : // 傳入一個(gè)距離distance(0<=distance<=getLength()),然后會(huì)計(jì)算當(dāng)前距 // 離的坐標(biāo)點(diǎn)和切線,pos會(huì)自動(dòng)填充上坐標(biāo),這個(gè)方法很重要。 mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此時(shí)就是中間距離點(diǎn)的坐標(biāo)值 // 移動(dòng)的商品圖片(動(dòng)畫(huà)圖片)的坐標(biāo)設(shè)置為該中間點(diǎn)的坐標(biāo) goods.setTranslationX(mCurrentPosition[0]); goods.setTranslationY(mCurrentPosition[1]); } }); // 五、 開(kāi)始執(zhí)行動(dòng)畫(huà) 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; /** * 貝塞爾曲線中間過(guò)程的點(diǎn)的坐標(biāo) */ 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(); } } /** * ★★★★★把商品添加到購(gòu)物車的動(dòng)畫(huà)效果★★★★★ * @param iv */ private void addCart( ImageView iv) { // 一、創(chuàng)造出執(zhí)行動(dòng)畫(huà)的主題---imageview //代碼new一個(gè)imageview,圖片資源是上面的imageview的圖片 // (這個(gè)圖片就是執(zhí)行動(dòng)畫(huà)的圖片,從開(kāi)始位置出發(fā),經(jīng)過(guò)一個(gè)拋物線(貝塞爾曲線),移動(dòng)到購(gòu)物車?yán)? final ImageView goods = new ImageView(MainActivity.this); goods.setImageDrawable(iv.getDrawable()); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100); rl.addView(goods, params); // 二、計(jì)算動(dòng)畫(huà)開(kāi)始/結(jié)束點(diǎn)的坐標(biāo)的準(zhǔn)備工作 //得到父布局的起始點(diǎn)坐標(biāo)(用于輔助計(jì)算動(dòng)畫(huà)開(kāi)始/結(jié)束時(shí)的點(diǎn)的坐標(biāo)) int[] parentLocation = new int[2]; rl.getLocationInWindow(parentLocation); //得到商品圖片的坐標(biāo)(用于計(jì)算動(dòng)畫(huà)開(kāi)始的坐標(biāo)) int startLoc[] = new int[2]; iv.getLocationInWindow(startLoc); //得到購(gòu)物車圖片的坐標(biāo)(用于計(jì)算動(dòng)畫(huà)結(jié)束后的坐標(biāo)) int endLoc[] = new int[2]; cart.getLocationInWindow(endLoc); // 三、正式開(kāi)始計(jì)算動(dòng)畫(huà)開(kāi)始/結(jié)束的坐標(biāo) //開(kāi)始掉落的商品的起始點(diǎn):商品起始點(diǎn)-父布局起始點(diǎn)+該商品圖片的一半 float startX = startLoc[0] - parentLocation[0] + iv.getWidth() / 2; float startY = startLoc[1] - parentLocation[1] + iv.getHeight() / 2; //商品掉落后的終點(diǎn)坐標(biāo):購(gòu)物車起始點(diǎn)-父布局起始點(diǎn)+購(gòu)物車圖片的1/5 float toX = endLoc[0] - parentLocation[0] + cart.getWidth() / 5; float toY = endLoc[1] - parentLocation[1]; // 四、計(jì)算中間動(dòng)畫(huà)的插值坐標(biāo)(貝塞爾曲線)(其實(shí)就是用貝塞爾曲線來(lái)完成起終點(diǎn)的過(guò)程) //開(kāi)始繪制貝塞爾曲線 Path path = new Path(); //移動(dòng)到起始點(diǎn)(貝塞爾曲線的起點(diǎn)) path.moveTo(startX, startY); //使用二次薩貝爾曲線:注意第一個(gè)起始坐標(biāo)越大,貝塞爾曲線的橫向距離就會(huì)越大,一般按照下面的式子取即可 path.quadTo((startX + toX) / 2, startY, toX, toY); //mPathMeasure用來(lái)計(jì)算貝塞爾曲線的曲線長(zhǎng)度和貝塞爾曲線中間插值的坐標(biāo), // 如果是true,path會(huì)形成一個(gè)閉環(huán) mPathMeasure = new PathMeasure(path, false); //★★★屬性動(dòng)畫(huà)實(shí)現(xiàn)(從0到貝塞爾曲線的長(zhǎng)度之間進(jìn)行插值計(jì)算,獲取中間過(guò)程的距離值) 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) { // 當(dāng)插值計(jì)算進(jìn)行時(shí),獲取中間的每個(gè)值, // 這里這個(gè)值是中間過(guò)程中的曲線長(zhǎng)度(下面根據(jù)這個(gè)值來(lái)得出中間點(diǎn)的坐標(biāo)值) float value = (Float) animation.getAnimatedValue(); // ★★★★★獲取當(dāng)前點(diǎn)坐標(biāo)封裝到mCurrentPosition // boolean getPosTan(float distance, float[] pos, float[] tan) : // 傳入一個(gè)距離distance(0<=distance<=getLength()),然后會(huì)計(jì)算當(dāng)前距 // 離的坐標(biāo)點(diǎn)和切線,pos會(huì)自動(dòng)填充上坐標(biāo),這個(gè)方法很重要。 mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此時(shí)就是中間距離點(diǎn)的坐標(biāo)值 // 移動(dòng)的商品圖片(動(dòng)畫(huà)圖片)的坐標(biāo)設(shè)置為該中間點(diǎn)的坐標(biāo) goods.setTranslationX(mCurrentPosition[0]); goods.setTranslationY(mCurrentPosition[1]); } }); // 五、 開(kāi)始執(zhí)行動(dòng)畫(huà) valueAnimator.start(); // 六、動(dòng)畫(huà)結(jié)束后的處理 valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } //當(dāng)動(dòng)畫(huà)結(jié)束后: @Override public void onAnimationEnd(Animator animation) { // 購(gòu)物車的數(shù)量加1 i++; count.setText(String.valueOf(i)); // 把移動(dòng)的圖片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); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)代碼畫(huà)虛線邊框背景效果
- Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫(huà)效果的方法
- Android實(shí)現(xiàn)在map上畫(huà)出路線的方法
- Android App中使用SurfaceView制作多線程動(dòng)畫(huà)的實(shí)例講解
- Android仿天貓商品拋物線加入購(gòu)物車動(dòng)畫(huà)
- Android利用二階貝塞爾曲線實(shí)現(xiàn)添加購(gòu)物車動(dòng)畫(huà)詳解
- Android補(bǔ)間動(dòng)畫(huà)基本使用(位移、縮放、旋轉(zhuǎn)、透明)
- Android動(dòng)畫(huà)之漸變動(dòng)畫(huà)(Tween Animation)詳解 (漸變、縮放、位移、旋轉(zhuǎn))
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android利用Canvas標(biāo)點(diǎn)畫(huà)線并加入位移動(dòng)畫(huà)(1)
相關(guān)文章
Android Studio 實(shí)現(xiàn)九宮格功能
這篇文章主要介紹了Android Studio 實(shí)現(xiàn)九宮格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04如何造個(gè)android Flow流式響應(yīng)的輪子
這篇文章主要介紹了如何造個(gè)android Flow流式響應(yīng)的輪子,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
RecyclerView比GridView來(lái)得更加強(qiáng)大,不僅是在分割線的繪制方面,在條目的編輯上也做得同樣出色,下面就來(lái)看一下Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面的實(shí)例2016-06-06Android中SurfaceView用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中SurfaceView用法,以一個(gè)簡(jiǎn)單的圖形繪制及改變位置實(shí)現(xiàn)方法分析了SurfaceView的使用技巧,需要的朋友可以參考下2015-10-10Flutter滾動(dòng)組件之SingleChildScrollView使用詳解
這篇文章主要為大家詳細(xì)介紹了Flutter滾動(dòng)組件之SingleChildScrollView使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Android自定義DataTimePicker日期時(shí)間選擇器使用詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義DataTimePicker日期時(shí)間選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
這篇文章主要介紹了Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息的使用方法,代碼很簡(jiǎn)單2014-01-01android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法
今天小編就為大家分享一篇關(guān)于android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03