前言
美團外賣的添加物品時,物品飛入購物車的效果很炫,網上參考了諸多大神的實作,本文參考了某位大神的實作,具體連結網絡,悲催啊。不過再此仍然表示感謝。最後将此實作整合成一個工具類,友善大家使用。
此文主要講:
貝塞爾曲線的一些使用
仿鍊式程式設計的結構化代碼
正文
廢話不多說了,直接上代碼:
package com.orderform.util;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class FlyAnimUtil {
private ViewGroup mRootView;
private PathMeasure mPathMeasure;
private float[] mCurrentPosition = new float[2];//貝塞爾曲線中間過程的點的坐标
private View mFromView;
private View mToView;
private int mDrawableId;
private Context mContext;
public FlyAnimUtil(Context context){
this.mContext=context;
}
public FlyAnimUtil setFromView(View fromView){
this.mFromView=fromView;
return this;
}
public FlyAnimUtil setToView(View toView){
this.mToView=toView;
return this;
}
public FlyAnimUtil setDrawableId(int drawableId){
this.mDrawableId=drawableId;
return this;
}
public void playAnimation(final OnAfterAnimListener onAfterAnimListener) {
if(mFromView==null||mToView==null||mContext==null){
throw new NullPointerException("=====FlyAnim空指針異常============");
}
// 一、創造出執行動畫的主題---imageview
//代碼new一個imageview,圖檔資源是上面的imageview的圖檔
// (這個圖檔就是執行動畫的圖檔,從開始位置出發,經過一個抛物線(貝塞爾曲線),移動到購物車裡)
final ImageView goods = new ImageView(mContext);
goods.setImageResource(mDrawableId);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
mRootView = (ViewGroup) ((Activity)mContext).getWindow().getDecorView();
mRootView.addView(goods, params);
// 二、計算動畫開始/結束點的坐标的準備工作
//得到父布局的起始點坐标(用于輔助計算動畫開始/結束時的點的坐标)
int[] parentLocation = new int[2];
mRootView.getLocationInWindow(parentLocation);
//得到商品圖檔的坐标(用于計算動畫開始的坐标)
int startLoc[] = new int[2];
mFromView.getLocationInWindow(startLoc);
//得到購物車圖檔的坐标(用于計算動畫結束後的坐标)
int endLoc[] = new int[2];
mToView.getLocationInWindow(endLoc);
// 三、正式開始計算動畫開始/結束的坐标
//開始掉落的商品的起始點:商品起始點-父布局起始點+該商品圖檔的一半
float startX = startLoc[0] - parentLocation[0] + mFromView.getWidth() / 2;
float startY = startLoc[1] - parentLocation[1] + mFromView.getHeight() / 2;
//商品掉落後的終點坐标:購物車起始點-父布局起始點+購物車圖檔的1/5
float toX = endLoc[0] - parentLocation[0] + mToView.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(500);
// 勻速線性插值器
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) {
// 把移動的圖檔imageview從父布局裡移除
mRootView.removeView(goods);
if(onAfterAnimListener!=null){
onAfterAnimListener.afterAnim();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
public interface OnAfterAnimListener{
void afterAnim();
}
}
在 MainActivity 點選時調用如下:
new FlyAnimUtil(mContext).setFromView(view)
.setToView(mTvCount)
.setDrawableId(R.drawable.ball_round_bg)
.playAnimation(new FlyAnimUtil.OnAfterAnimListener() {
@Override
public void afterAnim() {
showShortToast("=====打老虎=====");
LogUtil.e("=====打老虎=====");
}
});
簡單介紹下使用參數:
setFromView(view) ------ view 産生圓點的控件
setToView(mTvCount) -------- mTvCount 為接收圓點的控件
setDrawableId(R.drawable.ball_round_bg) ------ R.drawable.ball_round_bg為圓點 drawable 的id
最後那個afterAnim()是點選事件的響應
這裡提供一個圓點背景的實作,我是用 drawable檔案寫的,ball_round_bg.xml 代碼如下:
android:shape="oval"
android:useLevel="false">
android:width="5dp"
android:height="5dp"/>
最後是效果圖

1.gif
好了,今天有關物品飛入購物車的效果就講到這裡,謝謝诶。