詳解Android實(shí)現(xiàn)購(gòu)物車(chē)頁(yè)面及購(gòu)物車(chē)效果(點(diǎn)擊動(dòng)畫(huà))
本文介紹了Android實(shí)現(xiàn)購(gòu)物車(chē)頁(yè)面及購(gòu)物車(chē)效果(點(diǎn)擊動(dòng)畫(huà)),分享給大家,具體如下:
效果圖如下:

思路:
(1)思考每個(gè)條目中的數(shù)字的更新原理。
(2)購(gòu)物車(chē)的動(dòng)畫(huà)效果。
(3)購(gòu)物清單怎么顯示(這個(gè)我暫時(shí)沒(méi)有寫(xiě),如果需要的話(huà),可以在我的簡(jiǎn)書(shū)下給我留言)。
1.因?yàn)檫M(jìn)入頁(yè)面,所有的商品個(gè)數(shù)都顯示為零,所以我用 ArrayList<HashMap<String, Object>> data,把商品集合都附上零:
//下面把data都添加0,為了剛開(kāi)始顯示時(shí),顯示的是0
for (int i = 0; i < list.size(); i++) {
HashMap<String, Object> myhashmap = new HashMap<String, Object>();
myhashmap.put("number", "" + 0);
data.add(myhashmap);
}
然后把data傳入Adapter:
adapter = new MyAdapter(data);
當(dāng)我們對(duì)商品進(jìn)行增減時(shí),我們可以通過(guò)hashmap來(lái)更改,如下是增加商品的部分代碼:
b = Integer.parseInt((String) data.get(position).get(
"number"));
data.get(position).put("number", "" + (b + 1));
2.購(gòu)物車(chē)動(dòng)畫(huà)效果:
首先獲取點(diǎn)擊時(shí)的XY坐標(biāo),并且設(shè)置動(dòng)畫(huà)圖片:
// ball是個(gè)imageview
startLocation = new int[2];// 一個(gè)整型數(shù)組,用來(lái)存儲(chǔ)按鈕的在屏幕的X、Y坐標(biāo)
view.getLocationInWindow(startLocation);// 這是獲取購(gòu)買(mǎi)按鈕的在屏幕的X、Y坐標(biāo)(這也是動(dòng)畫(huà)開(kāi)始的坐標(biāo))
ball = new ImageView(MainActivity.this);
ball.setImageResource(R.mipmap.sign);// 設(shè)置動(dòng)畫(huà)的圖片我的是一個(gè)小球(R.mipmap.sign)
然后是開(kāi)始執(zhí)行動(dòng)畫(huà):
private void setAnim(final View v, int[] startLocation) {
anim_mask_layout = null;
anim_mask_layout = createAnimLayout(); //創(chuàng)建動(dòng)畫(huà)層
anim_mask_layout.addView(v);//把動(dòng)畫(huà)小球添加到動(dòng)畫(huà)層
final View view = addViewToAnimLayout(anim_mask_layout, v,
startLocation);
int[] endLocation = new int[2];// 存儲(chǔ)動(dòng)畫(huà)結(jié)束位置的X、Y坐標(biāo)
re_zhongcai_tanchu.getLocationInWindow(endLocation);// re_zhongcai_tanchu是那個(gè)拋物線(xiàn)最后掉落的控件
// 計(jì)算位移
int endX = 0 - startLocation[0] + 40;// 動(dòng)畫(huà)位移的X坐標(biāo)
int endY = endLocation[1] - startLocation[1];// 動(dòng)畫(huà)位移的y坐標(biāo)
TranslateAnimation translateAnimationX = new TranslateAnimation(0,
endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());
translateAnimationX.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù)
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù)
translateAnimationX.setFillAfter(true);
final AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
set.addAnimation(translateAnimationY);
set.addAnimation(translateAnimationX);
set.setDuration(800);// 動(dòng)畫(huà)的執(zhí)行時(shí)間
view.startAnimation(set);
// 動(dòng)畫(huà)監(jiān)聽(tīng)事件
set.setAnimationListener(new Animation.AnimationListener() {
// 動(dòng)畫(huà)的開(kāi)始
@Override
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
// Log.e("動(dòng)畫(huà)","asdasdasdasd");
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
// 動(dòng)畫(huà)的結(jié)束
@Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
set.cancel();
animation.cancel();
}
});
}
需要注意的是,當(dāng)動(dòng)畫(huà)結(jié)束必須關(guān)閉動(dòng)畫(huà):
v.setVisibility(View.GONE);
set.cancel();
animation.cancel();
購(gòu)物車(chē)的彈出清單功能,我沒(méi)有寫(xiě),需要的話(huà),可以去我的簡(jiǎn)書(shū)留言.
github地址:https://github.com/javaexception/ShoppingCart
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android把商品添加到購(gòu)物車(chē)的動(dòng)畫(huà)效果(貝塞爾曲線(xiàn))
- Android中貝塞爾曲線(xiàn)的繪制方法示例代碼
- android中貝塞爾曲線(xiàn)的應(yīng)用示例
- Android貝塞爾曲線(xiàn)初步學(xué)習(xí)第三課 Android實(shí)現(xiàn)添加至購(gòu)物車(chē)的運(yùn)動(dòng)軌跡
- Android 利用三階貝塞爾曲線(xiàn)繪制運(yùn)動(dòng)軌跡的示例
- Android仿餓了么加入購(gòu)物車(chē)旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫(huà)的按鈕效果(實(shí)例詳解)
- Android實(shí)現(xiàn)購(gòu)物車(chē)添加物品的動(dòng)畫(huà)效果
- Android實(shí)現(xiàn)添加商品到購(gòu)物車(chē)動(dòng)畫(huà)效果
- Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē)
- Android利用二階貝塞爾曲線(xiàn)實(shí)現(xiàn)添加購(gòu)物車(chē)動(dòng)畫(huà)詳解
相關(guān)文章
Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話(huà)框效果(實(shí)例代碼)
這篇文章主要介紹了Android 中之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話(huà)框效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Flutter Image實(shí)現(xiàn)圖片加載
這篇文章主要為大家詳細(xì)介紹了Flutter Image實(shí)現(xiàn)圖片加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android實(shí)現(xiàn)搜索保存歷史記錄功能
這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Kotlin如何安全訪(fǎng)問(wèn)lateinit變量的實(shí)現(xiàn)
這篇文章主要介紹了Kotlin如何安全訪(fǎng)問(wèn)lateinit變量的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Android編程實(shí)現(xiàn)TCP客戶(hù)端的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TCP客戶(hù)端的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)TCP客戶(hù)端的原理及數(shù)據(jù)通信的相關(guān)技巧,需要的朋友可以參考下2016-04-04
淺談Android開(kāi)發(fā)者2017年最值得關(guān)注的25個(gè)實(shí)用庫(kù)
本篇文章主要介紹了Android開(kāi)發(fā)者2017年最值得關(guān)注的25個(gè)庫(kù),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
Android Studio 視頻播放失敗 start called in state1 異常怎么解決
很多朋友問(wèn)小編在使用MediaPlayer播放音頻時(shí)報(bào)出 E/MediaPlayerNative: start called in state 1, mPlayer(0x0)問(wèn)題,該如何處理呢,今天小編給大家?guī)?lái)了Android Studio 視頻播放失敗 start called in state1 異常問(wèn)題,需要的朋友可以參考下2020-03-03
Android 數(shù)據(jù)存儲(chǔ)之 FileInputStream 工具類(lèi)及FileInputStream類(lèi)的使用
這篇文章主要介紹了Android 數(shù)據(jù)存儲(chǔ)之 FileInputStream 工具類(lèi)及FileInputStream類(lèi)的使用的相關(guān)資料,需要的朋友可以參考下2015-11-11
Android開(kāi)發(fā)手冊(cè)SeekBar拖動(dòng)條使用實(shí)例
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)SeekBar拖動(dòng)條使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

