question
- 貝塞爾曲線(xiàn)是什么笛坦?
- 貝塞爾曲線(xiàn)可以做什么?
- 怎么做?
what is it姆泻?
貝塞爾曲線(xiàn)在百度定義是貝塞爾曲線(xiàn)(Bézier curve)咒唆,又稱(chēng)貝茲曲線(xiàn)或貝濟(jì)埃曲線(xiàn),是應(yīng)用于二維圖形應(yīng)用程序的數(shù)學(xué)曲線(xiàn)茶鹃。
do what?
貝塞爾曲線(xiàn)根據(jù)不同點(diǎn)實(shí)現(xiàn)不同動(dòng)態(tài)效果:
- 一階貝塞爾曲線(xiàn)(兩點(diǎn))涣雕,繪制成一條直線(xiàn)
- 二階貝塞爾曲線(xiàn)(三點(diǎn))
- 三階貝塞爾曲線(xiàn)(四點(diǎn))
- 四階貝塞爾曲線(xiàn)(五點(diǎn))
- 五階貝塞爾曲線(xiàn)(六點(diǎn))
看了上面貝塞爾曲線(xiàn)不同點(diǎn)不同效果后,相信大家都清楚貝塞爾曲線(xiàn)能干什么闭翩?沒(méi)錯(cuò)挣郭,貝塞爾曲線(xiàn)能造高逼格動(dòng)畫(huà)~~
就筆者目前了解的采用貝塞爾曲線(xiàn)實(shí)現(xiàn)的知名開(kāi)源項(xiàng)目有:
- QQ拖拽清除效果
- 紙飛機(jī)刷新動(dòng)畫(huà)
- 滴油刷新動(dòng)畫(huà)
- 波浪動(dòng)畫(huà)
到此大家是不是很興奮,想更多了解如何造一個(gè)高逼格貝塞爾曲線(xiàn)動(dòng)畫(huà)疗韵。接下來(lái)我就給大家講述如何造一個(gè)基于貝塞爾曲線(xiàn)實(shí)現(xiàn)的購(gòu)物車(chē)動(dòng)畫(huà)兑障,大家擦亮眼睛啦~~
how to do it
思路
- 確定動(dòng)畫(huà)起終點(diǎn)
- 在起終點(diǎn)之間使用二次貝塞爾曲線(xià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í)的操作
知識(shí)點(diǎn)
- Android中提供了繪制一階、二階者疤、三階的接口:
- 一階接口:
public void lineTo(float x,float y)
- 二階接口:
public void quadTo(float x1, float y1, float x2, float y2)
- 三階接口:
public void cubicTo(float x1, float y1, float x2, float y2, float x3, float y3)
- PathMeasure使用
- getLength()
- 理解 boolean getPosTan(float distance, float[] pos, float[] tan)
- 如何獲取控件在屏幕中的絕對(duì)坐標(biāo)
- int[] location = new int[2]; view.getLocationInWindow(location); 得到view在屏幕中的絕對(duì)坐標(biāo)福澡。
- 理解屬性動(dòng)畫(huà)插值器ValueAnimator
code
首先寫(xiě)購(gòu)物車(chē)布局xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rly_bezier_curve_shopping_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<FrameLayout
android:id="@+id/fly_bezier_curve_shopping_cart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:paddingRight="30dp"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/iv_bezier_curve_shopping_cart"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:src="@drawable/menu_shop_car_selected" />
<TextView
android:id="@+id/tv_bezier_curve_shopping_cart_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:background="@drawable/corner_view"
android:text="0"
android:layout_gravity="right"/>
</FrameLayout>
<ListView
android:id="@+id/lv_bezier_curve_shopping_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/fly_bezier_curve_shopping_cart"/>
</RelativeLayout>
然后寫(xiě)購(gòu)物車(chē)適配器宛渐、實(shí)體類(lèi)竞漾,代碼如下:
/**
* @className: GoodsAdapter
* @classDescription: 購(gòu)物車(chē)商品適配器
* @author: leibing
* @createTime: 2016/09/28
*/
public class GoodsAdapter extends BaseAdapter{
// 數(shù)據(jù)源(購(gòu)物車(chē)商品圖片)
private ArrayList<GoodsModel> mData;
// 布局
private LayoutInflater mLayoutInflater;
// 回調(diào)監(jiān)聽(tīng)
private CallBackListener mCallBackListener;
/**
* 構(gòu)造函數(shù)
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param context 上下文
* @param mData 數(shù)據(jù)源(購(gòu)物車(chē)商品圖片)
* @return
*/
public GoodsAdapter(Context context, ArrayList<GoodsModel> mData){
mLayoutInflater = LayoutInflater.from(context);
this.mData = mData;
}
@Override
public int getCount() {
return mData != null ? mData.size(): 0;
}
@Override
public Object getItem(int i) {
return mData != null ? mData.get(i): null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null){
view = mLayoutInflater.inflate(R.layout.adapter_shopping_cart_item, null);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else {
// 復(fù)用ViewHolder
viewHolder = (ViewHolder) view.getTag();
}
// 更新UI
if (i < mData.size())
viewHolder.updateUI(mData.get(i));
return view;
}
/**
* @className: ViewHolder
* @classDescription: 商品ViewHolder
* @author: leibing
* @createTime: 2016/09/28
*/
class ViewHolder{
// 顯示商品圖片
private ImageView mShoppingCartItemIv;
/**
* 構(gòu)造函數(shù)
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param view 視圖
* @return
*/
public ViewHolder(View view){
// findView
mShoppingCartItemIv = (ImageView) view.findViewById(R.id.iv_shopping_cart_item);
// onClick
view.findViewById(R.id.tv_shopping_cart_item).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mShoppingCartItemIv != null && mCallBackListener != null)
mCallBackListener.callBackImg(mShoppingCartItemIv);
}
});
}
/**
* 更新UI
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param goods 商品實(shí)體對(duì)象
* @return
*/
public void updateUI(GoodsModel goods){
if (goods != null
&& goods.getmGoodsBitmap() != null
&& mShoppingCartItemIv != null)
mShoppingCartItemIv.setImageBitmap(goods.getmGoodsBitmap());
}
}
/**
* 設(shè)置回調(diào)監(jiān)聽(tīng)
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param mCallBackListener 回調(diào)監(jiān)聽(tīng)
* @return
*/
public void setCallBackListener(CallBackListener mCallBackListener){
this.mCallBackListener = mCallBackListener;
}
/**
* @interfaceName: CallBackListener
* @interfaceDescription: 回調(diào)監(jiān)聽(tīng)
* @author: leibing
* @createTime: 2016/09/28
*/
public interface CallBackListener{
void callBackImg(ImageView goodsImg);
}
}
然后寫(xiě)添加數(shù)據(jù)源以及設(shè)置適配器,代碼如下:
// 購(gòu)物車(chē)父布局
private RelativeLayout mShoppingCartRly;
// 購(gòu)物車(chē)列表顯示
private ListView mShoppingCartLv;
// 購(gòu)物數(shù)目顯示
private TextView mShoppingCartCountTv;
// 購(gòu)物車(chē)圖片顯示
private ImageView mShoppingCartIv;
// 購(gòu)物車(chē)適配器
private GoodsAdapter mGoodsAdapter;
// 數(shù)據(jù)源(購(gòu)物車(chē)商品圖片)
private ArrayList<GoodsModel> mData;
// 貝塞爾曲線(xiàn)中間過(guò)程點(diǎn)坐標(biāo)
private float[] mCurrentPosition = new float[2];
// 路徑測(cè)量
private PathMeasure mPathMeasure;
// 購(gòu)物車(chē)商品數(shù)目
private int goodsCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findView
mShoppingCartLv = (ListView) findViewById(R.id.lv_bezier_curve_shopping_cart);
mShoppingCartCountTv = (TextView) findViewById(R.id.tv_bezier_curve_shopping_cart_count);
mShoppingCartRly = (RelativeLayout) findViewById(R.id.rly_bezier_curve_shopping_cart);
mShoppingCartIv = (ImageView) findViewById(R.id.iv_bezier_curve_shopping_cart);
// 是否顯示購(gòu)物車(chē)商品數(shù)目
isShowCartGoodsCount();
// 添加數(shù)據(jù)源
addData();
// 設(shè)置適配器
setAdapter();
}
/**
* 設(shè)置適配器
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param
* @return
*/
private void setAdapter() {
// 初始化適配器
mGoodsAdapter = new GoodsAdapter(this, mData);
// 設(shè)置適配器監(jiān)聽(tīng)
mGoodsAdapter.setCallBackListener(new GoodsAdapter.CallBackListener() {
@Override
public void callBackImg(ImageView goodsImg) {
// 添加商品到購(gòu)物車(chē)
addGoodsToCart(goodsImg);
}
});
// 設(shè)置適配器
mShoppingCartLv.setAdapter(mGoodsAdapter);
}
接下來(lái)寫(xiě)最重要的一塊窥翩,添加商品到購(gòu)物車(chē)业岁,代碼如下:
/**
* 添加商品到購(gòu)物車(chē)
* @author leibing
* @createTime 2016/09/28
* @lastModify 2016/09/28
* @param goodsImg 商品圖標(biāo)
* @return
*/
private void addGoodsToCart(ImageView goodsImg) {
// 創(chuàng)造出執(zhí)行動(dòng)畫(huà)的主題goodsImg(這個(gè)圖片就是執(zhí)行動(dòng)畫(huà)的圖片,從開(kāi)始位置出發(fā),經(jīng)過(guò)一個(gè)拋物線(xiàn)(貝塞爾曲線(xiàn)),移動(dòng)到購(gòu)物車(chē)?yán)铮? final ImageView goods = new ImageView(this);
goods.setImageDrawable(goodsImg.getDrawable());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
mShoppingCartRly.addView(goods, params);
// 得到父布局的起始點(diǎn)坐標(biāo)(用于輔助計(jì)算動(dòng)畫(huà)開(kāi)始/結(jié)束時(shí)的點(diǎn)的坐標(biāo))
int[] parentLocation = new int[2];
mShoppingCartRly.getLocationInWindow(parentLocation);
// 得到商品圖片的坐標(biāo)(用于計(jì)算動(dòng)畫(huà)開(kāi)始的坐標(biāo))
int startLoc[] = new int[2];
goodsImg.getLocationInWindow(startLoc);
// 得到購(gòu)物車(chē)圖片的坐標(biāo)(用于計(jì)算動(dòng)畫(huà)結(jié)束后的坐標(biāo))
int endLoc[] = new int[2];
mShoppingCartIv.getLocationInWindow(endLoc);
// 開(kāi)始掉落的商品的起始點(diǎn):商品起始點(diǎn)-父布局起始點(diǎn)+該商品圖片的一半
float startX = startLoc[0] - parentLocation[0] + goodsImg.getWidth() / 2;
float startY = startLoc[1] - parentLocation[1] + goodsImg.getHeight() / 2;
// 商品掉落后的終點(diǎn)坐標(biāo):購(gòu)物車(chē)起始點(diǎn)-父布局起始點(diǎn)+購(gòu)物車(chē)圖片的1/5
float toX = endLoc[0] - parentLocation[0] + mShoppingCartIv.getWidth() / 5;
float toY = endLoc[1] - parentLocation[1];
// 開(kāi)始繪制貝塞爾曲線(xiàn)
Path path = new Path();
// 移動(dòng)到起始點(diǎn)(貝塞爾曲線(xiàn)的起點(diǎn))
path.moveTo(startX, startY);
// 使用二階貝塞爾曲線(xiàn):注意第一個(gè)起始坐標(biāo)越大,貝塞爾曲線(xiàn)的橫向距離就會(huì)越大寇蚊,一般按照下面的式子取即可
path.quadTo((startX + toX) / 2, startY, toX, toY);
// mPathMeasure用來(lái)計(jì)算貝塞爾曲線(xiàn)的曲線(xiàn)長(zhǎng)度和貝塞爾曲線(xiàn)中間插值的坐標(biāo)笔时,如果是true,path會(huì)形成一個(gè)閉環(huán)
mPathMeasure = new PathMeasure(path, false);
// 屬性動(dòng)畫(huà)實(shí)現(xiàn)(從0到貝塞爾曲線(xiàn)的長(zhǎng)度之間進(jìn)行插值計(jì)算仗岸,獲取中間過(guò)程的距離值)
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());
valueAnimator.setDuration(500);
// 勻速線(xiàn)性插值器
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 當(dāng)插值計(jì)算進(jìn)行時(shí)允耿,獲取中間的每個(gè)值,
// 這里這個(gè)值是中間過(guò)程中的曲線(xiàn)長(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)和切線(xiàn)较锡,pos會(huì)自動(dòng)填充上坐標(biāo),這個(gè)方法很重要盗痒。
// mCurrentPosition此時(shí)就是中間距離點(diǎn)的坐標(biāo)值
mPathMeasure.getPosTan(value, mCurrentPosition, null);
// 移動(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) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 購(gòu)物車(chē)商品數(shù)量加1
goodsCount ++;
isShowCartGoodsCount();
mShoppingCartCountTv.setText(String.valueOf(goodsCount));
// 把執(zhí)行動(dòng)畫(huà)的商品圖片從父布局中移除
mShoppingCartRly.removeView(goods);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
代碼分析完畢蚂蕴,一個(gè)高逼格貝塞爾曲線(xiàn)實(shí)現(xiàn)的購(gòu)物車(chē)添加商品動(dòng)畫(huà)效果實(shí)現(xiàn)分析完畢~~
效果圖如下:
筆者文筆太糟低散,歡迎吐槽,如有不對(duì)之處骡楼,請(qǐng)留言指點(diǎn)~~
呼吁大家動(dòng)手實(shí)踐熔号,一切將會(huì)變得很容易~~~
項(xiàng)目地址:BezierCurveShoppingCart
關(guān)于作者
- QQ:872721111
- Email:leibing1989@126.com
- Github:leibing@github
- 簡(jiǎn)書(shū):leibing@jianshu
- 掘金:leibing@juejin